diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-06-21 15:33:36 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-06-21 15:33:36 -0400 |
commit | 6eb42974bdd4da4f9a6d77c8fde4c19f9f0a351b (patch) | |
tree | 6b0490f7f17d5d663f0f764589328e8acb79dd22 /src | |
parent | 5c3da90f441c321f55ae735d6002f4da91f2481e (diff) | |
download | tanzanite-6eb42974bdd4da4f9a6d77c8fde4c19f9f0a351b.tar.gz tanzanite-6eb42974bdd4da4f9a6d77c8fde4c19f9f0a351b.tar.bz2 tanzanite-6eb42974bdd4da4f9a6d77c8fde4c19f9f0a351b.zip |
fix(db): made it work now
Diffstat (limited to 'src')
29 files changed, 248 insertions, 80 deletions
diff --git a/src/commands/config/prefix.ts b/src/commands/config/prefix.ts index 3bb717b..1326426 100644 --- a/src/commands/config/prefix.ts +++ b/src/commands/config/prefix.ts @@ -1,7 +1,6 @@ -import { Guild as DiscordGuild, Message } from 'discord.js'; -import { SlashCommandOption } from '../../lib/extensions/BushClientUtil'; +import { Message } from 'discord.js'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; import { Guild } from '../../lib/models'; export default class PrefixCommand extends BushCommand { @@ -11,7 +10,8 @@ export default class PrefixCommand extends BushCommand { category: 'config', args: [ { - id: 'prefix' + id: 'prefix', + type: 'string' } ], userPermissions: ['MANAGE_GUILD'], @@ -32,39 +32,18 @@ export default class PrefixCommand extends BushCommand { }); } - async changePrefix(guild: DiscordGuild, prefix?: string): Promise<void> { - let row = await Guild.findByPk(guild.id); + async exec(message: Message | BushSlashMessage, { prefix }: { prefix?: string }): Promise<void> { + let row = await Guild.findByPk(message.guild.id); if (!row) { row = Guild.build({ - id: guild.id + id: message.guild.id }); } - // this.client.console.debug(row); + await row.update({ prefix: prefix || this.client.config.prefix }); if (prefix) { - row.prefix = prefix; - await row.save(); + await message.util.send(`${this.client.util.emojis.success} changed prefix from \`${prefix}\``); } else { - const row = await Guild.findByPk(guild.id); - row.prefix = this.client.config.prefix; - await row.save(); - } - } - - async exec(message: Message, { prefix }: { prefix?: string }): Promise<void> { - await this.changePrefix(message.guild, prefix); - if (prefix) { - await message.util.send(`Sucessfully set prefix to \`${prefix}\``); - } else { - await message.util.send(`Sucessfully reset prefix to \`${this.client.config.prefix}\``); - } - } - - async execSlash(message: BushInteractionMessage, { prefix }: { prefix?: SlashCommandOption<string> }): Promise<void> { - await this.changePrefix(message.guild, prefix?.value); - if (prefix) { - await message.reply(`Sucessfully set prefix to \`${prefix.value}\``); - } else { - await message.reply(`Sucessfully reset prefix to \`${this.client.config.prefix}\``); + await message.util.send(`${this.client.util.emojis.success} reset prefix to \`${this.client.config.prefix}\``); } } } diff --git a/src/commands/dev/eval.ts b/src/commands/dev/eval.ts index 8bf88ff..d2fe432 100644 --- a/src/commands/dev/eval.ts +++ b/src/commands/dev/eval.ts @@ -5,7 +5,7 @@ import { CommandInteraction, MessageEmbed, MessageEmbedOptions, Util } from 'dis import { transpile } from 'typescript'; import { inspect, promisify } from 'util'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; import { BushMessage } from '../../lib/extensions/BushMessage'; const clean = (text) => { @@ -123,7 +123,7 @@ export default class EvalCommand extends BushCommand { } public async exec( - message: BushMessage | BushInteractionMessage, + message: BushMessage | BushSlashMessage, args: { sel_depth: number; code: string; @@ -138,7 +138,7 @@ export default class EvalCommand extends BushCommand { if (!this.client.config.owners.includes(message.author.id)) return await message.util.reply(`${this.client.util.emojis.error} Only my developers can run this command.`); if (message.util.isSlash) { - await (message as BushInteractionMessage).interaction.defer({ ephemeral: args.silent }); + await (message as BushSlashMessage).interaction.defer({ ephemeral: args.silent }); } const code: { js?: string | null; ts?: string | null; lang?: 'js' | 'ts' } = {}; @@ -181,7 +181,13 @@ export default class EvalCommand extends BushCommand { channel = message.channel, config = this.client.config, members = message.guild.members, - roles = message.guild.roles; + roles = message.guild.roles, + { Ban } = await import('../../lib/models/Ban'), + { Global } = await import('../../lib/models/Global'), + { Guild } = await import('../../lib/models/Guild'), + { Level } = await import('../../lib/models/Level'), + { Modlog } = await import('../../lib/models/Modlog'), + { StickyRole } = await import('../../lib/models/StickyRole'); if (code[code.lang].replace(/ /g, '').includes('9+10' || '10+9')) { output = 21; } else { diff --git a/src/commands/dev/reload.ts b/src/commands/dev/reload.ts index 9aee9a7..94e31b0 100644 --- a/src/commands/dev/reload.ts +++ b/src/commands/dev/reload.ts @@ -2,7 +2,7 @@ import { stripIndent } from 'common-tags'; import { Message } from 'discord.js'; import { SlashCommandOption } from '../../lib/extensions/BushClientUtil'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; export default class ReloadCommand extends BushCommand { constructor() { @@ -55,7 +55,7 @@ export default class ReloadCommand extends BushCommand { await message.util.send(await this.getResponse(fast)); } - public async execSlash(message: BushInteractionMessage, { fast }: { fast: SlashCommandOption<boolean> }): Promise<void> { + public async execSlash(message: BushSlashMessage, { fast }: { fast: SlashCommandOption<boolean> }): Promise<void> { await message.interaction.reply(await this.getResponse(fast?.value)); } } diff --git a/src/commands/dev/setLevel.ts b/src/commands/dev/setLevel.ts index 58c01dd..ca555db 100644 --- a/src/commands/dev/setLevel.ts +++ b/src/commands/dev/setLevel.ts @@ -1,7 +1,7 @@ import { Message, User } from 'discord.js'; import { SlashCommandOption } from '../../lib/extensions/BushClientUtil'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; import { Level } from '../../lib/models'; import AllowedMentions from '../../lib/utils/AllowedMentions'; @@ -61,8 +61,7 @@ export default class SetLevelCommand extends BushCommand { id: user.id } }); - levelEntry.xp = Level.convertLevelToXp(level); - await levelEntry.save(); + await levelEntry.update({ xp: Level.convertLevelToXp(level) }); return `Successfully set level of <@${user.id}> to \`${level}\` (\`${levelEntry.xp}\` XP)`; } @@ -74,7 +73,7 @@ export default class SetLevelCommand extends BushCommand { } async execSlash( - message: BushInteractionMessage, + message: BushSlashMessage, { user, level }: { user: SlashCommandOption<void>; level: SlashCommandOption<number> } ): Promise<void> { await message.interaction.reply({ diff --git a/src/commands/dev/superUser.ts b/src/commands/dev/superUser.ts new file mode 100644 index 0000000..c3ed0b0 --- /dev/null +++ b/src/commands/dev/superUser.ts @@ -0,0 +1,73 @@ +import { Constants } from 'discord-akairo'; +import { User } from 'discord.js'; +import { BushCommand } from '../../lib/extensions/BushCommand'; +import { BushMessage } from '../../lib/extensions/BushMessage'; +import { Global } from '../../lib/models/Global'; + +export default class SuperUserCommand extends BushCommand { + public constructor() { + super('superuser', { + aliases: ['superuser', 'su'], + category: 'dev', + description: { + content: 'A command to manage superusers.', + usage: 'superuser <add/remove> <user>', + examples: ['superuser add IRONM00N'] + }, + clientPermissions: ['SEND_MESSAGES'], + ownerOnly: true + }); + } + *args(): unknown { + const action = yield { + id: 'action', + type: ['add', 'remove'], + match: Constants.ArgumentMatches.PHRASE, + prompt: { + start: 'Would you like to `add` or `remove` a user from the superuser list?', + retry: '{error} Choose if you would like to `add` or `remove` a user.', + required: true + } + }; + const user = yield { + id: 'user', + type: Constants.ArgumentTypes.USER, + match: Constants.ArgumentMatches.REST_CONTENT, + prompt: { + start: `Who would you like to ${action || 'add/remove'} from the superuser list?`, + retry: `Choose a valid user to ${action || 'add/remove'} from the superuser list.`, + required: true + } + }; + return { action, user }; + } + public async exec(message: BushMessage, args: { action: 'add' | 'remove'; user: User }): Promise<unknown> { + if (!this.client.config.owners.includes(message.author.id)) + return await message.util.reply(`${this.client.util.emojis.error} Only my developers can run this command...`); + + const superUsers = (await Global.findByPk(this.client.config.dev ? 'development' : 'production')).superUsers; + let success; + if (args.action === 'add') { + if (superUsers.includes(args.user.id)) { + return message.util.reply(`${this.client.util.emojis.warn} \`${args.user.tag}\` is already a superuser.`); + } + success = await this.client.util.insertOrRemoveFromGlobal('add', 'superUsers', args.user.id).catch(() => false); + } else { + if (!superUsers.includes(args.user.id)) { + return message.util.reply(`${this.client.util.emojis.warn} \`${args.user.tag}\` is not superuser.`); + } + success = await this.client.util.insertOrRemoveFromGlobal('remove', 'superUsers', args.user.id).catch(() => false); + } + if (success) { + const responses = [args.action == 'remove' ? `` : 'made', args.action == 'remove' ? 'is no longer' : '']; + return message.util.reply( + `${this.client.util.emojis.success} ${responses[0]} \`${args.user.tag}\` ${responses[1]} a superuser.` + ); + } else { + const response = [args.action == 'remove' ? `removing` : 'making', args.action == 'remove' ? `from` : 'to']; + return message.util.reply( + `${this.client.util.emojis.error} There was an error ${response[0]} \`${args.user.tag}\` ${response[1]} the superuser list.` + ); + } + } +} diff --git a/src/commands/info/botInfo.ts b/src/commands/info/botInfo.ts index 406ea2d..120527d 100644 --- a/src/commands/info/botInfo.ts +++ b/src/commands/info/botInfo.ts @@ -1,7 +1,7 @@ import { Message, MessageEmbed } from 'discord.js'; import { duration } from 'moment'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; export default class BotInfoCommand extends BushCommand { constructor() { @@ -51,7 +51,7 @@ export default class BotInfoCommand extends BushCommand { await message.util.send({ embeds: [await this.generateEmbed()] }); } - public async execSlash(message: BushInteractionMessage): Promise<void> { + public async execSlash(message: BushSlashMessage): Promise<void> { await message.interaction.reply({ embeds: [await this.generateEmbed()] }); } } diff --git a/src/commands/info/help.ts b/src/commands/info/help.ts index 2ffac1b..0efc6b3 100644 --- a/src/commands/info/help.ts +++ b/src/commands/info/help.ts @@ -1,6 +1,6 @@ import { MessageActionRow, MessageButton, MessageEmbed } from 'discord.js'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; import { BushMessage } from '../../lib/extensions/BushMessage'; export default class HelpCommand extends BushCommand { @@ -44,7 +44,7 @@ export default class HelpCommand extends BushCommand { } public async exec( - message: BushMessage | BushInteractionMessage, + message: BushMessage | BushSlashMessage, args: { command: BushCommand | string; showHidden?: boolean } ): Promise<unknown> { const prefix = this.client.config.dev ? 'dev ' : message.util.parsed.prefix; diff --git a/src/commands/info/ping.ts b/src/commands/info/ping.ts index fb93c50..e80cfb3 100644 --- a/src/commands/info/ping.ts +++ b/src/commands/info/ping.ts @@ -1,6 +1,6 @@ import { Message, MessageEmbed } from 'discord.js'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; export default class PingCommand extends BushCommand { constructor() { @@ -34,7 +34,7 @@ export default class PingCommand extends BushCommand { }); } - public async execSlash(message: BushInteractionMessage): Promise<void> { + public async execSlash(message: BushSlashMessage): Promise<void> { const timestamp1 = message.interaction.createdTimestamp; await message.interaction.reply('Pong!'); const timestamp2 = await message.interaction.fetchReply().then((m) => (m as Message).createdTimestamp); diff --git a/src/commands/info/pronouns.ts b/src/commands/info/pronouns.ts index 83aa5ca..79baeef 100644 --- a/src/commands/info/pronouns.ts +++ b/src/commands/info/pronouns.ts @@ -2,7 +2,7 @@ import { CommandInteraction, Message, MessageEmbed, User } from 'discord.js'; import got, { HTTPError } from 'got'; import { SlashCommandOption } from '../../lib/extensions/BushClientUtil'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; export const pronounMapping = { unspecified: 'Unspecified', @@ -107,7 +107,7 @@ export default class PronounsCommand extends BushCommand { const u = user || message.author; await this.sendResponse(message, u, u.id === message.author.id); } - async execSlash(message: BushInteractionMessage, { user }: { user?: SlashCommandOption<void> }): Promise<void> { + async execSlash(message: BushSlashMessage, { user }: { user?: SlashCommandOption<void> }): Promise<void> { const u = user?.user || message.author; await this.sendResponse(message.interaction, u, u.id === message.author.id); } diff --git a/src/commands/moderation/ban.ts b/src/commands/moderation/ban.ts index a493071..692691e 100644 --- a/src/commands/moderation/ban.ts +++ b/src/commands/moderation/ban.ts @@ -2,7 +2,7 @@ import { CommandInteraction, Message, User } from 'discord.js'; import moment from 'moment'; import { SlashCommandOption } from '../../lib/extensions/BushClientUtil'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; import { Ban, Guild, Modlog, ModlogType } from '../../lib/models'; const durationAliases: Record<string, string[]> = { @@ -168,7 +168,7 @@ export default class BanCommand extends BushCommand { } async execSlash( - message: BushInteractionMessage, + message: BushSlashMessage, { user, reason, diff --git a/src/commands/moderation/kick.ts b/src/commands/moderation/kick.ts index b97fe91..d00ae55 100644 --- a/src/commands/moderation/kick.ts +++ b/src/commands/moderation/kick.ts @@ -1,6 +1,6 @@ import { CommandInteraction, GuildMember, Message } from 'discord.js'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; import { Guild, Modlog, ModlogType } from '../../lib/models'; export default class KickCommand extends BushCommand { @@ -100,7 +100,7 @@ export default class KickCommand extends BushCommand { } } - async execSlash(message: BushInteractionMessage, { user, reason }: { user: GuildMember; reason?: string }): Promise<void> { + async execSlash(message: BushSlashMessage, { user, reason }: { user: GuildMember; reason?: string }): Promise<void> { for await (const response of this.genResponses(message.interaction, user, reason)) { await message.interaction.reply(response); } diff --git a/src/commands/moulberry-bush/capePerms.ts b/src/commands/moulberry-bush/capePerms.ts index 1592175..3e4563a 100644 --- a/src/commands/moulberry-bush/capePerms.ts +++ b/src/commands/moulberry-bush/capePerms.ts @@ -2,7 +2,7 @@ import { Message, MessageEmbed } from 'discord.js'; import got from 'got'; import { SlashCommandOption } from '../../lib/extensions/BushClientUtil'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; interface Capeperms { success: boolean; @@ -109,7 +109,7 @@ export default class CapePermissionsCommand extends BushCommand { await message.reply(await this.getResponse(user)); } - public async execSlash(message: BushInteractionMessage, { user }: { user: SlashCommandOption<string> }): Promise<void> { + public async execSlash(message: BushSlashMessage, { user }: { user: SlashCommandOption<string> }): Promise<void> { await message.reply(await this.getResponse(user.value)); } } diff --git a/src/commands/moulberry-bush/level.ts b/src/commands/moulberry-bush/level.ts index 64d6dad..f822555 100644 --- a/src/commands/moulberry-bush/level.ts +++ b/src/commands/moulberry-bush/level.ts @@ -1,6 +1,6 @@ import { CommandInteractionOption, Message, User } from 'discord.js'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; import { Level } from '../../lib/models'; /* import canvas from 'canvas'; @@ -148,7 +148,7 @@ export default class LevelCommand extends BushCommand { // ); await message.reply(await this.getResponse(user || message.author)); } - async execSlash(message: BushInteractionMessage, { user }: { user?: CommandInteractionOption }): Promise<void> { + async execSlash(message: BushSlashMessage, { user }: { user?: CommandInteractionOption }): Promise<void> { // await message.reply( // new MessageAttachment( // await this.getImage(user?.user || message.user), diff --git a/src/commands/moulberry-bush/rule.ts b/src/commands/moulberry-bush/rule.ts index 16cda5e..e9b09eb 100644 --- a/src/commands/moulberry-bush/rule.ts +++ b/src/commands/moulberry-bush/rule.ts @@ -2,7 +2,7 @@ import { Argument } from 'discord-akairo'; import { CommandInteraction, Message, MessageEmbed, User } from 'discord.js'; import { SlashCommandOption } from '../../lib/extensions/BushClientUtil'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; export default class RuleCommand extends BushCommand { private rules = [ @@ -157,7 +157,7 @@ export default class RuleCommand extends BushCommand { } public async execSlash( - message: BushInteractionMessage, + message: BushSlashMessage, { rule, user }: { rule?: SlashCommandOption<number>; user?: SlashCommandOption<void> } ): Promise<void> { const response = this.getResponse(message.interaction, rule?.value, user?.user); diff --git a/src/lib/extensions/BushClient.ts b/src/lib/extensions/BushClient.ts index ad22cfe..d0758d4 100644 --- a/src/lib/extensions/BushClient.ts +++ b/src/lib/extensions/BushClient.ts @@ -36,6 +36,7 @@ export class BushClient extends AkairoClient { public db: Sequelize; public logger: BushLogger; public constants = BushConstants; + public cache = BushCache; constructor(config: BotConfig) { super( { @@ -164,7 +165,7 @@ export class BushClient extends AkairoClient { Models.Ban.initModel(this.db); Models.Level.initModel(this.db); Models.StickyRole.initModel(this.db); - await this.db.sync(); // Sync all tables to fix everything if updated + await this.db.sync({ alter: true }); // Sync all tables to fix everything if updated this.console.success('Startup', `Successfully connected to <<database>>.`, false); } catch (error) { this.console.error('Startup', `Failed to connect to <<database>> with error:\n` + error?.stack, false); diff --git a/src/lib/extensions/BushClientUtil.ts b/src/lib/extensions/BushClientUtil.ts index 6687cb0..4d428ee 100644 --- a/src/lib/extensions/BushClientUtil.ts +++ b/src/lib/extensions/BushClientUtil.ts @@ -31,6 +31,8 @@ import { } from 'discord.js'; import got from 'got'; import { promisify } from 'util'; +import { Global } from '../models/Global'; +import { BushCache } from '../utils/BushCache'; import { BushClient } from './BushClient'; import { BushMessage } from './BushMessage'; @@ -233,7 +235,7 @@ export class BushClientUtil extends ClientUtil { /** Commonly Used Emojis */ public emojis = { success: '<:checkmark:837109864101707807>', - warn: '<:warn:848726900876247050> ', + warn: '<:warn:848726900876247050>', error: '<:error:837123021016924261>', successFull: '<:checkmark_full:850118767576088646>', warnFull: '<:warn_full:850118767391539312>', @@ -481,4 +483,20 @@ export class BushClientUtil extends ClientUtil { array[l - 1] = `${conjunction} ${array[l - 1]}`; return array.join(', '); } + + public async insertOrRemoveFromGlobal(action: 'add' | 'remove', key: keyof typeof BushCache, value: any) { + const environment = this.client.config.dev ? 'development' : 'production'; + let row = await Global.findByPk(environment); + const oldValue: any[] = row[key]; + let newValue: any[]; + if (action === 'add') { + if (!oldValue.includes(action)) oldValue.push(value); + newValue = oldValue; + } else { + newValue = oldValue.filter((ae) => ae !== value); + } + row[key] = newValue; + this.client.cache[key] = newValue; + return await row.save().catch((e) => this.client.logger.error('insertOrRemoveFromGlobal', e)); + } } diff --git a/src/lib/extensions/BushCommand.ts b/src/lib/extensions/BushCommand.ts index 8358c46..bc6ff68 100644 --- a/src/lib/extensions/BushCommand.ts +++ b/src/lib/extensions/BushCommand.ts @@ -4,7 +4,7 @@ import { Command, CommandOptions } from 'discord-akairo'; import { Snowflake } from 'discord.js'; import { BushClient } from './BushClient'; import { BushCommandHandler } from './BushCommandHandler'; -import { BushInteractionMessage } from './BushInteractionMessage'; +import { BushSlashMessage } from './BushInteractionMessage'; import { BushMessage } from './BushMessage'; export interface BushCommandOptions extends CommandOptions { @@ -37,7 +37,7 @@ export class BushCommand extends Command { } public exec(message: BushMessage, args: any): any; - public exec(message: BushMessage | BushInteractionMessage, args: any): any { + public exec(message: BushMessage | BushSlashMessage, args: any): any { super.exec(message, args); } } diff --git a/src/lib/extensions/BushInteractionMessage.ts b/src/lib/extensions/BushInteractionMessage.ts index 9bdc291..ade11ea 100644 --- a/src/lib/extensions/BushInteractionMessage.ts +++ b/src/lib/extensions/BushInteractionMessage.ts @@ -2,7 +2,7 @@ import { AkairoMessage } from 'discord-akairo'; import { CommandInteraction } from 'discord.js'; import { BushClient } from './BushClient'; -export class BushInteractionMessage extends AkairoMessage { +export class BushSlashMessage extends AkairoMessage { public constructor( client: BushClient, interaction: CommandInteraction, diff --git a/src/lib/models/Global.ts b/src/lib/models/Global.ts index 65f51c4..abe0ab3 100644 --- a/src/lib/models/Global.ts +++ b/src/lib/models/Global.ts @@ -3,6 +3,7 @@ import { DataTypes, Optional, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; export interface GlobalModel { + environment: 'production' | 'development'; superUsers: Snowflake[]; disabledCommands: string[]; blacklistedUsers: Snowflake[]; @@ -15,6 +16,7 @@ export type GlobalModelCreationAttributes = Optional< >; export class Global extends BaseModel<GlobalModel, GlobalModelCreationAttributes> implements GlobalModel { + environment: 'production' | 'development'; superUsers: Snowflake[]; disabledCommands: string[]; blacklistedUsers: Snowflake[]; @@ -23,24 +25,58 @@ export class Global extends BaseModel<GlobalModel, GlobalModelCreationAttributes static initModel(sequelize: Sequelize): void { Global.init( { + environment: { + type: DataTypes.STRING, + primaryKey: true + }, superUsers: { - type: DataTypes.ARRAY(DataTypes.STRING), + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('superUsers') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('superUsers', JSON.stringify(val) as unknown as Snowflake[]); + }, allowNull: true }, disabledCommands: { - type: DataTypes.ARRAY(DataTypes.STRING), + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('disabledCommands') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('disabledCommands', JSON.stringify(val) as unknown as string[]); + }, allowNull: true }, blacklistedUsers: { - type: DataTypes.ARRAY(DataTypes.STRING), + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('blacklistedUsers') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('blacklistedUsers', JSON.stringify(val) as unknown as Snowflake[]); + }, allowNull: true }, blacklistedGuilds: { - type: DataTypes.ARRAY(DataTypes.STRING), + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('blacklistedGuilds') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('blacklistedGuilds', JSON.stringify(val) as unknown as Snowflake[]); + }, allowNull: true }, blacklistedChannels: { - type: DataTypes.ARRAY(DataTypes.STRING), + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('blacklistedChannels') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('blacklistedChannels', JSON.stringify(val) as unknown as Snowflake[]); + }, allowNull: true } }, diff --git a/src/lib/models/Guild.ts b/src/lib/models/Guild.ts index bc93951..c4ae53e 100644 --- a/src/lib/models/Guild.ts +++ b/src/lib/models/Guild.ts @@ -30,11 +30,23 @@ export class Guild extends BaseModel<GuildModel, GuildModelCreationAttributes> i defaultValue: client.config.prefix }, autoPublishChannels: { - type: DataTypes.ARRAY(DataTypes.STRING), + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('autoPublishChannels') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('autoPublishChannels', JSON.stringify(val) as unknown as Snowflake[]); + }, allowNull: true }, blacklistedChannels: { - type: DataTypes.ARRAY(DataTypes.STRING), + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('blacklistedChannels') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('blacklistedChannels', JSON.stringify(val) as unknown as Snowflake[]); + }, allowNull: true } }, diff --git a/src/lib/models/Level.ts b/src/lib/models/Level.ts index 6113627..426ec1a 100644 --- a/src/lib/models/Level.ts +++ b/src/lib/models/Level.ts @@ -32,7 +32,7 @@ export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> { defaultValue: 0 } }, - { sequelize } + { sequelize: sequelize } ); } static convertXpToLevel(xp: number): number { diff --git a/src/lib/models/StickyRole.ts b/src/lib/models/StickyRole.ts index 597d7c5..a3928e7 100644 --- a/src/lib/models/StickyRole.ts +++ b/src/lib/models/StickyRole.ts @@ -30,8 +30,14 @@ export class StickyRole extends BaseModel<StickyRoleModel, StickyRoleModelCreati }, roles: { - type: DataTypes.ARRAY(DataTypes.STRING), - allowNull: false + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('roles') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('roles', JSON.stringify(val) as unknown as Snowflake[]); + }, + allowNull: true } }, { sequelize } diff --git a/src/lib/utils/BushCache.ts b/src/lib/utils/BushCache.ts index 915fcb1..947b15d 100644 --- a/src/lib/utils/BushCache.ts +++ b/src/lib/utils/BushCache.ts @@ -2,4 +2,8 @@ import { Snowflake } from 'discord.js'; export class BushCache { public static superUsers = new Array<Snowflake>(); + public static disabledCommands = new Array<string>(); + public static blacklistedChannels = new Array<Snowflake>(); + public static blacklistedGuilds = new Array<Snowflake>(); + public static blacklistedUsers = new Array<Snowflake>(); } diff --git a/src/listeners/commands/slashBlocked.ts b/src/listeners/commands/slashBlocked.ts index d8ef736..e87ba70 100644 --- a/src/listeners/commands/slashBlocked.ts +++ b/src/listeners/commands/slashBlocked.ts @@ -1,5 +1,5 @@ import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; import { BushListener } from '../../lib/extensions/BushListener'; export default class SlashBlockedListener extends BushListener { @@ -10,7 +10,7 @@ export default class SlashBlockedListener extends BushListener { }); } - public async exec(message: BushInteractionMessage, command: BushCommand, reason: string): Promise<unknown> { + public async exec(message: BushSlashMessage, command: BushCommand, reason: string): Promise<unknown> { this.client.console.info( 'SlashBlocked', `<<${message.author.tag}>> tried to run <<${message.util.parsed.command}>> but was blocked because <<${reason}>>.`, diff --git a/src/listeners/commands/slashCommandError.ts b/src/listeners/commands/slashCommandError.ts index 718e992..b9123e8 100644 --- a/src/listeners/commands/slashCommandError.ts +++ b/src/listeners/commands/slashCommandError.ts @@ -1,7 +1,7 @@ import { stripIndents } from 'common-tags'; import { MessageEmbed } from 'discord.js'; import { BushCommand } from '../../lib/extensions/BushCommand'; -import { BushInteractionMessage } from '../../lib/extensions/BushInteractionMessage'; +import { BushSlashMessage } from '../../lib/extensions/BushInteractionMessage'; import { BushListener } from '../../lib/extensions/BushListener'; export default class SlashCommandErrorListener extends BushListener { @@ -11,7 +11,7 @@ export default class SlashCommandErrorListener extends BushListener { event: 'slashError' }); } - async exec(error: Error, message: BushInteractionMessage, command: BushCommand): Promise<void> { + async exec(error: Error, message: BushSlashMessage, command: BushCommand): Promise<void> { const errorNo = Math.floor(Math.random() * 6969696969) + 69; // hehe funny number const errorEmbed: MessageEmbed = new MessageEmbed() .setTitle(`Slash Error # \`${errorNo}\`: An error occurred`) diff --git a/src/listeners/message/level.ts b/src/listeners/message/level.ts index 615c013..74c4db8 100644 --- a/src/listeners/message/level.ts +++ b/src/listeners/message/level.ts @@ -28,8 +28,12 @@ export default class LevelListener extends BushListener { }); const xpToGive = Level.genRandomizedXp(); user.xp += xpToGive; - await user.save(); - await this.client.logger.verbose(`LevelListener`, `Gave <<${xpToGive}>> XP to <<${message.author.tag}>>.`); + const success = await user.save().catch((e) => { + this.client.logger.error('LevelMessageListener', e); + return false; + }); + if (success) + await this.client.logger.verbose(`LevelMessageListener`, `Gave <<${xpToGive}>> XP to <<${message.author.tag}>>.`); this.levelCooldowns.add(message.author.id); setTimeout(() => this.levelCooldowns.delete(message.author.id), 60_000); } diff --git a/src/tasks/unmute.ts b/src/tasks/unmute.ts new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/tasks/unmute.ts diff --git a/src/tasks/unrole.ts b/src/tasks/unrole.ts new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/tasks/unrole.ts diff --git a/src/tasks/updateCache.ts b/src/tasks/updateCache.ts new file mode 100644 index 0000000..5994af9 --- /dev/null +++ b/src/tasks/updateCache.ts @@ -0,0 +1,30 @@ +import { BushTask } from '../lib/extensions/BushTask'; +import { Global } from '../lib/models'; + +export default class UpdateCacheTask extends BushTask { + constructor() { + super('updateCache', { + delay: 300_000, // 5 minutes + runOnStart: true + }); + } + async exec(): Promise<void> { + const environment = this.client.config.dev ? 'development' : 'production'; + let row = await Global.findByPk(environment); + if (!row) { + row = await Global.create({ + environment, + superUsers: [], + blacklistedChannels: [], + blacklistedGuilds: [], + blacklistedUsers: [], + disabledCommands: [] + }); + } + + for (let option in row) { + if (this.client.cache[option]) this.client.cache[option] = row[option]; + } + this.client.logger.verbose(`UpdateCache`, `Updated cache.`); + } +} |