forked from AttorneyOnline/AO2-Client
-
Notifications
You must be signed in to change notification settings - Fork 3
/
discord_rich_presence.cpp
142 lines (119 loc) · 3.94 KB
/
discord_rich_presence.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "discord_rich_presence.h"
#include <cstring>
#include <ctime>
#include <QDebug>
namespace AttorneyOnline {
Discord::Discord()
{
// DiscordEventHandlers handlers;
// std::memset(&handlers, 0, sizeof(handlers));
// handlers = {};
// handlers.ready = [] {
// qInfo() << "Discord RPC ready";
// };
// handlers.disconnected = [](int errorCode, const char* message) {
// qInfo() << "Discord RPC disconnected! " << message;
// };
// handlers.errored = [](int errorCode, const char* message) {
// qWarning() << "Discord RPC errored out! " << message;
// };
// qInfo() << "Initializing Discord RPC";
// Discord_Initialize(APPLICATION_ID1, &handlers, 1, nullptr);
start(APPLICATION_ID[0]);
}
void Discord::start(const char *APPLICATION_ID)
{
DiscordEventHandlers handlers;
std::memset(&handlers, 0, sizeof(handlers));
handlers = {};
handlers.ready = [] {
qInfo() << "Discord RPC ready";
};
handlers.disconnected = [](int errorCode, const char* message) {
qInfo() << "Discord RPC disconnected! " << message;
};
handlers.errored = [](int errorCode, const char* message) {
qWarning() << "Discord RPC errored out! " << message;
};
qInfo() << "Initializing Discord RPC";
Discord_Initialize(APPLICATION_ID, &handlers, 1, nullptr);
}
Discord::~Discord()
{
Discord_Shutdown();
}
void Discord::restart(const char *APPLICATION_ID)
{
Discord_Shutdown();
start(APPLICATION_ID);
}
void Discord::toggle(int p_index)
{
if(p_index >=0 && p_index < 2)
{
if(p_index!=m_index) { restart(APPLICATION_ID[p_index]); m_index = p_index; }
}
else qDebug() << p_index << "is not a valid APPLICATION_ID Index";
}
void Discord::state_lobby()
{
DiscordRichPresence presence;
std::memset(&presence, 0, sizeof(presence));
presence.largeImageKey = "danganronpa_online";
presence.largeImageText = "Sore Wa Chigau Yo!";
presence.instance = 1;
presence.state = "In Lobby";
presence.details = "Idle";
Discord_UpdatePresence(&presence);
}
void Discord::state_server(std::string name, std::string server_id)
{
qDebug() << "Discord RPC: Setting server state";
DiscordRichPresence presence;
std::memset(&presence, 0, sizeof(presence));
presence.largeImageKey = "danganronpa_online";
presence.largeImageText = "Sore Wa Chigau Yo!";
presence.instance = 1;
auto timestamp = static_cast<int64_t>(std::time(nullptr));
presence.state = "In a Server";
presence.details = name.c_str();
presence.matchSecret = server_id.c_str();
presence.startTimestamp = this->timestamp;
this->server_id = server_id;
this->server_name = name;
this->timestamp = timestamp;
Discord_UpdatePresence(&presence);
}
void Discord::state_character(std::string name)
{
auto name_internal = QString(name.c_str()).toLower().replace(' ', '_').toStdString();
auto name_friendly = QString(name.c_str()).replace('_', ' ').toStdString();
const std::string playing_as = "Playing as " + name_friendly;
qDebug() << "Discord RPC: Setting character state (" << playing_as.c_str() << ")";
DiscordRichPresence presence;
std::memset(&presence, 0, sizeof(presence));
presence.largeImageKey = name_internal.c_str();
presence.instance = 1;
presence.details = this->server_name.c_str();
presence.matchSecret = this->server_id.c_str();
presence.startTimestamp = this->timestamp;
presence.state = playing_as.c_str();
presence.smallImageKey = "danganronpa_online";
presence.smallImageText = "Danganronpa Online";
Discord_UpdatePresence(&presence);
}
void Discord::state_spectate()
{
qDebug() << "Discord RPC: Setting specator state";
DiscordRichPresence presence;
std::memset(&presence, 0, sizeof(presence));
presence.largeImageKey = "danganronpa_online";
presence.largeImageText = "Sore Wa Chigau Yo!";
presence.instance = 1;
presence.details = this->server_name.c_str();
presence.matchSecret = this->server_id.c_str();
presence.startTimestamp = this->timestamp;
presence.state = "Spectating";
Discord_UpdatePresence(&presence);
}
}