-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
245 lines (230 loc) · 9.84 KB
/
index.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
});
const discord = require("discord.js");
const fs = require("fs");
const axios = require("axios").default;
const path = require("path");
var mysql = require("mysql");
if (!fs.existsSync("./config.json")) {
fs.copyFileSync("./config.template.json", "./config.json");
console.log("Update your token in config.json and restart the bot");
process.exit();
}
const config = JSON.parse(fs.readFileSync("./config.json"));
var connection = mysql.createConnection({
host : config.host,
user : config.user,
password : config.password,
database : config.database
});
connection.connect();
const bot = new discord.Client({
ws: {
intents: [
"GUILDS",
"GUILD_MEMBERS",
"GUILD_MESSAGES",
"GUILD_MESSAGE_REACTIONS",
"GUILD_BANS",
"GUILD_INVITES",
"GUILD_PRESENCES",
"DIRECT_MESSAGES",
"DIRECT_MESSAGE_REACTIONS"
]
}
});
bot.on("ready", () => {
console.log("Bot connected");
});
bot.on("presenceUpdate", (_, presence) => {
presence.activities.forEach(async activity => {
if (activity.name == "Dolphin Emulator") {
const gameRegex = /(.*)\((.*)\)/;
const regexRes = gameRegex.exec(activity.details);
if (!regexRes) return;
const gameID = regexRes[2];
const game = regexRes[1];
if (gameID.includes(",")) {
gameID = gameID.split(",")[0];
console.log(gameID);
}
console.log(gameID);
if (gameID.length > 6) {
console.log(`${presence.user.username} is playing a game that isn't available on RiiTag.`);
return;
}
if (gameID) {
var key = await getKey(presence.user.id);
console.log(key);
if (!key) {
console.log(`${presence.user.username} does not have a registered account on RiiTag.`);
return;
}
try {
var url = `http://tag.rc24.xyz/wii?key=${key}&game=${gameID}`;
//console.log(url);
var res = await axios.get(encodeURI(url));
if (res.status == 200) {
console.log(`${presence.user.username} is now playing ${activity.details}.`);
} else {
console.log(`Request for ${presence.user.username} failed with response code ${res.status} for game ${activity.details}.`);
}
} catch (error) {
console.log(`Error occurred during the request: ${error.message}`);
}
} else {
console.log("No Game ID detected");
}
}
if (activity.name == "citra" || activity.name == "Nintendo 3DS" || activity.name.includes("(3DS)")) {
if (activity.name == "citra") {
currGame = activity.state;
}
if (activity.name == "Nintendo 3DS") {
currGame = activity.details;
}
if (activity.name.includes("(3DS)")) {
currGame = activity.name.replace(" (3DS)", "");
}
if (currGame) {
currGame = currGame.replace(/&/g, "%26").replace("\n", " ");
var key = await getKey(presence.user.id);
if (!key) {
console.log(`${presence.user.username} does not have a registered account on RiiTag.`);
return;
}
try {
var url = `http://tag.rc24.xyz/3ds?key=${key}&gameName=${currGame}`;
//console.log(url);
var res = await axios.get(encodeURI(url));
if (res.status == 200) {
console.log(`${presence.user.username} is now playing ${currGame}.`);
} else {
console.log(`Request for ${presence.user.username} failed with response code ${res.status} for game ${currGame}.`);
}
} catch (error) {
console.log(`Error occurred during the request: ${error.message}`);
}
} else {
console.log("No Game detected");
}
}
if (activity.name == "Cemu") {
currGame = activity.state;
if ( currGame && currGame != "Idling" ) {
currGame = currGame.replace("Playing", "").trim().replace(/&/g, "%26");
var key = await getKey(presence.user.id);
if (!key) {
console.log(`${presence.user.username} does not have a registered account on RiiTag.`);
return;
}
try {
var url = `http://tag.rc24.xyz/wiiu?key=${key}&origin=Cemu&gameTID=${currGame}`;
//console.log(url);
var res = await axios.get(encodeURI(url));
if (res.status == 200) {
console.log(`${presence.user.username} is now playing ${currGame}.`);
} else {
console.log(`Request for ${presence.user.username} failed with response code ${res.status} for game ${currGame}.`);
}
} catch (error) {
console.log(`Error occurred during the request: ${error.message}`);
}
} else {
console.log("No Game detected");
}
}
if (activity.name == "Nintendo Switch" || activity.name == "Switch") {
if (activity.name == "Nintendo Switch") {
currGame = activity.details;
}
if (activity.name == "Switch") {
currGame = activity.details.replace("Playing ", "");
}
if (currGame) {
currGame = currGame.replace(/&/g, "%26").replace("\n", " ");
var key = await getKey(presence.user.id);
if (!key) {
console.log(`${presence.user.username} does not have a registered account on RiiTag.`);
return;
}
try {
var url = `http://tag.rc24.xyz/switch?key=${key}&gameName=${currGame}`;
//console.log(url);
var res = await axios.get(encodeURI(url));
if (res.status == 200) {
console.log(`${presence.user.username} is now playing ${currGame}.`);
} else {
console.log(`Request for ${presence.user.username} failed with response code ${res.status} for game ${currGame}.`);
}
} catch (error) {
console.log(`Error occurred during the request: ${error.message}`);
}
} else {
console.log("No Game detected");
}
}
if (activity.name == "Yuzu") {
currGame = activity.state;
if ( currGame && currGame != "Currently not in game" ) {
currGame = currGame.trim().replace(/&/g, "%26");
var key = await getKey(presence.user.id);
if (!key) {
console.log(`${presence.user.username} does not have a registered account on RiiTag.`);
return;
}
var url = `http://tag.rc24.xyz/switch?key=${key}&game=${currGame}&source=Yuzu`;
//console.log(url);
var res = await axios.get(encodeURI(url));
if (res.status == 200) {
console.log(`${presence.user.username} is now playing ${activity.state}.`);
} else {
console.log(`Request for ${presence.user.username} failed with response code ${res.status} for game ${activity.state}}.`);
}
} else {
console.log("No Game detected");
}
}
if (activity.name == "Ryujinx") {
currGame = activity.state;
if ( currGame && currGame != "Idling" ) {
currGame = currGame.replace("Playing", "").trim().replace(/&/g, "%26");
var key = await getKey(presence.user.id);
if (!key) {
console.log(`${presence.user.username} does not have a registered account on RiiTag.`);
return;
}
var url = `http://tag.rc24.xyz/switch?key=${key}&game=${currGame}&source=Ryujinx`;
//console.log(url);
var res = await axios.get(encodeURI(url));
if (res.status == 200) {
console.log(`${presence.user.username} is now playing ${activity.state}.`);
} else {
console.log(`Request for ${presence.user.username} failed with response code ${res.status} for game ${activity.state}}.`);
}
} else {
console.log("No Game detected");
}
}
});
});
bot.on("messageReactionAdd", (reaction, user) => {
console.log("Hi there!!");
console.log(reaction.emoji);
if (reaction.message.author.id == bot.user.id && reaction.emoji == bot.emojis.resolve("🚫")) {
console.log(`${user.username} opted out of future RiiTag notifications.`);
}
});
function saveConfig() {
fs.writeFileSync("config.json", JSON.stringify(config, null, 4));
}
async function getKey(id) {
return new Promise((resolve, reject) => {
connection.query("SELECT randkey from `user` WHERE `username` = ?", [id], function (err, res, fields) {
if (err) return reject(err);
return resolve(res[0].randkey);
});
})
}
bot.login(config.token);