-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
241 lines (213 loc) · 7.42 KB
/
Copy pathindex.js
File metadata and controls
241 lines (213 loc) · 7.42 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
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
230
231
232
233
234
235
236
237
238
239
240
241
/**
* Wright State University eSports
* Discord Bot
*
* ===========================
* Contributors:
* @author Joshua Quaintance
* ===========================
*
* A general purpose discord bot created to
* simplify tedious jobs around the discord server
* including:
* - Member Sign-up
* - Auto-role
* - Announcements
* - Message edit/delete logging
* - In-Discord logging
* - *Sheets webhook integration
* - *Fun commands for users
*
* __Anything with (*) is not implemented yet__
*
* The bot uses the discord.js module to interact with
* the discord API
*
* ===========================
*
* Guidelines:
* - All events from discord should be handled in this file
* - All functions should be in their own module
* - All functions should be imported into the main index.js file
* - All functions should be documented with JSDoc
*
* This `index.js` file is the main entry point for the bot
* and handles all the events and interactions, but it does not
* have the actual functions themselves as they will be imported
* as modules.
*
* If any events are added, it should be added here
* as a centralized location and any functions that will handle
* the events should be imported from their respective modules.
*/
import 'dotenv/config'; // env variables
import { Events, GatewayIntentBits, Client, Partials } from 'discord.js';
// Interactions modules
import {
approveMember,
approveGuest,
cancelApproval,
onMessageEdit,
onMessageDelete,
signUpForm
} from './interactions/index.js';
import { addRestrictions, initiateApprovalEmbed } from './utils/new-member.js';
// Utilities
import logger from './utils/loggers/logger.js';
import loadCommands from './utils/loadCommands.js';
import token from './accessToken.js';
// Introductions
logger.info('==============================');
logger.info(' WRIGHT STATE ESPORTS ');
logger.info(' DISCORD BOT ');
logger.info('==============================');
logger.info('Booting up...!');
logger.info('Creating client...');
const TOKEN = process.env.DISCORD_TOKEN;
const startTime = new Date();
/**
* The client is the main entry point for the bot
*
* Since the big update discord did to the API, we need to tell the
* API what we want to have access to and essentially what the bot
* will be doing. This is done through the intents and partials
*/
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildWebhooks,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildMessageReactions
],
partials: [Partials.Channel, Partials.Message, Partials.Reaction]
});
await token.initToken();
// loads all the commands to discord's rest api
logger.info('Loading commands into client...');
await loadCommands(client);
logger.info('Attaching event listeners...');
client.once(Events.ClientReady, async (ready) => {
logger.info(
`Successfully logged in as ${ready.user.tag}. \n\nStarted up in ${Date.now() - startTime}ms`
);
});
/**
* Removes a reactions from a specific user.
* This is a really bad implementation because it had no comments and targets specific users while exposing their personal userID.
* UserIDs were removed and code commented out to restore server functionality to users.
*/
/*
client.on(Events.MessageReactionAdd, async (reaction, user) => {
try {
// Ignore reactions from bots
if (user.bot) return;
if (reaction?.partial) {
await reaction.fetch();
}
if (reaction?.emoji?.name === '🐝') {
await reaction.remove();
logger.info(`Removed bee reaction from <@${user.id}>`);
return;
}
const not_banned = ['✈️', '🗼', '✅', '❌', '🫃'];
if (user.id === '00000' && !not_banned.includes(reaction.emoji.name)) {
await reaction.remove();
logger.info(`Removed ${reaction.emoji.name} reaction from <@${user.id}>`);
}
} catch (err) {
logger.error(err, 'Error handling MessageReactionAdd');
}
});
*/
/**
* When there is a message, we will try to check if it's in a specific channel
* and if it's a webhook
*/
client.on(Events.MessageCreate, initiateApprovalEmbed);
/**
* ! When a message is sent before the bot is turned on, the bot doesn't have prior knowledge
* ! of the message and therefore will have missing data
* When a message is edited, if it's not a webhook or a bot, we will log the original message
* and the updated message
*/
client.on(Events.MessageUpdate, onMessageEdit);
/**
* When a message is deleted, if it's not a webhook or a bot, we will log the message
* and the channel it was deleted in
*/
client.on(Events.MessageDelete, onMessageDelete);
/**
* When any interaction is detected
* @param {Interaction} interaction - The interaction that was created
*/
client.on(Events.InteractionCreate, async (interaction) => {
try {
// If the interaction is from a bot, which we can check by seeing if the member has the bot role
// which ONLY a bot can have, we will ignore it
if (interaction?.member?.roles.botRole) return;
if (interaction.isChatInputCommand()) {
const name = interaction.commandName;
const command = interaction.client.commands.get(name);
if (!command) {
await interaction.reply(`No command named \`${name}\``);
logger.info(
`<@${interaction.user.id}> tried to use \`${name}\` command, but it doesn't exist`
);
return;
}
await command.execute(interaction);
logger.info(
`<@${interaction.user.id}> (${interaction.user.username}) used \`${name}\` command ` +
`in <#${interaction.channel.id}> (${interaction.channel.name})`
);
} else if (interaction.isButton()) {
switch (interaction.component.customId) {
case 'approveMember':
approveMember(interaction);
break;
case 'approveGuest':
approveGuest(interaction);
break;
case 'cancelApproval':
cancelApproval(interaction);
break;
case 'sign-up-form':
signUpForm(interaction);
break;
}
}
} catch (err) {
logger.error(err, 'Error handling interaction');
}
});
/**
* When someone joins the server
*/
client.on(Events.GuildMemberAdd, async (member) => {
try {
logger.section.START();
logger.info(`New member detected! ${member.displayName}`);
addRestrictions(member);
logger.info('Successfully added permissions on categories');
logger.section.END();
} catch (err) {
logger.error(err, 'Unexpected error on member join!');
}
});
/**
* When someone leaves the server (voluntarily or non-voluntarily)
*/
client.on(Events.GuildMemberRemove, async () => {});
/**
* When a member of the server has something updated about them
* nickname, roles, etc...
*/
client.on(Events.GuildMemberUpdate, async (_old) => {});
logger.info('Logging in...');
client.login(TOKEN);
logger.info('Attaching client to logger...');
logger._client = client;