Rework eventHandlers to have their own files
This commit is contained in:
parent
8caec2a8f8
commit
1447e8c0e4
|
@ -1,6 +1,6 @@
|
|||
export const config = {
|
||||
'name': 'Sweeper Bot', // Name of the bot
|
||||
'version': '0.0.0', // Version of the bot
|
||||
'version': '0.1.0', // Version of the bot
|
||||
'token': 'the_bot_token', // Discord API Token for this bot
|
||||
'localtoken': 'local_testing_token', // Discord API Token for a secondary OPTIONAL testing bot, THIS MUST BE DIFFERENT FROM "token"
|
||||
'prefix': 's!', // Prefix for all commands
|
||||
|
|
2
deps.ts
2
deps.ts
|
@ -6,7 +6,7 @@ export const botId = getBotIdFromToken(LOCALMODE ? config.localtoken : config.to
|
|||
|
||||
export { ActivityTypes, createBot, editBotNickname, editBotStatus, Intents, sendMessage, startBot } from 'https://deno.land/x/discordeno@13.0.0/mod.ts';
|
||||
|
||||
export type { Bot, CreateMessage, EventHandlers, Message } from 'https://deno.land/x/discordeno@13.0.0/mod.ts';
|
||||
export type { Bot, CreateMessage, EventHandlers, Guild, Message } from 'https://deno.land/x/discordeno@13.0.0/mod.ts';
|
||||
|
||||
export { Client } from 'https://deno.land/x/mysql@v2.10.2/mod.ts';
|
||||
|
||||
|
|
2
mod.ts
2
mod.ts
|
@ -14,7 +14,7 @@ import {
|
|||
createBot,
|
||||
} from './deps.ts';
|
||||
// import { dbClient, ignoreList } from './src/db.ts';
|
||||
import { events } from './src/eventHandlers.ts';
|
||||
import { events } from './src/events.ts';
|
||||
|
||||
// Initialize logging client with folder to use for logs, needs --allow-write set on Deno startup
|
||||
initLog('logs', DEBUG);
|
||||
|
|
|
@ -1,191 +0,0 @@
|
|||
import config from '../config.ts';
|
||||
import { DEVMODE, LOCALMODE } from '../flags.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
ActivityTypes,
|
||||
botId,
|
||||
editBotNickname,
|
||||
editBotStatus,
|
||||
EventHandlers,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// Discordeno deps
|
||||
sendMessage,
|
||||
} from '../deps.ts';
|
||||
// import { dbClient, ignoreList } from './src/db.ts';
|
||||
import commands from './commands/_index.ts';
|
||||
import { getRandomSweeperLine, successColor, warnColor } from './commandUtils.ts';
|
||||
import utils from './utils.ts';
|
||||
|
||||
export const events: Partial<EventHandlers> = {};
|
||||
events.ready = (bot) => {
|
||||
log(LT.INFO, `${config.name} Logged in!`);
|
||||
editBotStatus(bot, {
|
||||
activities: [{
|
||||
name: 'Booting up . . .',
|
||||
type: ActivityTypes.Game,
|
||||
createdAt: new Date().getTime(),
|
||||
}],
|
||||
status: 'online',
|
||||
});
|
||||
|
||||
// Interval to rotate the status text every 30 seconds to show off more commands
|
||||
setInterval(async () => {
|
||||
log(LT.LOG, 'Changing bot status');
|
||||
try {
|
||||
// Wrapped in try-catch due to hard crash possible
|
||||
editBotStatus(bot, {
|
||||
activities: [{
|
||||
name: getRandomSweeperLine(),
|
||||
type: ActivityTypes.Game,
|
||||
createdAt: new Date().getTime(),
|
||||
}],
|
||||
status: 'online',
|
||||
});
|
||||
} catch (e) {
|
||||
log(LT.ERROR, `Failed to update status: ${utils.jsonStringifyBig(e)}`);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
// setTimeout added to make sure the startup message does not error out
|
||||
setTimeout(() => {
|
||||
LOCALMODE && editBotNickname(bot, config.devServer, { nick: `LOCAL - ${config.name}` });
|
||||
editBotStatus(bot, {
|
||||
activities: [{
|
||||
name: 'Booting Complete',
|
||||
type: ActivityTypes.Game,
|
||||
createdAt: new Date().getTime(),
|
||||
}],
|
||||
status: 'online',
|
||||
});
|
||||
sendMessage(bot, config.logChannel, {
|
||||
embeds: [{
|
||||
title: `${config.name} is now Online`,
|
||||
color: successColor,
|
||||
fields: [
|
||||
{
|
||||
name: 'Version:',
|
||||
value: `${config.version}`,
|
||||
inline: true,
|
||||
},
|
||||
],
|
||||
}],
|
||||
}).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:88', 'Startup', e));
|
||||
}, 1000);
|
||||
};
|
||||
|
||||
events.guildCreate = (bot, guild) => {
|
||||
log(LT.LOG, `Handling joining guild ${utils.jsonStringifyBig(guild)}`);
|
||||
sendMessage(bot, config.logChannel, {
|
||||
embeds: [{
|
||||
title: 'New Guild Joined!',
|
||||
color: successColor,
|
||||
fields: [
|
||||
{
|
||||
name: 'Name:',
|
||||
value: `${guild.name}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Id:',
|
||||
value: `${guild.id}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Member Count:',
|
||||
value: `${guild.memberCount}`,
|
||||
inline: true,
|
||||
},
|
||||
],
|
||||
}],
|
||||
}).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:95', 'Join Guild', e));
|
||||
};
|
||||
|
||||
events.guildDelete = (bot, guildId) => {
|
||||
log(LT.LOG, `Handling leaving guild ${utils.jsonStringifyBig(guildId)}`);
|
||||
sendMessage(bot, config.logChannel, {
|
||||
embeds: [{
|
||||
title: 'Removed from Guild',
|
||||
color: warnColor,
|
||||
fields: [
|
||||
{
|
||||
name: 'Id:',
|
||||
value: `${guildId}`,
|
||||
inline: true,
|
||||
},
|
||||
],
|
||||
}],
|
||||
}).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:99', 'Leave Guild', e));
|
||||
};
|
||||
|
||||
if (DEVMODE) {
|
||||
events.debug = (dmsg) => log(LT.LOG, `Debug Message | ${utils.jsonStringifyBig(dmsg)}`);
|
||||
}
|
||||
|
||||
events.messageCreate = (bot, message) => {
|
||||
// Ignore all other bots
|
||||
if (message.isFromBot) return;
|
||||
|
||||
// Ignore all messages that are not commands
|
||||
if (message.content.indexOf(config.prefix) !== 0) {
|
||||
// Handle @bot messages
|
||||
if (message.mentionedUserIds[0] === botId && (message.content.trim().startsWith(`<@${botId}>`) || message.content.trim().startsWith(`<@!${botId}>`))) {
|
||||
commands.handleMentions(bot, message);
|
||||
}
|
||||
|
||||
// return as we are done handling this command
|
||||
return;
|
||||
}
|
||||
|
||||
log(LT.LOG, `Handling ${config.prefix}command message: ${utils.jsonStringifyBig(message)}`);
|
||||
|
||||
// Split into standard command + args format
|
||||
const args = message.content.slice(config.prefix.length).trim().split(/[ \n]+/g);
|
||||
const command = args.shift()?.toLowerCase();
|
||||
|
||||
// All commands below here
|
||||
switch (command) {
|
||||
case 'ping':
|
||||
// s!ping
|
||||
// Its a ping test, what else do you want.
|
||||
commands.ping(bot, message);
|
||||
break;
|
||||
case 'help':
|
||||
case 'h':
|
||||
case '?':
|
||||
// s!help or s!h or s!?
|
||||
// Help command, prints from help file
|
||||
commands.help(bot, message);
|
||||
break;
|
||||
case 'info':
|
||||
case 'i':
|
||||
// s!info or s!i
|
||||
// Info command, prints short desc on bot and some links
|
||||
commands.info(bot, message);
|
||||
break;
|
||||
case 'version':
|
||||
case 'v':
|
||||
// s!version or s!v
|
||||
// Returns version of the bot
|
||||
commands.version(bot, message);
|
||||
break;
|
||||
case 'report':
|
||||
case 'r':
|
||||
// s!report or s!r (command that failed)
|
||||
// Manually report a failed roll
|
||||
commands.report(bot, message, args);
|
||||
break;
|
||||
case 'sm':
|
||||
// s!sm [channelId]
|
||||
// Manually sends a message thru the bot
|
||||
if (message.authorId === config.ownerId) {
|
||||
commands.sendMessage(bot, message, args);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// Non-standard commands
|
||||
console.log(`${command} WIP`);
|
||||
break;
|
||||
}
|
||||
};
|
|
@ -0,0 +1,17 @@
|
|||
import { DEVMODE } from '../flags.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
EventHandlers,
|
||||
} from '../deps.ts';
|
||||
import eventHandlers from './events/_index.ts';
|
||||
|
||||
export const events: Partial<EventHandlers> = {};
|
||||
|
||||
events.ready = eventHandlers.ready;
|
||||
events.guildCreate = eventHandlers.guildCreate;
|
||||
events.guildDelete = eventHandlers.guildDelete;
|
||||
events.messageCreate = eventHandlers.messageCreate;
|
||||
|
||||
if (DEVMODE) {
|
||||
events.debug = eventHandlers.debug
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
import { ready } from './ready.ts';
|
||||
import { guildCreate } from './guildCreate.ts';
|
||||
import { guildDelete } from './guildDelete.ts';
|
||||
import { debug } from './debug.ts';
|
||||
import { messageCreate } from './messageCreate.ts';
|
||||
|
||||
export default {
|
||||
ready,
|
||||
guildCreate,
|
||||
guildDelete,
|
||||
debug,
|
||||
messageCreate,
|
||||
};
|
|
@ -0,0 +1,8 @@
|
|||
import {
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
} from '../../deps.ts';
|
||||
import utils from '../utils.ts';
|
||||
|
||||
export const debug = (dmsg: string) => log(LT.LOG, `Debug Message | ${utils.jsonStringifyBig(dmsg)}`);
|
|
@ -0,0 +1,40 @@
|
|||
import config from '../../config.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
Bot,
|
||||
Guild,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// Discordeno deps
|
||||
sendMessage,
|
||||
} from '../../deps.ts';
|
||||
import { successColor } from '../commandUtils.ts';
|
||||
import utils from '../utils.ts';
|
||||
|
||||
export const guildCreate = (bot: Bot, guild: Guild) => {
|
||||
log(LT.LOG, `Handling joining guild ${utils.jsonStringifyBig(guild)}`);
|
||||
sendMessage(bot, config.logChannel, {
|
||||
embeds: [{
|
||||
title: 'New Guild Joined!',
|
||||
color: successColor,
|
||||
fields: [
|
||||
{
|
||||
name: 'Name:',
|
||||
value: `${guild.name}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Id:',
|
||||
value: `${guild.id}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Member Count:',
|
||||
value: `${guild.memberCount}`,
|
||||
inline: true,
|
||||
},
|
||||
],
|
||||
}],
|
||||
}).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:95', 'Join Guild', e));
|
||||
};
|
|
@ -0,0 +1,29 @@
|
|||
import config from '../../config.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
Bot,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// Discordeno deps
|
||||
sendMessage,
|
||||
} from '../../deps.ts';
|
||||
import { warnColor } from '../commandUtils.ts';
|
||||
import utils from '../utils.ts';
|
||||
|
||||
export const guildDelete = (bot: Bot, guildId: bigint) => {
|
||||
log(LT.LOG, `Handling leaving guild ${utils.jsonStringifyBig(guildId)}`);
|
||||
sendMessage(bot, config.logChannel, {
|
||||
embeds: [{
|
||||
title: 'Removed from Guild',
|
||||
color: warnColor,
|
||||
fields: [
|
||||
{
|
||||
name: 'Id:',
|
||||
value: `${guildId}`,
|
||||
inline: true,
|
||||
},
|
||||
],
|
||||
}],
|
||||
}).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:99', 'Leave Guild', e));
|
||||
};
|
|
@ -0,0 +1,79 @@
|
|||
import config from '../../config.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
Bot,
|
||||
Message,
|
||||
botId,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
} from '../../deps.ts';
|
||||
import commands from '../commands/_index.ts';
|
||||
import utils from '../utils.ts';
|
||||
|
||||
export const messageCreate = (bot: Bot, message: Message) => {
|
||||
// Ignore all other bots
|
||||
if (message.isFromBot) return;
|
||||
|
||||
// Ignore all messages that are not commands
|
||||
if (message.content.indexOf(config.prefix) !== 0) {
|
||||
// Handle @bot messages
|
||||
if (message.mentionedUserIds[0] === botId && (message.content.trim().startsWith(`<@${botId}>`) || message.content.trim().startsWith(`<@!${botId}>`))) {
|
||||
commands.handleMentions(bot, message);
|
||||
}
|
||||
|
||||
// return as we are done handling this command
|
||||
return;
|
||||
}
|
||||
|
||||
log(LT.LOG, `Handling ${config.prefix}command message: ${utils.jsonStringifyBig(message)}`);
|
||||
|
||||
// Split into standard command + args format
|
||||
const args = message.content.slice(config.prefix.length).trim().split(/[ \n]+/g);
|
||||
const command = args.shift()?.toLowerCase();
|
||||
|
||||
// All commands below here
|
||||
switch (command) {
|
||||
case 'ping':
|
||||
// s!ping
|
||||
// Its a ping test, what else do you want.
|
||||
commands.ping(bot, message);
|
||||
break;
|
||||
case 'help':
|
||||
case 'h':
|
||||
case '?':
|
||||
// s!help or s!h or s!?
|
||||
// Help command, prints from help file
|
||||
commands.help(bot, message);
|
||||
break;
|
||||
case 'info':
|
||||
case 'i':
|
||||
// s!info or s!i
|
||||
// Info command, prints short desc on bot and some links
|
||||
commands.info(bot, message);
|
||||
break;
|
||||
case 'version':
|
||||
case 'v':
|
||||
// s!version or s!v
|
||||
// Returns version of the bot
|
||||
commands.version(bot, message);
|
||||
break;
|
||||
case 'report':
|
||||
case 'r':
|
||||
// s!report or s!r (command that failed)
|
||||
// Manually report a failed roll
|
||||
commands.report(bot, message, args);
|
||||
break;
|
||||
case 'sm':
|
||||
// s!sm [channelId]
|
||||
// Manually sends a message thru the bot
|
||||
if (message.authorId === config.ownerId) {
|
||||
commands.sendMessage(bot, message, args);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// Non-standard commands
|
||||
console.log(`${command} WIP`);
|
||||
break;
|
||||
}
|
||||
};
|
|
@ -0,0 +1,72 @@
|
|||
import config from '../../config.ts';
|
||||
import { LOCALMODE } from '../../flags.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
Bot,
|
||||
ActivityTypes,
|
||||
editBotNickname,
|
||||
editBotStatus,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// Discordeno deps
|
||||
sendMessage,
|
||||
} from '../../deps.ts';
|
||||
import { getRandomSweeperLine, successColor } from '../commandUtils.ts';
|
||||
import utils from '../utils.ts';
|
||||
|
||||
export const ready = (bot: Bot) => {
|
||||
log(LT.INFO, `${config.name} Logged in!`);
|
||||
editBotStatus(bot, {
|
||||
activities: [{
|
||||
name: 'Booting up . . .',
|
||||
type: ActivityTypes.Game,
|
||||
createdAt: new Date().getTime(),
|
||||
}],
|
||||
status: 'online',
|
||||
});
|
||||
|
||||
// Interval to rotate the status text every 30 seconds to show off more commands
|
||||
setInterval(async () => {
|
||||
log(LT.LOG, 'Changing bot status');
|
||||
try {
|
||||
// Wrapped in try-catch due to hard crash possible
|
||||
editBotStatus(bot, {
|
||||
activities: [{
|
||||
name: getRandomSweeperLine(),
|
||||
type: ActivityTypes.Game,
|
||||
createdAt: new Date().getTime(),
|
||||
}],
|
||||
status: 'online',
|
||||
});
|
||||
} catch (e) {
|
||||
log(LT.ERROR, `Failed to update status: ${utils.jsonStringifyBig(e)}`);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
// setTimeout added to make sure the startup message does not error out
|
||||
setTimeout(() => {
|
||||
LOCALMODE && editBotNickname(bot, config.devServer, { nick: `LOCAL - ${config.name}` });
|
||||
editBotStatus(bot, {
|
||||
activities: [{
|
||||
name: 'Booting Complete',
|
||||
type: ActivityTypes.Game,
|
||||
createdAt: new Date().getTime(),
|
||||
}],
|
||||
status: 'online',
|
||||
});
|
||||
sendMessage(bot, config.logChannel, {
|
||||
embeds: [{
|
||||
title: `${config.name} is now Online`,
|
||||
color: successColor,
|
||||
fields: [
|
||||
{
|
||||
name: 'Version:',
|
||||
value: `${config.version}`,
|
||||
inline: true,
|
||||
},
|
||||
],
|
||||
}],
|
||||
}).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:88', 'Startup', e));
|
||||
}, 1000);
|
||||
};
|
Loading…
Reference in New Issue