-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
71 lines (59 loc) · 2.21 KB
/
bot.ts
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
import * as Discord from 'discord.js';
import * as dotenv from 'dotenv';
import * as AWS from 'aws-sdk';
import { ISubscription } from './models';
import BoardRoomApiService from './services/boardRoomApiService';
import localCommands from './commands';
import checkSubscriptions from './checkSubscriptions';
import SubscriptionService from './services/subscriptionService';
AWS.config.update({ region:'us-east-1' });
dotenv.config();
console.log('Bot starting up...');
const bot = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
]
});
const boardroomApi = new BoardRoomApiService();
const subscriptionService = new SubscriptionService();
let subscriptions: ISubscription[] = [];
const commands = localCommands.map(localCommand => {
const { name, description } = localCommand;
const options: Discord.ApplicationCommandOptionData[] = (localCommand.options || []).map(localOption => {
return {
name: localOption.name,
description: localOption.description,
type: localOption.type || 'STRING',
required: typeof localOption.required === 'boolean' ? localOption.required : true,
};
});
const command: Discord.ApplicationCommandData = { name, description, options };
return command;
});
bot.on('ready', async () => {
console.log(`Bot successfully connected as ${bot.user ? bot.user.tag : ''}`);
if (bot.application) {
await bot.application.commands.set(commands);
console.log('Registered commands.');
}
});
bot.on('interactionCreate', async (interaction) => {
if (! interaction.isCommand()) {
return;
}
const command = localCommands.find(command => command.name === interaction.commandName);
if (command) {
console.log(`Executing command: ${interaction.commandName}`);
await command.handler({ interaction, boardroomApi, subscriptionService, subscriptions });
console.log('Ran successfully.');
} else {
console.log(`Could not find command: ${interaction.commandName}`);
}
});
async function main() {
subscriptions = await subscriptionService.list();
bot.login(process.env.BOT_TOKEN);
setInterval(checkSubscriptions, 30000, { client: bot, subscriptions, boardroomApi, subscriptionService });
}
main();