-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathODUE_discord_MV.js
More file actions
406 lines (366 loc) · 12.8 KB
/
ODUE_discord_MV.js
File metadata and controls
406 lines (366 loc) · 12.8 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
/*:
* @plugindesc (Ver1.2.1) Discord Rich Presence integration to RPG Maker MV.
* @author ODUE
* @url https://github.com/00due/Discord-RPC-for-RMMZ
* @target MV
*
* @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
*
*
* Plugin commands:
*
* rpc_replaceRow1 <text to replace with> - Replaces row 1 (maximum 128 characters)
* rpc_replaceRow2 <text to replace with> - Replaces row 2 (maximum 128 characters)
* rpc_saveRows - Save both rows for later use
* rpc_restore <row1 / row2> – Restores a saved row
* rpc_enable row2 - Enables the second row
* rpc_disable row2 - Disables the second row
*
* Examples:
* // On battle start
* rpc_saveRows
* rpc_replaceRow1 Fighting a monster
* rpc_disable row2
*
* // On battle end
* rpc_restore row1
* rpc_enable row2
*
*
* 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 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 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 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
*/
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 discordParameters = PluginManager.parameters('ODUE_discord_MV');
const appId = discordParameters['Discord application ID'];
if (appId.length < 10) {
console.error("DISCORD ERROR: Invalid Application ID!");
}
let bigPicture = discordParameters['Large picture'];
let bigPictureText = discordParameters['Large picture text'];
let smallPictureEnabled;
smallPictureEnabled = discordParameters['Enable small picture'] === "true";
let smallPicture = discordParameters['Small picture'];
let smallPictureText = discordParameters['Small picture text'];
let firstRow = discordParameters['Row 1'];
let secondRow = discordParameters['Row 2'];
let firstRowSaved;
let secondRowSaved;
let row2Enabled;
row2Enabled = discordParameters['Show row 2'] === "true";
let playtime;
if (discordParameters['Show playtime'] === "true") {
playtime = Date.now();
}
let button1Url;
let button1Text;
let button2Url;
let button2Text;
let buttons = getButtons(discordParameters);
function getButtons(discordParameters) {
if (discordParameters['Enable button 1'] === "true") {
button1Text = discordParameters['Button 1 text'];
button1Url = discordParameters['Button 1 URL'];
let buttonArr = [{ label: button1Text, url: button1Url }];
if (discordParameters['Enable button 2'] === "true") {
button2Text = discordParameters['Button 2 text'];
button2Url = discordParameters['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.");
}
}
let pluginComm = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
pluginComm.call(this, command, args);
if (command === 'rpc_replaceRow1') {
if (args[0] != '') {
combinedArgs = args.join(" ")
replaceRow1(combinedArgs);
}
}
if (command === 'rpc_replaceRow2') {
if (args[0] != '') {
combinedArgs = args.join(" ")
replaceRow2(combinedArgs);
}
}
if (command === 'rpc_saveRows') {
saveRows();
}
if (command === 'rpc_restore') {
switch (args[0]) {
case 'row1': restoreRows(1); break;
case 'row2': restoreRows(2); break;
}
}
if (command === 'rpc_enable') {
if (args[0] === "row2") enableSecondRow();
}
if (command === 'rpc_disable') {
if (args[0] === "row2") disableSecondRow();
}
};
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 () {
let activity = createActivityObject(firstRow, secondRow);
client.request('SET_ACTIVITY', activity);
};
let deleteRow2 = function () {
let activity = createActivityObject(firstRow);
client.request('SET_ACTIVITY', activity);
};
client.on('ready', () => {
if (row2Enabled) setPresence();
else deleteRow2();
console.log("RPC enabled")
});
saveRows = function () {
firstRowSaved = firstRow;
secondRowSaved = secondRow;
};
replaceRow1 = function (newRow) {
let interpretedText = interpretText(newRow);
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.")
};
replaceRow2 = function (newRow) {
let interpretedText = interpretText(newRow);
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 been disabled.")
};
restoreRows = function (rowToRestore) {
switch (rowToRestore) {
case 1:
firstRow = firstRowSaved;
if (row2Enabled) setPresence();
else deleteRow2();
return;
case 2:
secondRow = secondRowSaved;
if (row2Enabled) setPresence();
else deleteRow2();
return;
}
};
disableSecondRow = function() {
row2Enabled = false;
deleteRow2();
};
enableSecondRow = function() {
row2Enabled = true;
setPresence();
};
}