-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
141 lines (122 loc) · 4.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
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
const BOT_VERSION = '2.1.1';
// Load some necessary files
const fs = require('fs');
const {
Client, Collection, Events, GatewayIntentBits,
} = require('discord.js');
const Config = require('./config.json');
const Constants = require('./constants/index.json');
const Logger = require('./services/logger');
const Message = require('./services/message');
const CustomStatus = require('./services/customStatus');
// Commands
const commandFiles = fs.readdirSync('./commands').filter((file) => file.endsWith('.js'));
// Basic initialisation
const intents = [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildVoiceStates,
];
const client = new Client({ intents });
client.commands = new Collection();
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
// Events handlers
client.once(Events.ClientReady, async () => {
// Initialize commands
await Promise.all(client.commands.map(async (command) => {
if (typeof command.onBotReady === 'undefined') return;
Logger.verbose(`Initializing "${command.name}" command...`);
await command.onBotReady(client);
}));
// set a random custom status
Logger.verbose('Initializing random status...');
await CustomStatus.initRandomStatusJob(client);
Logger.info(`Osmose Utility Bot is ready ! Version : ${BOT_VERSION}`);
});
client.on(Events.InteractionCreate, async (interaction) => {
// Do some checks depending on the interaction type
let command = null;
if (interaction.isCommand()) {
command = client.commands.get(interaction.commandName);
} else if (interaction.isButton()) {
const commandName = interaction.customId.split('-')[0];
command = client.commands.get(commandName);
}
if (!command) {
return;
}
// run the command
try {
if (interaction.isChatInputCommand()) {
Logger.info(`Executing "${command.name}" command from ${interaction.user.username} (${interaction.user.id})`);
await command.execute(interaction);
} else if (interaction.isButton()) {
Logger.info(`Executing "${command.name}" button interaction from ${interaction.user.username} (${interaction.user.id})`);
await command.buttonExecute(interaction);
}
} catch (error) {
Logger.error(error);
await Message.errorReply(interaction, {
title: Constants.UNKNOWN_ERROR_TITLE,
description: Constants.UNKNOWN_ERROR_DESCRIPTION,
});
}
});
client.on(Events.VoiceStateUpdate, (oldState, newState) => {
Logger.verbose('voiceStateUpdate event detected !');
client.commands.each((command) => {
if (typeof command.onVoiceStateUpdate === 'undefined') return;
Logger.verbose(`Triggering onVoiceStateUpdate on ${command.name}`);
command.onVoiceStateUpdate(client, oldState, newState);
});
});
client.on(Events.GuildMemberAdd, async (member) => {
await Promise.all(client.commands.map(async (command) => {
if (typeof command.onGuildMemberAdd === 'undefined') return;
Logger.verbose(`Triggering onGuildMemberAdd on ${command.name}`);
await command.onGuildMemberAdd(client, member);
}));
});
client.on(Events.GuildMemberRemove, async (member) => {
await Promise.all(client.commands.map(async (command) => {
if (typeof command.onGuildMemberRemove === 'undefined') return;
Logger.verbose(`Triggering guildMemberRemove on ${command.name}`);
await command.onGuildMemberRemove(client, member);
}));
});
client.on(Events.GuildMemberUpdate, (oldMember, newMember) => {
client.commands.each((command) => {
if (typeof command.onGuildMemberUpdate === 'undefined') return;
Logger.verbose(`Triggering onGuildMemberUpdate on ${command.name}`);
command.onGuildMemberUpdate(client, oldMember, newMember);
});
});
client.on(Events.UserUpdate, (oldUser, newUser) => {
client.commands.each((command) => {
if (typeof command.onUserUpdate === 'undefined') return;
Logger.verbose(`Triggering userUpdate on ${command.name}`);
command.onUserUpdate(client, oldUser, newUser);
});
});
client.on(Events.InviteCreate, (invite) => {
client.commands.each((command) => {
if (typeof command.onInviteCreate === 'undefined') return;
Logger.verbose(`Triggering inviteCreate on ${command.name}`);
command.onInviteCreate(client, invite);
});
});
client.on(Events.InviteDelete, (invite) => {
client.commands.each((command) => {
if (typeof command.onInviteDelete === 'undefined') return;
Logger.verbose(`Triggering inviteDelete on ${command.name}`);
command.onInviteDelete(client, invite);
});
});
client.on(Events.Error, Logger.error);
client.login(Config.token);