-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbot.js
418 lines (399 loc) · 14.8 KB
/
bot.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
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
// noinspection DuplicatedCode,JSCheckFunctionSignatures
// noinspection JSUnusedAssignment
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////
Importing packages and credentials | | | | | | | | | |
| | | | | | | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
require("dotenv").config();
const token = process.env.BOTOKEN;
const logdnakey = process.env.LOGDNAKEY;
const topGGAuth = process.env.TOPGG_AUTH;
const cronitorID = process.env.CRONITORID;
const topGGToken = process.env.TOPGG_TOKEN;
/////////////////////////////////////////////////////////
const logdna = require("logdna");
const options = {
app: "muser",
level: "debug",
};
const logger = logdna.createLogger(logdnakey, options);
logger.log("Hello world!", "info");
const cronitor = require("cronitor")(cronitorID);
const monitor = new cronitor.Monitor("Muser");
const userinfo = require("./models/user.js");
/////////////////////////////////////////////////////////
const fs = require("fs");
const db = require("quick.db");
const Discord = require("discord.js");
const { Client, Collection, Intents, DiscordAPIError } = require("discord.js");
const { SoundCloudPlugin } = require("@distube/soundcloud");
/////////////////////////////////////////////////////////
let timeouts = new Map();
logger.info("All packages imported. Credentials set.");
console.log("All packages imported. Credentials set.");
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////
| | | | | | | | | | All the ugly constants related to DJS | | | | | | | | | | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
const client = new Client({
allowedMentions: {
parse: ["users", "roles"],
repliedUser: true,
},
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_WEBHOOKS,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.GUILD_VOICE_STATES,
],
});
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////
The code
that makes voting work
/////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
/*/////////////////////////////////////////////////////////////
| | | | | | | | | | Custom Functions | | | | | | | | | | | |
*/ /////////////////////////////////////////////////////////////
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////
This is
(supposed to be) the command parser
/////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
client.scommands = new Collection();
const commandFiles = fs
.readdirSync("./scommands")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./scommands/${file}`);
client.scommands.set(command.data.name, command);
}
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////
DisTube stuff
/////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
const { DisTube } = require("distube");
const { SpotifyPlugin } = require("@distube/spotify");
const distube = new DisTube(client, {
searchSongs: 10,
emitNewSongOnly: true,
leaveOnEmpty: true,
leaveOnStop: true,
plugins: [new SpotifyPlugin(), new SoundCloudPlugin()],
});
client.distube = distube;
// noinspection JSUnresolvedVariable
client.on("voiceStateUpdate", async (oldState, newState) => {
const queue = await client.distube.getQueue(oldState);
if (!queue) return;
const channel = await queue.textChannel;
const premium = await db.get(`PREMIUM_${oldState.guild.id}`);
if (premium === "yes") return;
if (
oldState.channelID !== oldState.guild.me.voice.channelID ||
newState.channel
)
return;
if (oldState.channel.members.size > 1) {
if (timeouts.get(oldState.guild.id)) {
clearTimeout(timeouts.get(oldState.guild.id));
timeouts.delete(oldState.guild.id);
}
} else {
if (!timeouts.get(oldState.guild.id)) {
// check if there is already a timeout
timeouts.set(
oldState.guild.id,
setTimeout(async () => {
const embed = new Discord.MessageEmbed()
.setTitle(`Channel empty`)
.setDescription(
`If you have premium, then I won't leave the VC, but if you don't then I will because no one is there in the VC. [Check out Premium for just 2.99$](https://muser.pgamerx.com/premium)`
)
.setColor("RED");
await channel.send({ embeds: [embed] });
await queue.stop();
timeouts.delete(oldState.guild.id);
}, 120000)
);
}
}
});
/*
client.on('voiceStateUpdate', async (oldState, newState) => {
if (oldState.channelID !== oldState.guild.me.voice.channelID ||
newState.channel) return; const premium_status = await
db.get(`PREMIUM_${oldState.guild.id}`) if(premium_status == "yes") return
console.log(`Premium guild boii`) if (!oldState.channel.members.size - 1)
console.log("Not premium, leaving")
setTimeout(() => {
if (!oldState.channel.members.size - 1)
oldState.disconnect();
console.log("leaving")
}, 120000);
});
////LMAO CODE VERSIONING BE LIKE
// ^ Peipr adding comments be like ~PG
*/
const mongoose = require("mongoose");
const dbURI = process.env.DBURI;
// noinspection JSCheckFunctionSignatures
mongoose
.connect(dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("owo did it work");
});
// noinspection JSCheckFunctionSignatures
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////
Startup function
/////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
client.once("ready", () => {
client.shard.fetchClientValues("guilds.cache.size").then((results) => {
client.shard
.broadcastEval((c) =>
c.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0)
)
.then((results2) => {
// logger.info("New bot instance: complete");
monitor.ping({ message: `New instance`, state: "complete" });
client.user.setPresence({
activities: [
{
name: `with ${results2.reduce(
(acc, memberCount) => acc + memberCount,
0
)} users | /help`,
},
],
status: "online",
});
setInterval(() => {
client.user.setPresence({
activities: [
{
name: `music in ${results.reduce(
(acc, guildCount) => acc + guildCount,
0
)} servers | /help`,
},
],
status: "online",
});
}, 1800000);
});
});
});
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////
When the
bot discovers a new message
/////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
client.on("messageCreate", async (message) => {
if (message.content.startsWith(".eval")) {
const owners_id = [
"587663056046391302" /*PGamerX*/,
"707512325740953690" /*Luckie*/,
"460511909435932672" /*Peipr*/,
];
if (!owners_id.includes(message.author.id)) return;
const args2 = message.content.split(" ").slice(1);
const clean = (text) => {
if (typeof text === "string")
return text
.replace(/`/g, "`" + String.fromCharCode(8203))
.replace(/@/g, "@" + String.fromCharCode(8203));
else return text;
};
try {
const code = args2.join(" ");
let evaled = eval(code);
// noinspection JSUnresolvedFunction
message.channel.send(`\`\`\`js\n${clean(evaled)}\`\`\``);
// noinspection JSUnresolvedFunction
message.channel.send(clean(evaled), { code: "xl" });
} catch (err) {
// noinspection JSUnresolvedFunction
message.channel.send(`\`ERROR\` \`\`\`xl\n${clean(err)}\n\`\`\``);
}
}
if (message.author.bot) return;
if (
message.mentions.has(client.user) &&
message.mentions.everyone === false
) {
const embed = new Discord.MessageEmbed().setTitle(
`Only usable through Slash commands`
).setDescription(`
Hey there, I have just been completely renovated to be compatible with slash commands.
> Cannot see my slash commands? or getting \`Invalid interaction\`?
Please ask a server administrator to re-invite me using [This Link](https://discord.com/oauth2/authorize?client_id=763418289689985035&scope=+applications.commands+bot&permissions=37084480) so I can get permission to create slash commands in this server.
`);
message.reply({ embeds: [embed] });
}
});
// const status = (queue) => `Volume: \`${queue.volume}%\` | Filter:
// \`${queue.filters || "Off"}\` | Loop: \`${queue.repeatMode ? queue.repeatMode
// == 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ?
// "On" : "Off"}\``;
// noinspection JSUnresolvedVariable
client.distube
.on("playSong", (queue, song) => {
const embed = new Discord.MessageEmbed()
.setTitle(`Playing: ${song.name} - ${song.formattedDuration}`)
.setDescription(
`
Filter : ${queue.filter || "Off"}
Loop : ${
queue.repeatMode
? queue.repeatMode === 2
? "All Queue"
: "This Song"
: "Off"
}
Autoplay : ${queue.autoplay ? "On" : "Off"}
Volume : ${queue.volume}`
)
.addField(
`Requested by`,
`${song.member.user.username}#${song.member.user.discriminator}`
)
.setColor("RED")
.setThumbnail(song.thumbnail);
queue.textChannel.send({ embeds: [embed] });
})
.on("addSong", (queue, song) => {
const embed = new Discord.MessageEmbed()
.setTitle(`Added : ${song.name} - ${song.formattedDuration}`)
.setDescription(
`
Filter : ${queue.filter || "Off"}
Loop : ${
queue.repeatMode
? queue.repeatMode === 2
? "All Queue"
: "This Song"
: "Off"
}
Autoplay : ${queue.autoplay ? "On" : "Off"}
Volume : ${queue.volume}`
)
.addField(
`Requested by`,
`${song.member.user.username}#${song.member.user.discriminator}`
)
.setColor("RED")
.setThumbnail(song.thumbnail);
queue.textChannel.send({ embeds: [embed] });
})
.on("playList", (queue, playlist, song) => {
const embed = new Discord.MessageEmbed()
.setTitle(
`Playlist Playing: ${playlist.name} - ${playlist.songs.length} songs`
)
.setDescription(
`
Filter : ${queue.filter || "Off"}
Loop : ${
queue.repeatMode
? queue.repeatMode === 2
? "All Queue"
: "This Song"
: "Off"
}
Autoplay : ${queue.autoplay ? "On" : "Off"}
Volume : ${queue.volume}`
)
.addField(
`Requested by`,
`${song.member.user.username}#${song.member.user.discriminator}`
)
.setColor("RED")
.setThumbnail(song.thumbnail);
queue.textChannel.send({ embeds: [embed] });
})
.on("addList", (queue, playlist) => {
const embed = new Discord.MessageEmbed()
.setTitle(
`Playlist Added : ${playlist.name} - ${playlist.songs.length} songs`
)
.setDescription(
`
Filter : ${queue.filter || "Off"}
Loop : ${
queue.repeatMode
? queue.repeatMode === 2
? "All Queue"
: "This Song"
: "Off"
}
Autoplay : ${queue.autoplay ? "On" : "Off"}
Volume : ${queue.volume}`
)
.addField(
`Requested by`,
`${playlist.member.user.username}#${playlist.member.user.discriminator}`
)
.setColor("RED")
.setThumbnail(playlist.thumbnail);
queue.textChannel.send({ embeds: [embed] });
})
// DisTubeOptions.searchSongs = true
.on("searchResult", (result) => {
let i = 0;
// noinspection JSUnresolvedVariable
queue.textChannel.send(
`**Choose an option from below**\n${result
.map(
(song) => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``
)
.join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`
);
})
// DisTubeOptions.searchSongs = true
.on("searchCancel", (message) => queue.textChannel.send(`Searching canceled`))
.on("error", (channel, error) =>
channel.send(` An error encountered: ${error}`)
)
.on("empty", (queue) => {
const embed = new Discord.MessageEmbed()
.setTitle(`Channel empty`)
.setDescription(
`If you have premium, then I won't leave the VC, but if you don't then I will because no one is there in the VC. [Check out Premium](https://muser.pgamerx.com/premium)`
)
.setColor("RED");
queue.textChannel.send({ embeds: [embed] });
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isCommand()) {
try {
// noinspection JSUnresolvedVariable
monitor.ping({
message: `Interaction name: ${interaction.commandName} \n Requested by: ${interaction.member} \n Requested in guild: ${interaction.guildId}`,
state: "run",
series: interaction.id,
});
// noinspection JSUnresolvedVariable
await client.scommands.get(interaction.commandName).execute(interaction);
let intr = JSON.stringify(interaction);
// noinspection JSUnresolvedVariable
monitor.ping({
message: `Interaction successful: ${interaction.commandName} \n Requested by: ${interaction.member} \n Raw JSON: ${intr}`,
state: "complete",
series: interaction.id,
});
} catch (error) {
console.error(error);
let intr = JSON.stringify(interaction);
// noinspection JSUnresolvedVariable
monitor.ping({
message: `Interaction ${interaction.commandName} failed \n Error: ${error} \n Raw JSON: ${intr}`,
state: "fail",
series: interaction.id,
});
// noinspection JSUnresolvedVariable
logger.error(
`Interaction ${interaction.commandName} failed \n Error: ${error} \n Raw JSON: ${intr}`
);
}
}
});
client.login(token);