add Discord_ClearPresence() (#104)

send-presence example updated - start a line with 'c' to toggle
whether presence information is sent or not.

Added ClearPresence to .cs bindings

Added ClearPresence to UE4 blueprint class.
This commit is contained in:
Ben Morse 2018-01-05 15:17:23 -08:00 committed by GitHub
parent b0e31a9e25
commit 6a59509b7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 18 deletions

View file

@ -78,6 +78,9 @@ public class DiscordRpc
[DllImport("discord-rpc", EntryPoint = "Discord_UpdatePresence", CallingConvention = CallingConvention.Cdecl)] [DllImport("discord-rpc", EntryPoint = "Discord_UpdatePresence", CallingConvention = CallingConvention.Cdecl)]
public static extern void UpdatePresence(ref RichPresence presence); public static extern void UpdatePresence(ref RichPresence presence);
[DllImport("discord-rpc", EntryPoint = "Discord_ClearPresence", CallingConvention = CallingConvention.Cdecl)]
public static extern void ClearPresence();
[DllImport("discord-rpc", EntryPoint = "Discord_Respond", CallingConvention = CallingConvention.Cdecl)] [DllImport("discord-rpc", EntryPoint = "Discord_Respond", CallingConvention = CallingConvention.Cdecl)]
public static extern void Respond(string userId, Reply reply); public static extern void Respond(string userId, Reply reply);
} }

View file

@ -14,6 +14,7 @@
static const char* APPLICATION_ID = "345229890980937739"; static const char* APPLICATION_ID = "345229890980937739";
static int FrustrationLevel = 0; static int FrustrationLevel = 0;
static int64_t StartTime; static int64_t StartTime;
static int SendPresence = 1;
static int prompt(char* line, size_t size) static int prompt(char* line, size_t size)
{ {
@ -32,24 +33,28 @@ static int prompt(char* line, size_t size)
static void updateDiscordPresence() static void updateDiscordPresence()
{ {
char buffer[256]; if (SendPresence) {
DiscordRichPresence discordPresence; char buffer[256];
memset(&discordPresence, 0, sizeof(discordPresence)); DiscordRichPresence discordPresence;
discordPresence.state = "West of House"; memset(&discordPresence, 0, sizeof(discordPresence));
sprintf(buffer, "Frustration level: %d", FrustrationLevel); discordPresence.state = "West of House";
discordPresence.details = buffer; sprintf(buffer, "Frustration level: %d", FrustrationLevel);
discordPresence.startTimestamp = StartTime; discordPresence.details = buffer;
discordPresence.endTimestamp = time(0) + 5 * 60; discordPresence.startTimestamp = StartTime;
discordPresence.largeImageKey = "canary-large"; discordPresence.endTimestamp = time(0) + 5 * 60;
discordPresence.smallImageKey = "ptb-small"; discordPresence.largeImageKey = "canary-large";
discordPresence.partyId = "party1234"; discordPresence.smallImageKey = "ptb-small";
discordPresence.partySize = 1; discordPresence.partyId = "party1234";
discordPresence.partyMax = 6; discordPresence.partySize = 1;
discordPresence.matchSecret = "xyzzy"; discordPresence.partyMax = 6;
discordPresence.joinSecret = "join"; discordPresence.matchSecret = "xyzzy";
discordPresence.spectateSecret = "look"; discordPresence.joinSecret = "join";
discordPresence.instance = 0; discordPresence.spectateSecret = "look";
Discord_UpdatePresence(&discordPresence); discordPresence.instance = 0;
Discord_UpdatePresence(&discordPresence);
} else {
Discord_ClearPresence();
}
} }
static void handleDiscordReady() static void handleDiscordReady()
@ -143,6 +148,18 @@ static void gameLoop()
continue; continue;
} }
if (line[0] == 'c') {
if (SendPresence) {
printf("Clearing presence information.\n");
SendPresence = 0;
} else {
printf("Restoring presence information.\n");
SendPresence = 1;
}
updateDiscordPresence();
continue;
}
if (line[0] == 'y') { if (line[0] == 'y') {
printf("Reinit Discord.\n"); printf("Reinit Discord.\n");
discordInit(); discordInit();

View file

@ -145,3 +145,8 @@ void UDiscordRpc::UpdatePresence()
Discord_UpdatePresence(&rp); Discord_UpdatePresence(&rp);
} }
void UDiscordRpc::ClearPresence()
{
Discord_ClearPresence();
}

View file

@ -108,6 +108,11 @@ public:
Category = "Discord") Category = "Discord")
void UpdatePresence(); void UpdatePresence();
UFUNCTION(BlueprintCallable,
meta = (DisplayName = "Clear presence", Keywords = "Discord rpc"),
Category = "Discord")
void ClearPresence();
UPROPERTY(BlueprintReadOnly, UPROPERTY(BlueprintReadOnly,
meta = (DisplayName = "Is Discord connected", Keywords = "Discord rpc"), meta = (DisplayName = "Is Discord connected", Keywords = "Discord rpc"),
Category = "Discord") Category = "Discord")

View file

@ -76,6 +76,7 @@ DISCORD_EXPORT void Discord_UpdateConnection(void);
#endif #endif
DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence* presence); DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence* presence);
DISCORD_EXPORT void Discord_ClearPresence();
DISCORD_EXPORT void Discord_Respond(const char* userid, /* DISCORD_REPLY_ */ int reply); DISCORD_EXPORT void Discord_Respond(const char* userid, /* DISCORD_REPLY_ */ int reply);

View file

@ -325,6 +325,11 @@ extern "C" DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence*
SignalIOActivity(); SignalIOActivity();
} }
extern "C" DISCORD_EXPORT void Discord_ClearPresence()
{
Discord_UpdatePresence(nullptr);
}
extern "C" DISCORD_EXPORT void Discord_Respond(const char* userId, /* DISCORD_REPLY_ */ int reply) extern "C" DISCORD_EXPORT void Discord_Respond(const char* userId, /* DISCORD_REPLY_ */ int reply)
{ {
// if we are not connected, let's not batch up stale messages for later // if we are not connected, let's not batch up stale messages for later

View file

@ -102,6 +102,7 @@ size_t JsonWriteRichPresenceObj(char* dest,
WriteKey(writer, "pid"); WriteKey(writer, "pid");
writer.Int(pid); writer.Int(pid);
if (presence != nullptr)
{ {
WriteObject activity(writer, "activity"); WriteObject activity(writer, "activity");