-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (80 loc) · 3.2 KB
/
Copy pathindex.js
File metadata and controls
91 lines (80 loc) · 3.2 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
const { Client, Collection, Events, GatewayIntentBits, MessageFlags, Partials } = require('discord.js');
const vote_create_command = require('./commands/vote-create.js');
const vote_manage_command = require('./commands/vote-manage.js');
const vote_settings_command = require('./commands/vote-settings.js');
const vote_archive_command = require('./commands/vote-archive.js');
const { init_backend } = require('./scripts/setup.js');
const {
create_guild_setting,
remove_guild_config,
purge_guild_scheduler,
schedule_guild_votes
} = require('./scripts/backend.js');
const { handle_embed_click } = require('./scripts/voting.js');
const { handle_message } = require('./scripts/registration.js');
const bot_token = process.env.BOT_TOKEN;
const [active_votes, settings, schedule] = init_backend();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions
]
});
client.commands = new Collection();
client.commands.set(vote_create_command.data.name, vote_create_command);
client.commands.set(vote_manage_command.data.name, vote_manage_command);
client.commands.set(vote_settings_command.data.name, vote_settings_command);
client.commands.set(vote_archive_command.data.name, vote_archive_command);
client.active_votes = active_votes;
client.vote_settings = settings;
client.schedule = schedule;
client.once(Events.ClientReady, async client => {
for (let guild_id in client.active_votes) {
schedule_guild_votes(client, guild_id);
}
console.log(`Ready! Logged in as ${client.user.tag}`);
});
client.on(Events.InteractionCreate, async interaction => {
if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral
});
}
}
}
else if (interaction.isButton() && interaction.customId.startsWith('embed_')) {
const equal = interaction.customId.indexOf('=');
const button_type = interaction.customId.slice(0, equal);
const vote = interaction.customId.slice(equal + 1);
handle_embed_click(vote, button_type, interaction);
}
});
client.on(Events.MessageCreate, async message => {
handle_message(message, client);
});
client.on(Events.GuildCreate, async guild => {
create_guild_setting(client.vote_settings, guild.id);
client.schedule[guild.id] = {};
});
client.on(Events.GuildDelete, async guild => {
remove_guild_config(client, guild.id);
purge_guild_scheduler(client, guild.id);
});
client.login(bot_token);