From 652cc2e5efed6ddef593570ee90634cbc1c452bb Mon Sep 17 00:00:00 2001 From: Max Isom Date: Mon, 9 Mar 2020 15:00:18 -0500 Subject: [PATCH] Add config command --- src/commands/config.ts | 55 ++++++++++++++++++++++++++++++++++++------ src/index.ts | 19 +++++++++++---- src/interfaces.ts | 4 +-- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/src/commands/config.ts b/src/commands/config.ts index fb79059..80d5727 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -1,22 +1,61 @@ +import {TextChannel} from 'discord.js'; import {CommandHandler} from '../interfaces'; +import {Settings} from '../models'; const config: CommandHandler = { name: 'config', description: 'Change various bot settings.', - execute: (msg, args) => { + execute: async (msg, args) => { + if (args.length === 0) { + // Show current settings + const settings = await Settings.findByPk(msg.guild!.id); + + if (settings) { + let response = `prefix: \`${settings.prefix}\`\n`; + response += `channel: ${msg.guild!.channels.cache.get(settings.channel)!.toString()}`; + + await msg.channel.send(response); + } + + return; + } + const setting = args[0]; - switch (setting) { - case 'prefix': - msg.channel.send('Prefix set'); - break; + if (args.length !== 2) { + await msg.channel.send('🚫 incorrect number of arguments'); + return; + } - case 'channel': - msg.channel.send('Channel bound'); + switch (setting) { + case 'prefix': { + const newPrefix = args[1]; + + await Settings.update({prefix: newPrefix}, {where: {guildId: msg.guild!.id}}); + + await msg.channel.send(`👍 prefix updated to \`${newPrefix}\``); break; + } + + case 'channel': { + const channel = msg.guild!.channels.cache.find(c => c.name === args[1]); + + if (channel && channel.type === 'text') { + await Settings.update({channel: channel.id}, {where: {guildId: msg.guild!.id}}); + + await Promise.all([ + (channel as TextChannel).send('hey apparently I\'m bound to this channel now'), + msg.react('👍') + ]); + } else { + await msg.channel.send('🚫 either that channel doesn\'t exist or you want me to become sentient and listen to a voice channel'); + } + + break; + } default: - msg.channel.send('Unknown setting'); + await msg.channel.send('🚫 I\'ve never met this setting in my life'); } } }; diff --git a/src/index.ts b/src/index.ts index 5771988..6681d45 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,12 +3,11 @@ import path from 'path'; import makeDir from 'make-dir'; import Discord from 'discord.js'; import {DISCORD_TOKEN, DISCORD_CLIENT_ID, DATA_DIR} from './utils/config'; +import {Settings} from './models'; import {sequelize} from './utils/db'; import {CommandHandler} from './interfaces'; import handleGuildCreate from './events/guild-create'; -const PREFIX = '!'; - const client = new Discord.Client(); const commands = new Discord.Collection(); @@ -22,12 +21,22 @@ for (const file of commandFiles) { } // Generic message handler -client.on('message', (msg: Discord.Message) => { - if (!msg.content.startsWith(PREFIX) || msg.author.bot) { +client.on('message', async (msg: Discord.Message) => { + // Get guild settings + const settings = await Settings.findByPk(msg.guild!.id); + + if (!settings) { + // Got into a bad state, send owner welcome message + return client.emit('guildCreate', msg.guild); + } + + const {prefix, channel} = settings; + + if (!msg.content.startsWith(prefix) || msg.author.bot || msg.channel.id !== channel) { return; } - const args = msg.content.slice(PREFIX.length).split(/ +/); + const args = msg.content.slice(prefix.length).split(/ +/); const command = args.shift()!.toLowerCase(); if (!commands.has(command)) { diff --git a/src/interfaces.ts b/src/interfaces.ts index b002a65..6909d47 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,7 +1,7 @@ -import Discord from 'discord.js'; +import {Message} from 'discord.js'; export interface CommandHandler { name: string; description: string; - execute: (msg: Discord.Message, args: string[]) => void; + execute: (msg: Message, args: string[]) => void; }