-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathODUE_discord.js
More file actions
497 lines (453 loc) · 15.4 KB
/
ODUE_discord.js
File metadata and controls
497 lines (453 loc) · 15.4 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*:
* @plugindesc (Ver 1.3) Discord Rich Presence integration to RPG Maker MZ.
* @author ODUE
* @url https://github.com/00due/Discord-RPC-for-RMMZ
* @target MZ
*
* @help
* Initial setup:
*
* 0. If you don't have Node.js installed, install it from https://nodejs.org/en/download
*
* 1. Open the folder of your game (where you have your game.rmmzproject in) and open CMD or powershell there.
*
* 2. Run the command 'npm install discord-rpc' (without the '' symbols)
* ---If you're getting an error Command not found or something like that, make sure you have Node.js installed!---
*
* 3. Go to https://discord.com/developers/applications and create your application. Click 'New application' and give it your game's name.
* After creating it, open the Rich Presence --> Art assets tab, and upload the large and small images.
* ---Recommendation: The large image is typically the logo of your game.---
* ---WARNING: The art assets have about 10-15 minutes delay until they become visible.---
*
* 4. Copy the Application ID from the 'General information' tab, and paste it into the 'Discord application ID' on this plugin.
*
* 5. Fill the other plugin parameters with anything you want.
* REQUIRED TO FILL:
* Application ID, Large picture, Large picture text, Row 1
*
*
*
* Changing the details after the initial setup:
*
* 1. Open Plugin commands --> ODUE_discord
*
* 2. Select either Edit row 1 or 2
*
* 3. Type the new value of it.
*
* You can also save the current values for later use.
* As of ver1.1, deleting the second row is also possible.
*
*
* Plugins Commands (v1.2 Extended by Maxii1996)
*
* You can use:
*
* \partyX[stat] or \v[x]
*
* Inside a plugin command to return that stat or variables
* inside command plugins.
*
* Example:
*
* \party1[name] will return Party 1 position Name.
*
*
*
* Terms of use:
*
* 1. You must give credit to ODUE
* 2. You can freely edit this plugin to your needs. However, you must still credit me.
* 3. This plugin is free for commercial and non-commercial projects.
* 4. This plugin is provided as is. I'm not responsible for anything you make with this plugin.
* 5. You can send feature requests to me on platforms such as Reddit (to u/SanttuPOIKA----).
* However, I have no obligation to fulfill your requests.
*
*
*
* @param Discord application ID
* @desc Type here your game's application ID
* @type text
*
* @param
*
* @param Large picture
* @desc Enter the name of the large picture you want to use.
* @type text
*
* @param Large picture text
* @desc Enter the text when hovering the large picture with your cursor. (Max 128 characters)
* @type text
* @default Playing a game
*
* @param Enable small picture
* @desc Enables the small picture shown in the rich presence.
* @type boolean
* @default false
* @on yes
* @off no
*
* @param Small picture
* @desc Enter the name of the small picture you want to use.
* @parent Enable small picture
* @type text
*
* @param Small picture text
* @desc Enter the text when hovering the small picture with your cursor. (Max 128 characters)
* @parent Enable small picture
* @type text
* @default Developed by someone
*
* @param
*
* @param Row 1
* @desc The first row of text in Discord. (Max 128 characters)
* @type text
* @default Playing a cool game!
*
* @param Show row 2
* @desc Disable if you don't want the second row to be visible.
* @type boolean
* @default true
* @on yes
* @off no
*
* @param Row 2
* @desc The second row of text in Discord. (Max 128 characters)
* @parent Show row 2
* @type text
* @default Exploring a cool world!
*
* @param
*
* @param Enable button 1
* @desc Enable the first button
* @type boolean
* @default true
* @on Enabled
* @off Disabled
*
* @param Button 1 URL
* @parent Enable button 1
* @desc Where should the first button lead to?
* @type text
* @default https://yourname.itch.io/your-game/
*
* @param Button 1 text
* @parent Enable button 1
* @desc The text on the button. (Max 32 characters)
* @type text
* @default Download this game!
*
* @param Enable button 2
* @desc Enable the second button (WARNING: You must enable Button 1 for this to work.)
* @type boolean
* @default false
* @on Enabled
* @off Disabled
*
* @param Button 2 URL
* @parent Enable button 2
* @desc Where should the first button lead to?
* @type text
* @default https://your-website.com/
*
* @param Button 2 text
* @parent Enable button 2
* @desc The text on the button. (Max 32 characters)
* @type text
* @default Visit the game's website!
*
* @param Show playtime
* @desc Shows how long has been played
* @type boolean
* @default true
* @on Show
* @off Don't show
*
* @param Enable settings option
* @desc Enable the settings option in the game to let the player enable / disable RPC.
* @type boolean
* @default false
* @on Show option
* @off Don't show
*
*
* @command Edit row 1
* @desc Edit the row 1 of Discord status
*
* @arg row1
* @text New value
* @desc Max 128 characters
* @type text
*
* @command Edit row 2
* @desc Edit the row 2 of Discord status
*
* @arg row2
* @text New value
* @desc Max 128 characters
* @type text
*
* @command Disable row 2
* @desc Disable the row 2 of Discord status
*
* @command Enable row 2
* @desc Enable the row 2 of Discord status
*
* @command Save values
* @desc Saves the current values of row 1 and 2
*
* @command Restore values
* @desc Restores the saved values of the rows
*
* @arg restoreRow1
* @text First row
* @type boolean
* @on Restore
* @off Don't restore
* @default true
*
* @arg restoreRow2
* @text Second row
* @type boolean
* @on Restore
* @off Don't restore
* @default true
*
*
* @command Change small picture
* @desc Change the Discord's small picture.
*
* @arg newSmallPicture
* @text New Small Picture
* @desc Name of the new small picture.
* @type text
*
* @command Change small picture text
* @desc Change the text of Discord's small picture.
*
* @arg newSmallPictureText
* @text New Small Picture Text
* @desc Text for the new small picture.
* @type text
*
* @command Toggle RPC
* @desc Disable or enable the Discord RPC visibility.
*
* @arg State
* @type boolean
* @default false
* @on Enable
* @off Disable
*
*/
if (typeof require !== 'function' || typeof process !== 'object' || !process.versions || !process.versions.nw) {
console.warn('Warning: nwjs not found (running in browser?) - Discord Rich Presence disabled.');
}
else {
let parameters = PluginManager.parameters('ODUE_discord');
const appId = parameters['Discord application ID'];
if (appId.length < 10) {
console.error("DISCORD ERROR: Invalid Application ID!");
}
let bigPicture = parameters['Large picture'];
let bigPictureText = parameters['Large picture text'];
let smallPictureEnabled = parameters['Enable small picture'] === "true";
let smallPicture = parameters['Small picture'];
let smallPictureText = parameters['Small picture text'];
let firstRow = parameters['Row 1'];
let secondRow = parameters['Row 2'];
let firstRowSaved;
let secondRowSaved;
let row2Enabled = parameters['Show row 2'] === "true";
let playtime;
if (parameters['Show playtime'] === "true") {
playtime = Date.now();
}
let button1Url;
let button1Text;
let button2Url;
let button2Text;
let buttons = getButtons(parameters);
let isActivityDisabledFromSettings = false;
let isActivityDisabledFromCommand = false;
let toggleRPC = function(shouldEnable, whereIsToggled) {
if (client) {
if (whereIsToggled === "settings") {
isActivityDisabledFromSettings = !shouldEnable;
}
else if (whereIsToggled === "command") {
isActivityDisabledFromCommand = !shouldEnable;
}
setPresence();
}
}
if (parameters['Enable settings option'] === "true") {
const Window_Options_prototype_addGeneralOptions = Window_Options.prototype.addGeneralOptions;
Window_Options.prototype.addGeneralOptions = function() {
Window_Options_prototype_addGeneralOptions.call(this);
this.addCommand("Enable Discord RPC", "useRPC", true);
};
const Window_Options_prototype_setConfigValue = Window_Options.prototype.setConfigValue;
Window_Options.prototype.setConfigValue = function(symbol, volume) {
Window_Options_prototype_setConfigValue.call(this, symbol, volume);
if (symbol == "useRPC") toggleRPC(volume, "settings");
};
const ConfigManager_makeData = ConfigManager.makeData;
ConfigManager.makeData = function() {
const config = ConfigManager_makeData.call(this);
config.useRPC = this.useRPC;
return config
}
const ConfigManager_applyData = ConfigManager.applyData;
ConfigManager.applyData = function(config) {
ConfigManager_applyData.call(this, config);
this.useRPC = this.readFlag(config, "useRPC", true);
}
}
function getButtons(parameters) {
if (parameters['Enable button 1'] === "true") {
button1Text = parameters['Button 1 text'];
button1Url = parameters['Button 1 URL'];
let buttonArr = [{ label: button1Text, url: button1Url }];
if (parameters['Enable button 2'] === "true") {
button2Text = parameters['Button 2 text'];
button2Url = parameters['Button 2 URL'];
buttonArr.push({ label: button2Text, url: button2Url });
}
return buttonArr;
}
}
function interpretText(text) {
text = text.replace(/\\v\[(\d+)\]/gi, function(match, p1) {
return $gameVariables.value(Number(p1));
});
text = text.replace(/\\party(\d+)\[(\w+)\]/gi, function(match, p1, p2) {
const memberIndex = Number(p1) - 1;
const stat = p2.toLowerCase();
const actor = $gameParty.members()[memberIndex];
if (actor) {
if (['mhp', 'mmp', 'atk', 'def', 'mat', 'mdf', 'agi', 'luk', 'hp', 'mp', 'tp', 'level'].includes(stat)) {
return actor[stat];
} else if (['name', 'nickname', 'profile'].includes(stat)) {
return actor[stat]();
} else if (stat === 'class') {
return actor.currentClass().name;
} else {
return '';
}
}
else return '';
});
return text;
}
//Warnings
function checkStringLength(text, maxLength, errorMessage) {
if (text.length > maxLength) console.error(errorMessage)
}
let stringsToCheck = [
{string: firstRow, length: 128, error: "DISCORD ERROR: The length of row 1 is over 128 characters.\nDiscord rich presence has been disabled."},
{string: secondRow, length: 128, error: "DISCORD ERROR: The length of row 2 is over 128 characters.\nDiscord rich presence has been disabled."},
{string: button1Text, length: 32, error: "DISCORD ERROR: The length of button 1 text is over 32 characters.\nDiscord rich presence has been disabled."},
{string: button2Text, length: 32, error: "DISCORD ERROR: The length of button 2 text is over 32 characters.\nDiscord rich presence has been disabled."},
{string: bigPictureText, length: 128, error: "DISCORD ERROR: The length of large picture text is over 32 characters.\nDiscord rich presence has been disabled."},
{string: smallPictureText, length: 128, error: "DISCORD ERROR: The length of small picture text is over 32 characters.\nDiscord rich presence has been disabled."}
]
for (let {string, length, error} of stringsToCheck) {
try {
checkStringLength(string, length, error);
}
catch (TypeError) {
console.warn("WARNING: Length check failed. Don't worry, this shouldn't matter.");
}
}
const rpc = require("discord-rpc");
const client = new rpc.Client({ transport: 'ipc' });
client.login({ clientId: appId });
const createActivityObject = (details, state) => ({
pid: process.pid,
activity: {
details,
...(state && {state}),
timestamps: { start: playtime },
assets: {
large_image: bigPicture,
large_text: bigPictureText,
...(smallPictureEnabled && {
small_image: smallPicture,
small_text: smallPictureText
}),
},
buttons,
}
});
let setPresence = function () {
if (isActivityDisabledFromSettings || isActivityDisabledFromCommand) {
client.clearActivity();
console.log("Discord RPC is disabled.");
return;
}
let activity = createActivityObject(firstRow, secondRow);
client.request('SET_ACTIVITY', activity);
console.log("Discord RPC is enabled.");
};
let deleteRow2 = function () {
let activity = createActivityObject(firstRow);
client.request('SET_ACTIVITY', activity);
};
client.on('ready', () => {
if (row2Enabled) setPresence();
else deleteRow2();
});
PluginManager.registerCommand("ODUE_discord", 'Save values', () => {
firstRowSaved = firstRow;
secondRowSaved = secondRow;
});
PluginManager.registerCommand("ODUE_discord", 'Edit row 1', args => {
let interpretedText = interpretText(args.row1);
if (interpretedText.length <= 128) {
firstRow = interpretedText;
if (row2Enabled) setPresence();
else deleteRow2();
}
else console.error("DISCORD ERROR: The length of row 1 is over 128 characters.\nDiscord rich presence has been disabled.")
});
PluginManager.registerCommand("ODUE_discord", 'Edit row 2', args => {
let interpretedText = interpretText(args.row2);
if (interpretedText.length <= 128) {
secondRow = interpretedText;
if (row2Enabled) setPresence();
else deleteRow2();
}
else console.error("DISCORD ERROR: The length of row 2 is over 128 characters.\nDiscord rich presence has not been updated.")
});
PluginManager.registerCommand("ODUE_discord", 'Restore values', args => {
if (args.restoreRow1 === "true" && firstRowSaved.length !== 0) firstRow = firstRowSaved;
if (args.restoreRow2 === "true" && secondRowSaved.length !== 0) secondRow = secondRowSaved;
if (row2Enabled) setPresence();
else deleteRow2();
});
PluginManager.registerCommand("ODUE_discord", 'Disable row 2', () => {
row2Enabled = false;
deleteRow2();
});
PluginManager.registerCommand("ODUE_discord", 'Enable row 2', () => {
row2Enabled = true;
setPresence();
});
PluginManager.registerCommand("ODUE_discord", 'Change small picture', args => {
smallPicture = interpretText(String(args.newSmallPicture));
setPresence();
});
PluginManager.registerCommand("ODUE_discord", 'Change small picture text', args => {
smallPictureText = interpretText(String(args.newSmallPictureText));
setPresence();
});
PluginManager.registerCommand("ODUE_discord", 'Toggle RPC', args => {
toggleRPC(args.State === "true", "command");
console.log("state: " + args.State);
console.log("isActivityDisabledFromSettings: " + isActivityDisabledFromSettings);
console.log("isActivityDisabledFromCommand: " + isActivityDisabledFromCommand);
});
}