-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
205 lines (170 loc) · 5.51 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
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
import fs from 'fs';
import path from 'path';
import _ from 'lodash';
import Discord, { GatewayIntentBits, Routes } from 'discord.js';
import { REST } from '@discordjs/rest';
import { ChannelType } from 'discord-api-types/v10';
import {fileURLToPath} from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
console.log('ChurchyBot starting');
const MODULES_DIR = path.join(__dirname, 'modules');
const LOG_CHANNEL = 'developers-developers';
async function main() {
// check for valid environment
const TOKEN = process.env.CHURCHYBOT_DISCORD_TOKEN;
if (!TOKEN) {
console.error('CHURCHYBOT_DISCORD_TOKEN env var is not set up, terminating');
process.exit(1);
}
let started = false;
let devChannel;
const devLog = msg => {
if (devChannel) {
devChannel.send({ content: msg });
}
};
const client = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildEmojisAndStickers,
GatewayIntentBits.GuildIntegrations,
GatewayIntentBits.GuildWebhooks,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.MessageContent,
],
});
// load modules
const dir = await fs.promises.opendir(MODULES_DIR);
const modules = [];
const commands = [];
const interactions = {};
for await (const entry of dir) {
console.log('>> Loading module:', entry.name);
const module = await import(path.join(__dirname, 'modules', entry.name));
if (!_.isFunction(module.default)) {
console.error('Invalid module!', entry.name, 'does not have a function as the default export');
console.error('Modules should look something like:');
console.error(' export default (client) => { /* do stuff */ }');
process.exit(2);
}
await module.default(client);
// single command export mode
if (module.command) {
if (module.execute) {
interactions[module.command.name] = module.execute;
} else {
console.error(`Module ${entry.name} has a slash command but no execute() handler!`);
process.exit(3);
}
commands.push(module.command);
}
// multiple command export mode
if (module.commands) {
_.each(module.commands, ({ command, execute }) => {
interactions[command.name] = execute;
commands.push(command);
});
}
}
// register commands with REST API
// Dave's test server and test bot
const testGuildId = '398645287926628362';
const testClientId = '805262898943229963';
const realClientId = '655917018339475485';
const rest = new REST({ version: '10' }).setToken(TOKEN);
try {
await rest.put(
Routes.applicationGuildCommands(testClientId, testGuildId),
{
body: _.map(commands, command => command.toJSON()),
},
);
console.log('Test commands updated');
} catch (error) {
if (error.code === 20012) {
console.log('Not authorized to update test commands');
} else {
console.error('Unable to PUT test commands');
console.error(error);
process.exit(4);
}
}
try {
await rest.put(
Routes.applicationCommands(realClientId),
{
body: _.map(commands, command => command.toJSON()),
},
);
console.log('Prod commands updated');
} catch (error) {
if (error.code === 20012) {
console.log('Not authorized to update prod commands');
} else {
console.error('Unable to PUT prod commands');
console.error(error);
process.exit(5);
}
}
// set up interaction handler
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) {
return;
}
console.log(`Received interaction ${interaction.commandName}`);
const execute = interactions[interaction.commandName];
if (execute) {
await execute(interaction);
} else {
await interaction.reply('Unknown interaction received. Might be a slash command in testing, might be a crazy error');
}
});
// set up discord events
client.on('ready', async () => {
console.log('Discord client ready');
const guild = client.guilds.cache.first();
devChannel = guild.channels.cache.find(c => c.name === LOG_CHANNEL);
// join all threads
const response = await guild.channels.fetchActiveThreads();
guild.channels.cache.each(channel => {
if (channel.type === ChannelType.PublicThread) {
if (channel.joinable) {
console.log('Joining thread', channel.name);
channel.join();
}
}
});
if (!started) {
devLog('Bot started');
started = true;
}
});
client.on('threadCreate', thread => {
if (thread.joinable) {
console.log('Joining thread', thread.name);
thread.join();
}
});
client.on('error', error => {
console.error('Discord client error', error);
devLog(`Client error: ${error.message}`);
});
client.on('warn', warning => {
console.error('Discord client warning', warning);
devLog(`Client warning: ${warning.message}`);
});
client.on('reconnecting', () => console.log('Discord client reconnecting'));
client.on('resume', () => console.log('Discord client resume'));
client.on('rateLimit', rateLimitInfo => {
console.log('Discord client encountered a rate limit');
console.log(rateLimitInfo);
});
// actually log the bot in
console.log('Discord client login');
client.login(TOKEN);
}
main();