-
Notifications
You must be signed in to change notification settings - Fork 2
/
Discord.js
229 lines (193 loc) · 6.01 KB
/
Discord.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
import { ApplicationCommandOptionType, Client, GatewayIntentBits, PermissionsBitField } from 'discord.js';
import getModuleName from './getModuleName.js';
global.discordClient = (typeof discordClient !== 'undefined') ? discordClient : new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.DirectMessages, GatewayIntentBits.GuildMessages] });
global.discordAdmin = (typeof discordAdmin !== 'undefined') ? discordAdmin : null;
if(!global.discordCommands) {
global.discordCommands = {};
}
if(!global.discordButtons) {
global.discordButtons = {};
}
discordCommands['pm'] = {
moduleName: 'core',
description: 'PM an user',
fn: async(discordClient, interaction) => {
const recipient = interaction.options.getUser('user');
if(!!recipient) {
recipient.send(interaction.options.getString('message'));
}
interaction.reply({
content: 'Sent !',
ephemeral: true
});
},
options: [
{
name: 'user',
type: ApplicationCommandOptionType.User,
description: 'The user to ping',
required: true,
},
{
name: 'message',
type: ApplicationCommandOptionType.String,
description: 'The message to send',
required: true
}
]
};
discordCommands['delete'] = {
moduleName: 'core',
description: 'Delete a specific message',
fn: async(discordClient, interaction) => {
const messageId = interaction.options.getString('message');
const foundMessage = await interaction.channel.messages.fetch(messageId);
console.log('Deleting ', messageId, foundMessage);
if(foundMessage) {
try {
foundMessage.delete();
interaction.reply({ content: 'Done !', ephemeral: true });
} catch(e) {
interaction.reply({ content: e.message, ephemeral: true });
}
}
},
options: [{
name: 'message',
type: ApplicationCommandOptionType.String,
description: 'Message id',
required: true
}]
};
discordCommands['purge'] = {
moduleName: 'core',
description: 'Purge messages following a specific pattern, default: "L.U.M.E.N online - awaiting orders"',
fn: async(discordClient, interaction) => {
try {
let foundMessages = await interaction.channel.messages.fetch({ limit: 100 });
let purgeString = interaction.options.getString('content') || 'L.U.M.E.N online - awaiting orders';
foundMessages = foundMessages.filter((elt) => elt.cleanContent.includes(purgeString));
let msg = `Purging pm - ${foundMessages.size} found`;
console.log(msg);
interaction.reply({ content: msg, ephemeral: true });
if(foundMessages) {
foundMessages.map((elt) => {
try {
elt.delete();
} catch(e) {
interaction.reply({ content: e.message, ephemeral: true });
}
});
}
} catch(e) {
interaction.reply({ content: e.message, ephemeral: true });
}
},
options: [
{
name: 'content',
type: ApplicationCommandOptionType.String,
description: 'Message content to search for',
}
]
};
discordCommands['reboot'] = {
moduleName: 'core',
description: 'Reboot a module',
fn: async(discordClient, interaction) => {
reloadModule(interaction.options.getString('module'));
},
options: [
{
name: 'module',
type: ApplicationCommandOptionType.String,
description: 'Module you want to reboot',
}
]
}
export default class Discord {
/**
* Connect to discord server, and listen messages
*/
static connect(callback) {
discordClient.on('ready', async() => {
log('Connected to discord server bot');
discordClient.user.setActivity('Monitoring the world');
discordClient.users.fetch(config.discord.adminId)
.then(user => {
discordAdmin = user;
callback();
});
});
discordClient.on('messageCreate', (message) => Discord.recievedMessage(message));
discordClient.on('error', console.error);
discordClient.on('interactionCreate', Discord.interactionCreated);
discordClient.login(config.discord.botToken);
return discordClient;
}
static sendMessage(message, callback) {
if(discordAdmin) {
discordAdmin.send(message);
}
if(callback) {
callback();
}
}
static recievedMessage(message) {
const username = (message.channel.recipient) ? message.channel.recipient.username : '???';
log('[Discord] ' + username + ': ' + message.cleanContent);
}
static interactionCreated(interaction) {
if(interaction.channel.id !== config.discord.pmChannel || interaction.user.id !== config.discord.adminId) {
log(`[Discord] Someone tried to execute outside of Elanis/LUMEN channel ! ${interaction.user.id}`)
return;
}
if(interaction.isCommand() && discordCommands[interaction.commandName]) {
try {
discordCommands[interaction.commandName].fn(discordClient, interaction);
} catch(err) {
console.log(err);
}
}
if(interaction.isButton() && discordButtons[interaction.component.customId]) {
try {
discordButtons[interaction.component.customId].fn(discordClient, interaction);
} catch(err) {
console.log(err);
}
}
}
static registerCmd(commandName, fn, commandData = {}) {
const moduleName = getModuleName(1);
if(discordCommands[commandName] && discordCommands[commandName].moduleName !== moduleName) {
log(`Discord command ${commandName} already registered in module ${discordCommands[commandName].moduleName}`, 'error');
return;
}
discordCommands[commandName] = {
moduleName,
fn,
...commandData
};
}
static registerBtn(commandName, fn) {
const moduleName = getModuleName(1);
if(discordButtons[commandName] && discordButtons[commandName].moduleName !== moduleName) {
log(`Discord command ${commandName} already registered in module ${discordButtons[commandName].moduleName}`, 'error');
return;
}
discordButtons[commandName] = {
moduleName,
fn
};
}
static registerCommands() {
const commandsList = Object.keys(discordCommands).map((cmd) => ({
name: cmd,
description: discordCommands[cmd].moduleName + ' - ' + (discordCommands[cmd].description || '?'),
options: discordCommands[cmd].options || [],
dmPermission: true,
defaultMemberPermissions: PermissionsBitField.Flags.Administrator,
}));
const command = discordClient.application?.commands.set(commandsList);
}
}