Refactor to more consistent structure (e.g. "plugins")
This commit is contained in:
154
index.ts
154
index.ts
@@ -1,12 +1,3 @@
|
||||
import type {Interaction} from 'discord.js';
|
||||
import {Client, Events, GatewayIntentBits, MessageFlags} from 'discord.js';
|
||||
import type {
|
||||
SlashCommandBuilder,
|
||||
SlashCommandOptionsOnlyBuilder,
|
||||
} from 'discord.js';
|
||||
|
||||
import {sql, GuildSetting, initDb} from './database';
|
||||
|
||||
const BLITZCRANK_BANNER = `
|
||||
****++++++++++*+++
|
||||
**+*+******+********+******+*++*
|
||||
@@ -62,6 +53,29 @@ const BLITZCRANK_BANNER = `
|
||||
"FIRED UP AND READY TO SERVE!"
|
||||
`;
|
||||
|
||||
import type {Interaction} from 'discord.js';
|
||||
import {
|
||||
Client,
|
||||
Collection,
|
||||
Events,
|
||||
GatewayIntentBits,
|
||||
MessageFlags,
|
||||
REST,
|
||||
Routes,
|
||||
} from 'discord.js';
|
||||
import {GuildSetting, initializeDatabase, initializeModels, sequelize, syncModels} from './database';
|
||||
import {readEnvSettings} from './environ';
|
||||
|
||||
import {Command} from './plugin';
|
||||
import { NagPlugin } from './plugins/nag'
|
||||
import { PingPlugin } from './plugins/ping'
|
||||
import { QuotePlugin } from './plugins/quote'
|
||||
import { RemindPlugin } from './plugins/remind'
|
||||
|
||||
|
||||
|
||||
const envSettings = readEnvSettings();
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
GatewayIntentBits.Guilds,
|
||||
@@ -70,84 +84,45 @@ const client = new Client({
|
||||
GatewayIntentBits.MessageContent,
|
||||
],
|
||||
});
|
||||
|
||||
import {Routes} from 'discord.js';
|
||||
import {guildId, appId, token, remindersChannelId} from './config.json';
|
||||
import {REST} from 'discord.js';
|
||||
|
||||
const rest = new REST();
|
||||
rest.setToken(token);
|
||||
rest.setToken(envSettings.BLITZCRANK_API_TOKEN);
|
||||
|
||||
interface Command {
|
||||
data: SlashCommandBuilder | SlashCommandOptionsOnlyBuilder;
|
||||
execute: (interaction: any) => Promise<void>;
|
||||
initialize: (any) => Promise<void>;
|
||||
}
|
||||
|
||||
import {Collection} from 'discord.js';
|
||||
const commands = new Collection<string, Command>();
|
||||
|
||||
import PingCommand from './commands/calendar/ping';
|
||||
import RemindCommand from './commands/calendar/remind';
|
||||
import QuoteCommand from './commands/quotes/quote';
|
||||
import NagCommand from './commands/calendar/nag/nag';
|
||||
import UnnagCommand from './commands/calendar/nag/unnag';
|
||||
import CheckinCommand from './commands/calendar/nag/checkin';
|
||||
const settings = {
|
||||
client: client,
|
||||
database: sequelize,
|
||||
}
|
||||
|
||||
import {Manager} from './commands/calendar/nag/service';
|
||||
const nagManager = new Manager({
|
||||
client: client,
|
||||
db: sql,
|
||||
});
|
||||
nagManager.start();
|
||||
const plugins = [
|
||||
new NagPlugin(settings),
|
||||
new QuotePlugin(settings),
|
||||
new RemindPlugin(settings),
|
||||
new PingPlugin(settings),
|
||||
]
|
||||
|
||||
console.debug(`${remindersChannelId}`);
|
||||
|
||||
commands.set('ping', PingCommand({client: client, db: sql}));
|
||||
commands.set(
|
||||
'remind',
|
||||
RemindCommand({
|
||||
client: client,
|
||||
db: sql,
|
||||
publicChannel: remindersChannelId,
|
||||
responseMode: 'public',
|
||||
}),
|
||||
);
|
||||
commands.set('quote', QuoteCommand({}));
|
||||
commands.set(
|
||||
'nag',
|
||||
NagCommand({
|
||||
client: client,
|
||||
db: sql,
|
||||
}),
|
||||
);
|
||||
commands.set(
|
||||
'unnag',
|
||||
UnnagCommand({
|
||||
client: client,
|
||||
db: sql,
|
||||
}),
|
||||
);
|
||||
commands.set(
|
||||
'checkin',
|
||||
CheckinCommand({
|
||||
client: client,
|
||||
db: sql,
|
||||
}),
|
||||
);
|
||||
|
||||
async function syncCommands() {
|
||||
try {
|
||||
console.log(`Started refreshing slash commands`);
|
||||
const _data = await rest.put(
|
||||
Routes.applicationGuildCommands(appId, guildId),
|
||||
{
|
||||
body: commands.mapValues(cmd => cmd.data.toJSON()),
|
||||
},
|
||||
);
|
||||
console.log(`Successfully reloaded slash commands`);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
async function addApplicationGuildCommands() {
|
||||
for (const plugin of plugins) {
|
||||
for (const command of plugin.commands) {
|
||||
commands.set(command.data.name, command);
|
||||
}
|
||||
}
|
||||
for (const [_guildId, guild] of client.guilds.cache) {
|
||||
try {
|
||||
console.log(`Started refreshing slash commands`);
|
||||
const _data = await rest.put(
|
||||
Routes.applicationGuildCommands(
|
||||
envSettings.BLITZCRANK_APP_ID,
|
||||
guild.id,
|
||||
),
|
||||
{
|
||||
body: commands.mapValues(cmd => cmd.data.toJSON()),
|
||||
},
|
||||
);
|
||||
console.log(`Successfully reloaded slash commands`);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,21 +150,16 @@ client.on(Events.InteractionCreate, async (interaction: Interaction) => {
|
||||
});
|
||||
|
||||
client.once(Events.ClientReady, async readyClient => {
|
||||
await syncCommands();
|
||||
initDb(); // TODO
|
||||
GuildSetting.sync(); // TODO
|
||||
await initializeDatabase();
|
||||
await initializeModels();
|
||||
await syncModels();
|
||||
await addApplicationGuildCommands();
|
||||
|
||||
for (const [_name, cmd] of commands) {
|
||||
await cmd?.initialize({
|
||||
client: client,
|
||||
db: sql,
|
||||
});
|
||||
}
|
||||
// Print banner
|
||||
for (const ln of BLITZCRANK_BANNER.split('\n')) {
|
||||
console.log(ln);
|
||||
}
|
||||
|
||||
console.log(`Logged in as ${readyClient.user.tag}`);
|
||||
});
|
||||
|
||||
client.login(token);
|
||||
client.login(envSettings.BLITZCRANK_API_TOKEN);
|
||||
|
||||
Reference in New Issue
Block a user