Simp-O-Matic

Dumb Discord bot in TS.
git clone git://git.knutsen.co/Simp-O-Matic
Log | Files | Refs | README | LICENSE

commit 7a6609f5b218bfeda23c70fa61feed7699f2a516
parent e640beb9d5c8c868577e3c6ac9e12accc52c690c
Author: Demonstrandum <moi@knutsen.co>
Date:   Mon, 23 Mar 2020 03:37:12 +0000

Deal with guild initialisation.

Diffstat:
Mlib/default.ts | 1+
Mlib/extensions.ts | 1+
Mlib/main.ts | 39++++++++++++++++++++++++++++++++-------
3 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/lib/default.ts b/lib/default.ts @@ -3,6 +3,7 @@ /// laid out. All fields are accounted for here. const DEFAULT_GUILD_CONFIG : ConfigType = { + main_channel: null, system_channel: null, pp_sizes: { '541761315887120399': 16 diff --git a/lib/extensions.ts b/lib/extensions.ts @@ -21,6 +21,7 @@ declare global { }; type ConfigType = { + main_channel: string, system_channel: string, pp_sizes: { [key: string]: number } weather_locations: { [key: string]: string }, diff --git a/lib/main.ts b/lib/main.ts @@ -26,6 +26,7 @@ import DEFAULT_GUILD_CONFIG from './default'; import { pastebin_latest, pastebin_update, pastebin_url } from './api/pastebin'; +import { Guild } from 'discord.js'; // Anything that hasn't been defined in `bot.json` // will be taken care of by the defaults. @@ -92,20 +93,44 @@ export class SimpOMatic { private static _CLIENT : Client; private _COMMAND_HISTORY : Message[] = []; + static init_guild(guild: Guild) { + const guild_id = guild.id; + GLOBAL_CONFIG.guilds[guild_id] = deep_copy(DEFAULT_GUILD_CONFIG); + } + static start() { this._CLIENT = new Client(); this._CLIENT.login( SECRETS.api.token, `${__dirname}/*Discord.ts` - ).then(() => { - console.log('Bot logged in.') - setTimeout(() => - system_message(this._CLIENT, "**We're back online baby!**"), - 2000); - }); + ).then(() => console.log('Bot logged in.')); + this._CLIENT.on('ready', () => this.events()); + return this._CLIENT; } + static events() { + const client = this._CLIENT; + system_message(client, "**We're back online baby!**"); + client.on('guildCreate', guild => { + if (!GLOBAL_CONFIG.guilds.hasOwnProperty(guild.id)) + this.init_guild(guild); + // TODO: + // Maybe try and find a channel to try message in (a main channel)? + // Ask them to set a main channel and system channel via commands. + // Ask them to read !help and !commands and !aliases. + // etc. + }); + client.on('guildMemberAdd', member => { + const guild_id = member.guild.id; + // TODO: Say hello to a member in main channel. + }); + client.on('guildMemberRemove', member => { + const guild_id = member.guild.id; + // TODO: Say goodbye to a member in main channel. + }); + } + expand_alias(operator: string, args: string[], message: Message) { const CONFIG = GLOBAL_CONFIG.guilds[message.guild.id]; @@ -367,7 +392,7 @@ export class SimpOMatic { // Initialise completely new Guilds. if (!GLOBAL_CONFIG.guilds.hasOwnProperty(guild_id)) - GLOBAL_CONFIG.guilds[guild_id] = deep_copy(DEFAULT_GUILD_CONFIG); + SimpOMatic.init_guild(message.guild); const CONFIG = GLOBAL_CONFIG.guilds[guild_id]; // Ignore empty messages...