forked from MrAugu/simple-discordjs-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (67 loc) · 2.67 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
/*
> Index.Js is the entry point of our application.
*/
// We import the modules.
const Discord = require("discord.js");
const mongoose = require("mongoose");
const config = require("./config");
const GuildSettings = require("./models/settings");
const Dashboard = require("./dashboard/dashboard");
// We instiate the client and connect to database.
const client = new Discord.Client({
ws: {
intents: [
"GUILDS",
"GUILD_MEMBERS",
"GUILD_MESSAGES"
]
}
});
mongoose.connect(config.mongodbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
});
client.config = config;
// We listen for client's ready event.
client.on("ready", async () => {
console.log("Fetching members...");
for (const [id, guild] of client.guilds.cache) {
await guild.members.fetch();
}
console.log("Fetched members.");
console.log(`Bot is ready. (${client.guilds.cache.size} Guilds - ${client.channels.cache.size} Channels - ${client.users.cache.size} Users)`);
Dashboard(client);
});
// We listen for message events.
client.on("message", async (message) => {
// Declaring a reply function for easier replies - we grab all arguments provided into the function and we pass them to message.channel.send function.
const reply = (...arguments) => message.channel.send(...arguments);
// Doing some basic command logic.
if (message.author.bot) return;
if (!message.channel.permissionsFor(message.guild.me).has("SEND_MESSAGES")) return;
// Retriving the guild settings from database.
var storedSettings = await GuildSettings.findOne({ gid: message.guild.id });
if (!storedSettings) {
// If there are no settings stored for this guild, we create them and try to retrive them again.
const newSettings = new GuildSettings({
gid: message.guild.id
});
await newSettings.save().catch(()=>{});
storedSettings = await GuildSettings.findOne({ gid: message.guild.id });
}
// If the message does not start with the prefix stored in database, we ignore the message.
if (message.content.indexOf(storedSettings.prefix) !== 0) return;
// We remove the prefix from the message and process the arguments.
const args = message.content.slice(storedSettings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// If command is ping we send a sample and then edit it with the latency.
if (command === "ping") {
const roundtripMessage = await reply("Pong!");
return roundtripMessage.edit(`*${roundtripMessage.createdTimestamp - message.createdTimestamp}ms*`);
}
});
// Listening for error & warn events.
client.on("error", console.error);
client.on("warn", console.warn);
// We login into the bot.
client.login(config.token);