-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyuuto.js
More file actions
144 lines (119 loc) · 4.13 KB
/
yuuto.js
File metadata and controls
144 lines (119 loc) · 4.13 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
// Requirement Files
import dotenv from "dotenv";
import { readdirSync } from "fs";
import { Client, Collection } from "@yuuto-project/discord.js";
import Command from "./commands/base/Command.js";
import "./settings/axiosSettings.js";
dotenv.config(); // configures the environment variables
export const prefix = process.env.PREFIX || "!";
// Collections and Sets
const yuuto = new Client({
ws: {
// Read more https://discord.js.org/#/docs/main/stable/class/Intents?scrollTo=s-FLAGS
intents: [
"GUILDS",
"GUILD_MEMBERS",
"GUILD_MESSAGES",
"GUILD_MESSAGE_REACTIONS"
]
},
// We need to fetch all members to make sure that finderUtil works properly
fetchAllMembers: true
});
yuuto.commands = new Collection();
yuuto.aliases = new Collection();
yuuto.cooldowns = new Collection();
(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 { default: CommandConstructor } = await import(`./commands/${file}`);
// Checks if we actually have a command here, we don't want to register classes that are not commands
if (!(CommandConstructor.prototype instanceof Command)) {
console.error(
`ERROR: command ${CommandConstructor.name} does not extend command base class, ignoring it`
);
continue;
}
const command = new CommandConstructor();
yuuto.commands.set(command.name, command);
if (command.aliases && Array.isArray(command.aliases)) {
command.aliases.forEach(alias => yuuto.aliases.set(alias, command.name));
}
}
})().catch(console.error);
// Initialise the bot's startup
yuuto.once("ready", () => {
console.log(`Hi, ${yuuto.user.username} is now online!`);
const presenceFn = () => {
console.debug("Setting presence");
yuuto.user.setPresence({
status: "online",
activity: {
name: "volleyball",
type: "PLAYING"
}
});
};
// This math stuff is one hour in milliseconds
// This interval makes sure that Yuuto keeps playing his games
setInterval(presenceFn, 1000 * 60 * 60);
presenceFn();
});
yuuto.on("message", async message => {
if (message.author.bot || !message.guild) return;
if (!message.content.startsWith(prefix)) return;
if (process.env.BOTCMDS && message.channel.id !== process.env.BOTCMDS) return; // bot channel check
// 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 && yuuto.aliases.has(cmd)) {
command = yuuto.commands.get(yuuto.aliases.get(cmd));
}
// If we still didn't find a command, ignore it
if (!command) return;
// 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;
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 {
await command.run(yuuto, message, args);
} catch (error) {
console.error(error);
message.reply("there was an error trying to execute that command!");
}
});
// login to Discord using the app token
yuuto.login(process.env.TOKEN).catch(console.error);