2021-01-07 05:34:14 -08:00
|
|
|
/* The Artificer was built in memory of Babka
|
|
|
|
* With love, Ean
|
|
|
|
*
|
|
|
|
* December 21, 2020
|
|
|
|
*/
|
|
|
|
|
2022-05-20 01:43:22 -07:00
|
|
|
import config from './config.ts';
|
|
|
|
import { DEBUG, DEVMODE, LOCALMODE } from './flags.ts';
|
2021-01-07 05:34:14 -08:00
|
|
|
import {
|
2022-05-20 01:43:22 -07:00
|
|
|
botId,
|
|
|
|
cache,
|
|
|
|
DiscordActivityTypes,
|
|
|
|
DiscordenoGuild,
|
|
|
|
DiscordenoMessage,
|
|
|
|
editBotNickname,
|
|
|
|
editBotStatus,
|
|
|
|
initLog,
|
2021-11-21 19:14:57 -08:00
|
|
|
Intents,
|
2022-05-20 01:43:22 -07:00
|
|
|
log,
|
2022-05-04 23:49:06 -07:00
|
|
|
// Log4Deno deps
|
2022-05-20 01:43:22 -07:00
|
|
|
LT,
|
|
|
|
sendMessage,
|
|
|
|
// Discordeno deps
|
|
|
|
startBot,
|
|
|
|
} from './deps.ts';
|
|
|
|
import api from './src/api.ts';
|
|
|
|
import commands from './src/commands/_index.ts';
|
|
|
|
import intervals from './src/intervals.ts';
|
|
|
|
import utils from './src/utils.ts';
|
2021-01-27 00:34:49 -08:00
|
|
|
|
2021-03-14 20:27:53 -07:00
|
|
|
// Initialize logging client with folder to use for logs, needs --allow-write set on Deno startup
|
2022-05-20 01:43:22 -07:00
|
|
|
initLog('logs', DEBUG);
|
2021-03-14 20:27:53 -07:00
|
|
|
|
2021-01-14 05:47:18 -08:00
|
|
|
// Start up the Discord Bot
|
2021-01-07 05:34:14 -08:00
|
|
|
startBot({
|
V1.4.0 Completed
This update adds a handful of new features and readies the API for public use. Detailed changes below:
config.example.ts - Bumped version, added local testing options, added prefill data, changed default logging to false, moved long strings for help and rollhelp to longStrings.ts
db/initialize.ts (previously initDB.ts) - Relocated file for organization, added local options, added new command count and allowed guilds table, created stored procedure for counting commands
db/populateDefaults.ts - Fills in db with default values, adding admin's api key and populating the command count table
flags.ts - Centralized flags for dev/local modes
longStrings.ts - Moved all long string commands to here, contains help, rollhelp, apihelp, info, and privacy commands
mod.ts - Moved flags out into flags.ts, implemented local mode for development, implemented command counting for basic statistics, added info and privacy commands for user help, added more stats to the stats command, added api command, allowing users to allow or block api rolls from happening in their server, reformatted API code, using proper HTTP methods, makes sure api is allowed to roll into chosen guild, added delete endpoint to remove user's data from the database, added endpoint to allow the general public to generate their own api keys
PRIVACY.md - I got bored and wrote a privacy policy, detailing how little data is collected and how the user can have their data removed
README.md - Added new commands to README, updated API documentation, added delete endpoint, updated self hosting details
src/utils.ts - Bumped discordeno version
www/api - Built API website
www/home (previously located in www) - Moved for better organization, minor fixes, updated API details,
2021-02-12 20:26:33 -08:00
|
|
|
token: LOCALMODE ? config.localtoken : config.token,
|
2021-11-21 19:14:57 -08:00
|
|
|
intents: [Intents.GuildMessages, Intents.DirectMessages, Intents.Guilds],
|
2021-01-07 05:34:14 -08:00
|
|
|
eventHandlers: {
|
|
|
|
ready: () => {
|
2022-05-04 23:49:06 -07:00
|
|
|
log(LT.INFO, `${config.name} Logged in!`);
|
2021-11-21 19:14:57 -08:00
|
|
|
editBotStatus({
|
|
|
|
activities: [{
|
2022-05-20 01:43:22 -07:00
|
|
|
name: 'Booting up . . .',
|
2021-11-21 19:14:57 -08:00
|
|
|
type: DiscordActivityTypes.Game,
|
2022-05-20 01:43:22 -07:00
|
|
|
createdAt: new Date().getTime(),
|
2021-11-21 19:14:57 -08:00
|
|
|
}],
|
2022-05-20 01:43:22 -07:00
|
|
|
status: 'online',
|
2021-11-21 19:14:57 -08:00
|
|
|
});
|
2021-02-19 17:44:38 -08:00
|
|
|
|
|
|
|
// Interval to rotate the status text every 30 seconds to show off more commands
|
2021-11-21 19:14:57 -08:00
|
|
|
setInterval(async () => {
|
2022-05-20 01:43:22 -07:00
|
|
|
log(LT.LOG, 'Changing bot status');
|
2021-02-19 17:44:38 -08:00
|
|
|
try {
|
|
|
|
// Wrapped in try-catch due to hard crash possible
|
2021-11-21 19:14:57 -08:00
|
|
|
editBotStatus({
|
|
|
|
activities: [{
|
|
|
|
name: await intervals.getRandomStatus(),
|
|
|
|
type: DiscordActivityTypes.Game,
|
2022-05-20 01:43:22 -07:00
|
|
|
createdAt: new Date().getTime(),
|
2021-11-21 19:14:57 -08:00
|
|
|
}],
|
2022-05-20 01:43:22 -07:00
|
|
|
status: 'online',
|
2021-11-21 19:14:57 -08:00
|
|
|
});
|
2021-03-14 20:27:53 -07:00
|
|
|
} catch (e) {
|
2022-05-04 23:49:06 -07:00
|
|
|
log(LT.ERROR, `Failed to update status: ${JSON.stringify(e)}`);
|
2021-02-19 17:44:38 -08:00
|
|
|
}
|
2021-02-12 22:59:25 -08:00
|
|
|
}, 30000);
|
2021-02-19 17:44:38 -08:00
|
|
|
|
2021-03-13 12:10:35 -08:00
|
|
|
// Interval to update bot list stats every 24 hours
|
2022-05-20 01:43:22 -07:00
|
|
|
LOCALMODE ? log(LT.INFO, 'updateListStatistics not running') : setInterval(() => {
|
|
|
|
log(LT.LOG, 'Updating all bot lists statistics');
|
2021-11-21 19:14:57 -08:00
|
|
|
intervals.updateListStatistics(botId, cache.guilds.size);
|
2021-03-27 21:29:06 -07:00
|
|
|
}, 86400000);
|
2021-03-13 12:10:35 -08:00
|
|
|
|
2021-01-07 11:00:46 -08:00
|
|
|
// setTimeout added to make sure the startup message does not error out
|
2021-01-07 05:34:14 -08:00
|
|
|
setTimeout(() => {
|
2021-11-21 19:14:57 -08:00
|
|
|
LOCALMODE && editBotNickname(config.devServer, `LOCAL - ${config.name}`);
|
2022-05-20 01:43:22 -07:00
|
|
|
LOCALMODE ? log(LT.INFO, 'updateListStatistics not running') : intervals.updateListStatistics(botId, cache.guilds.size);
|
2021-11-21 19:14:57 -08:00
|
|
|
editBotStatus({
|
|
|
|
activities: [{
|
2022-05-20 01:43:22 -07:00
|
|
|
name: 'Booting Complete',
|
2021-11-21 19:14:57 -08:00
|
|
|
type: DiscordActivityTypes.Game,
|
2022-05-20 01:43:22 -07:00
|
|
|
createdAt: new Date().getTime(),
|
2021-11-21 19:14:57 -08:00
|
|
|
}],
|
2022-05-20 01:43:22 -07:00
|
|
|
status: 'online',
|
2021-11-21 19:14:57 -08:00
|
|
|
});
|
2022-05-20 01:43:22 -07:00
|
|
|
sendMessage(config.logChannel, `${config.name} has started, running version ${config.version}.`).catch((e) => {
|
2022-05-04 23:49:06 -07:00
|
|
|
log(LT.ERROR, `Failed to send message: ${JSON.stringify(e)}`);
|
2021-01-07 05:34:14 -08:00
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
},
|
2021-11-21 19:14:57 -08:00
|
|
|
guildCreate: (guild: DiscordenoGuild) => {
|
2022-05-04 23:49:06 -07:00
|
|
|
log(LT.LOG, `Handling joining guild ${JSON.stringify(guild)}`);
|
2022-05-20 01:43:22 -07:00
|
|
|
sendMessage(config.logChannel, `New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`).catch((e) => {
|
2022-05-04 23:49:06 -07:00
|
|
|
log(LT.ERROR, `Failed to send message: ${JSON.stringify(e)}`);
|
2021-01-07 05:34:14 -08:00
|
|
|
});
|
|
|
|
},
|
2021-11-21 19:14:57 -08:00
|
|
|
guildDelete: (guild: DiscordenoGuild) => {
|
2022-05-04 23:49:06 -07:00
|
|
|
log(LT.LOG, `Handling leaving guild ${JSON.stringify(guild)}`);
|
2022-05-20 01:43:22 -07:00
|
|
|
sendMessage(config.logChannel, `I have been removed from: ${guild.name} (id: ${guild.id}).`).catch((e) => {
|
2022-05-04 23:49:06 -07:00
|
|
|
log(LT.ERROR, `Failed to send message: ${JSON.stringify(e)}`);
|
2021-01-07 05:34:14 -08:00
|
|
|
});
|
|
|
|
},
|
2022-05-22 12:29:59 -07:00
|
|
|
debug: DEVMODE ? (dmsg) => log(LT.LOG, `Debug Message | ${JSON.stringify(dmsg)}`) : undefined,
|
2022-05-04 21:57:34 -07:00
|
|
|
messageCreate: (message: DiscordenoMessage) => {
|
2021-01-07 05:34:14 -08:00
|
|
|
// Ignore all other bots
|
2021-11-21 19:14:57 -08:00
|
|
|
if (message.isBot) return;
|
2022-05-20 01:43:22 -07:00
|
|
|
|
2021-01-07 05:34:14 -08:00
|
|
|
// Ignore all messages that are not commands
|
2022-05-13 15:57:45 -07:00
|
|
|
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(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return as we are done handling this command
|
|
|
|
return;
|
|
|
|
}
|
2022-05-20 01:43:22 -07:00
|
|
|
|
2022-05-13 16:25:23 -07:00
|
|
|
log(LT.LOG, `Handling ${config.prefix}command message: ${JSON.stringify(message)}`);
|
2021-01-07 05:34:14 -08:00
|
|
|
|
|
|
|
// Split into standard command + args format
|
2021-03-27 21:29:06 -07:00
|
|
|
const args = message.content.slice(config.prefix.length).trim().split(/[ \n]+/g);
|
2021-01-07 05:34:14 -08:00
|
|
|
const command = args.shift()?.toLowerCase();
|
|
|
|
|
|
|
|
// All commands below here
|
|
|
|
|
2022-05-22 14:30:30 -07:00
|
|
|
switch (command) {
|
|
|
|
case 'ping':
|
|
|
|
// [[ping
|
|
|
|
// Its a ping test, what else do you want.
|
|
|
|
commands.ping(message);
|
|
|
|
break;
|
|
|
|
case 'rip':
|
|
|
|
case 'memory':
|
|
|
|
// [[rip [[memory
|
|
|
|
// Displays a short message I wanted to include
|
|
|
|
commands.rip(message);
|
|
|
|
break;
|
|
|
|
case 'rollhelp':
|
|
|
|
case 'rh':
|
|
|
|
case 'hr':
|
|
|
|
case '??':
|
|
|
|
// [[rollhelp or [[rh or [[hr or [[??
|
|
|
|
// Help command specifically for the roll command
|
|
|
|
commands.rollHelp(message);
|
|
|
|
break;
|
|
|
|
case 'help':
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
// [[help or [[h or [[?
|
|
|
|
// Help command, prints from help file
|
|
|
|
commands.help(message);
|
|
|
|
break;
|
|
|
|
case 'info':
|
|
|
|
case 'i':
|
|
|
|
// [[info or [[i
|
|
|
|
// Info command, prints short desc on bot and some links
|
|
|
|
commands.info(message);
|
|
|
|
break;
|
|
|
|
case 'privacy':
|
|
|
|
// [[privacy
|
|
|
|
// Privacy command, prints short desc on bot's privacy policy
|
|
|
|
commands.privacy(message);
|
|
|
|
break;
|
|
|
|
case 'version':
|
|
|
|
case 'v':
|
|
|
|
// [[version or [[v
|
|
|
|
// Returns version of the bot
|
|
|
|
commands.version(message);
|
|
|
|
break;
|
|
|
|
case 'report':
|
|
|
|
case 'r':
|
|
|
|
// [[report or [[r (command that failed)
|
|
|
|
// Manually report a failed roll
|
|
|
|
commands.report(message, args);
|
|
|
|
break;
|
|
|
|
case 'stats':
|
|
|
|
case 's':
|
|
|
|
// [[stats or [[s
|
|
|
|
// Displays stats on the bot
|
|
|
|
commands.stats(message);
|
|
|
|
break;
|
|
|
|
case 'api':
|
|
|
|
// [[api arg
|
|
|
|
// API sub commands
|
|
|
|
commands.api(message, args);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Non-standard commands
|
|
|
|
if (command?.startsWith('xdy')) {
|
|
|
|
// [[xdydz (aka someone copy pasted the template as a roll)
|
|
|
|
// Help command specifically for the roll command
|
|
|
|
commands.rollHelp(message);
|
|
|
|
} else if (command && (`${command}${args.join('')}`).indexOf(config.postfix) > -1) {
|
|
|
|
// [[roll]]
|
|
|
|
// Dice rolling commence!
|
|
|
|
commands.roll(message, args, command);
|
|
|
|
} else if (command) {
|
|
|
|
// [[emoji or [[emojialias
|
|
|
|
// Check if the unhandled command is an emoji request
|
|
|
|
commands.emoji(message, command);
|
|
|
|
}
|
|
|
|
break;
|
2021-01-18 08:50:22 -08:00
|
|
|
}
|
2022-05-20 01:43:22 -07:00
|
|
|
},
|
|
|
|
},
|
2021-01-07 05:34:14 -08:00
|
|
|
});
|
|
|
|
|
2021-01-07 11:00:46 -08:00
|
|
|
// Start up the command prompt for debug usage
|
|
|
|
if (DEBUG) {
|
2021-11-21 19:14:57 -08:00
|
|
|
utils.cmdPrompt(config.logChannel, config.name);
|
2021-01-07 11:00:46 -08:00
|
|
|
}
|
2021-01-11 00:42:57 -08:00
|
|
|
|
|
|
|
// Start up the API for rolling from third party apps (like excel macros)
|
|
|
|
if (config.api.enable) {
|
2022-05-04 21:57:34 -07:00
|
|
|
api.start();
|
2021-01-11 00:42:57 -08:00
|
|
|
}
|