commit 50307a31d1a212e118e08ad26a52c60a95d2f798
parent f4edc7eaa87f7d298c6babd366a2521e5c50a486
Author: Demonstrandum <moi@knutsen.co>
Date: Mon, 23 Mar 2020 01:40:48 +0000
Support for system messages.
Diffstat:
4 files changed, 78 insertions(+), 12 deletions(-)
diff --git a/lib/commands/system.ts b/lib/commands/system.ts
@@ -0,0 +1,32 @@
+import { help_info } from '../utils';
+const sys_channel = (channel: string) =>
+ (channel)
+ ? `is set to ${channel}.`
+ : `has not been set.`;
+
+export default (home_scope: HomeScope) => {
+ const { message, args, CONFIG } = home_scope;
+ const { uptime } = message.client;
+
+ if (args.length === 0 || args[0] === 'status') {
+ let msg = `**Bot up and running.**\n`;
+ msg += `${uptime} milliseconds since last re-boot.\n`;
+ msg += `system-information channel `;
+ msg += sys_channel(CONFIG.system_channel);
+ msg += `\nSee \`${CONFIG.commands.prefix}help system\` for more information.`;
+ message.reply(msg);
+ return;
+ }
+
+ if (args[0] === 'channel') {
+ const { channels } = message.mentions;
+ if (channels.size === 0)
+ return message.reply('System-information channel '
+ + sys_channel(CONFIG.system_channel));
+ CONFIG.system_channel = channels.first().toString();
+ return message.reply(
+ `System-information channel set to ${CONFIG.system_channel}.`);
+ }
+
+ message.reply(help_info('system', CONFIG.commands.prefix));
+};
diff --git a/lib/default.ts b/lib/default.ts
@@ -3,10 +3,10 @@
/// laid out. All fields are accounted for here.
const DEFAULT_GUILD_CONFIG : ConfigType = {
+ system_channel: null,
pp_sizes: {
'541761315887120399': 16
},
-
weather_locations: {
'541761315887120399': 'Moscow'
},
@@ -62,11 +62,15 @@ const DEFAULT_GUILD_CONFIG : ConfigType = {
{
match: "/(^|[^\\p{L}\\p{N}])+bots?([^\\p{L}\\p{N}]|$)+/iu",
response: "The hell you sayn' about bots?"
+ },
+ {
+ match: "/Good (Morning|Day) (Star|Sun)shine/i",
+ response: "The Earth says Hello!"
}
],
reject: [
{
- match: "/\\.{20,}/",
+ match: "/\\.{30,}/",
response: "Too many dots..."
},
],
@@ -75,8 +79,17 @@ const DEFAULT_GUILD_CONFIG : ConfigType = {
match: "/tbh/i",
response: 'desu'
},
+ {
+ match: "/Yahweh/i",
+ response: "Adonai"
+ }
+ ],
+ trigger: [
+ {
+ match: "/is the bot down/i",
+ response: "uptime"
+ }
],
- trigger: [],
// Blacklist (initially everyone can do everything,
// except for those listed specifically on this list).
blacklist: {
diff --git a/lib/extensions.ts b/lib/extensions.ts
@@ -21,6 +21,7 @@ declare global {
};
type ConfigType = {
+ system_channel: string,
pp_sizes: { [key: string]: number }
weather_locations: { [key: string]: string },
commands: {
diff --git a/lib/main.ts b/lib/main.ts
@@ -23,7 +23,6 @@ import { deep_merge, pp, compile_match,
import DEFAULT_GUILD_CONFIG from './default';
// API specific modules.
-import web_search from './api/google';
import { pastebin_latest,
pastebin_update,
pastebin_url } from './api/pastebin';
@@ -77,6 +76,17 @@ console.log('File/Execution locations:', {
'process.cwd()': process.cwd()
});
+const system_message = (client: Client, msg: string) => {
+ for (const guild in GLOBAL_CONFIG.guilds)
+ if (GLOBAL_CONFIG.guilds.hasOwnProperty(guild)
+ && GLOBAL_CONFIG.guilds[guild].system_channel
+ && client !== null)
+ client.channels
+ .fetch(GLOBAL_CONFIG.guilds[guild].system_channel)
+ .then((c: TextChannel) =>
+ c.send(msg));
+};
+
@Discord
export class SimpOMatic {
private static _CLIENT : Client;
@@ -90,6 +100,8 @@ export class SimpOMatic {
);
console.log('Secrets:', pp(SECRETS));
console.log('Known commands:', pp(KNOWN_COMMANDS));
+ system_message(this._CLIENT, 'We\'re back online baby!');
+ return this._CLIENT;
}
expand_alias(operator: string, args: string[], message: Message) {
@@ -396,11 +408,18 @@ export class SimpOMatic {
}
}
-function on_termination() {
+let CLIENT: Client = null;
+
+function on_termination(error_type) {
// Back-up the resultant CONFIG to an external file.
+ console.warn(`Received ${error_type}, shutting down.`);
console.log('Cleaning up...');
write_file(`${process.cwd()}/export-exit.json`, export_config(GLOBAL_CONFIG, {}));
pastebin_update(export_config(GLOBAL_CONFIG, {}));
+ // Message all system channels.
+ system_message(CLIENT,
+ `Bot got \`${error_type}\` signal.\n`
+ + `**Shutting down...**`);
// Make sure we saved ok.
setTimeout(() => {
console.log('Clean finished.');
@@ -409,12 +428,13 @@ function on_termination() {
}
// Handle exits.
-process.on('exit', on_termination);
-process.on('SIGINT', on_termination);
-process.on('SIGTERM', on_termination);
-process.on('SIGUSR1', on_termination);
-process.on('SIGUSR2', on_termination);
-process.on('uncaughtException', on_termination);
+process
+ .on('SIGTERM', on_termination.bind(this, 'SIGTERM'))
+ .on('SIGINT', on_termination.bind(this, 'SIGINT'))
+ .on('exit', on_termination.bind(this, 'exit'))
+ .on('SIGUSR1', on_termination.bind(this, 'SIGUSR1'))
+ .on('SIGUSR2', on_termination.bind(this, 'SIGUSR2'))
+ .on('uncaughtException', on_termination.bind(this, 'exception'));
// CONFIG will eventually update to the online version.
pastebin_latest().then(res => {
@@ -431,6 +451,6 @@ pastebin_latest().then(res => {
.mut_map(compile_match));
// Start The Simp'O'Matic.
- SimpOMatic.start();
+ CLIENT = SimpOMatic.start();
}).catch(console.warn);