-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyuuto.js
104 lines (86 loc) · 3.02 KB
/
yuuto.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
// Requirement Files
import dotenv from "dotenv";
import { readdirSync, readFileSync } from "fs";
import Discord from "discord.js";
const { Client, Collection } = Discord; // outdated Discord.js node workaround
dotenv.config(); // configures the environment variables
// Collections and Sets
const yuuto = new Client();
yuuto.commands = new Collection();
yuuto.aliases = new Collection();
yuuto.cooldowns = new Collection();
yuuto.once("ready", async () => {
// Load command files
const commandsFiles = readdirSync("./commands/").filter(file =>
file.endsWith(".js")
);
// Load commands
for (const file of commandsFiles) {
// eslint-disable-next-line no-await-in-loop
const { command } = await import(`./commands/${file}`);
yuuto.commands.set(command.name, command);
if (command.aliases && Array.isArray(command.aliases))
command.aliases.forEach(alias => yuuto.aliases.set(alias, command.name));
}
});
// Load the prefix | cannot import from json without node flag at the time of 13.9.0
const { prefix } = JSON.parse(readFileSync("./config.json", "utf-8")) || "!";
// Initialise the bot's startup
yuuto.once("ready", () => {
console.log(`Hi, ${yuuto.user.username} is now online!`);
yuuto.user.setPresence({
status: "online",
game: {
name: "volleyball",
type: "PLAYING"
}
});
});
yuuto.on("message", async message => {
if (message.author.bot || !message.guild) return;
if (!message.content.startsWith(prefix)) return;
// Get the command name. or return
const args = message.content
.slice(prefix.length)
.trim()
.split(/ +/g);
const cmd = args.shift().toLowerCase();
if (!cmd) return;
// Find command or alias
let command = yuuto.commands.get(cmd);
if (!command) command = yuuto.commands.get(yuuto.aliases.get(cmd));
// Add the command to the cooldown
if (!yuuto.cooldowns.has(command.name)) {
yuuto.cooldowns.set(command.name, new Collection());
}
// Set the cooldown's timestamp
const now = Date.now();
const timestamps = yuuto.cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
// Check if the user is on cooldown
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
await message.reply(
`please wait ${timeLeft.toFixed(
1
)} more second(s) before reusing the \`{command.name}\` command.`
);
return;
}
}
// Add the author to the cooldown timestamps,
// then remove the command after cooldown expires.
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
// Execute the command, and catch errors.
try {
command.run(yuuto, message, args);
} catch (error) {
console.error(error);
await message.reply("there was an error trying to execute that command!");
}
});
// login to Discord using the app token
yuuto.login(process.env.TOKEN);