forked from Pizzabelly/EasyRP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.cpp
99 lines (87 loc) · 2.97 KB
/
discord.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "include/discord-rpc.h"
#include <ctime>
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <string>
#include <conio.h>
static void handleDiscordReady()
{
std::cout << "\nDiscord: ready" << std::endl;
}
//Handle Discord Disconnected Event
static void handleDiscordDisconnected(int errcode, const char* message)
{
std::cout << printf("\nDiscord: disconnected (%d: %s)\n", errcode, message);
}
//Handle Discord Error Event
static void handleDiscordError(int errcode, const char* message)
{
std::cout << printf("\nDiscord: error (%d: %s)\n", errcode, message);
}
//Shutdown discord-rp
void Shutdown()
{
_getch();
std::cout << "Shutting Down..." << std::endl;
Discord_Shutdown();
exit(1);
}
//Update Discord Rich Presence
void UpdatePresence(const char* state, const char* details, const char* largeImageKey,
const char* smallImageKey, const char* smallImageText, const char* largeImageText, int64_t timestamp)
{
//Set required variables
char buffer[256];
DiscordRichPresence discordPresence;
memset(&discordPresence, 0, sizeof(discordPresence));
//Make sure that state nor details are either too big for discord or not set, then set them
if (sizeof(state) > 128 || strlen(state) < 1)
{
std::cout << "\nState parameter is too long or not set\nPress any key to exit..." << std::endl;
Shutdown();
}
discordPresence.state = state;
if (sizeof(details) > 128 || strlen(details) < 1)
{
std::cout << "\nDetails parameter is too long or not set\nPress any key to exit..." << std::endl;
Shutdown();
}
sprintf_s(buffer, "%s", details);
discordPresence.details = buffer;
if (strlen(largeImageKey) < 1)
{
std::cout << "\nLargeImage parameter not set\nPress any key to exit..." << std::endl;
Shutdown();
}
discordPresence.largeImageKey = largeImageKey;
if (timestamp != NULL){
if (timestamp == 1) {discordPresence.endTimestamp = time(0) + 0xF;}
if (timestamp>1) { discordPresence.startTimestamp = timestamp; }
}
//Make sure not to set the optional variables if they are not defined in the config
if (!(strlen(smallImageKey) < 1))
discordPresence.smallImageKey = smallImageKey;
if (!(strlen(smallImageText) < 1))
discordPresence.smallImageText = smallImageText;
if (!(strlen(largeImageText) < 1))
discordPresence.largeImageText = largeImageText;
//Actaully update the presence
Discord_UpdatePresence(&discordPresence);
}
//Initialize Discord Rich Presence
void InitDiscord(const char* clientID)
{
DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
handlers.ready = handleDiscordReady;
handlers.errored = handleDiscordError;
handlers.disconnected = handleDiscordDisconnected;
if (strlen(clientID) < 1)
{
std::cout << "\nClientID Not Set" << std::endl;
Shutdown();
}
Discord_Initialize(clientID, &handlers, 1, NULL);
}