diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/dev/reload.ts | 43 | ||||
-rw-r--r-- | src/commands/info/pronouns.ts | 25 | ||||
-rw-r--r-- | src/commands/moulberry-bush/level.ts | 109 | ||||
-rw-r--r-- | src/lib/extensions/BushClient.ts | 6 | ||||
-rw-r--r-- | src/lib/extensions/Util.ts | 166 | ||||
-rw-r--r-- | src/lib/models/Ban.ts | 9 | ||||
-rw-r--r-- | src/lib/models/Modlog.ts | 9 | ||||
-rw-r--r-- | src/listeners/client/syncSlashCommands.ts | 5 |
8 files changed, 326 insertions, 46 deletions
diff --git a/src/commands/dev/reload.ts b/src/commands/dev/reload.ts index 64e5745..36c6fd7 100644 --- a/src/commands/dev/reload.ts +++ b/src/commands/dev/reload.ts @@ -2,6 +2,8 @@ import { BushCommand } from '../../lib/extensions/BushCommand'; import { stripIndent } from 'common-tags'; import { Message } from 'discord.js'; import { CommandInteraction } from 'discord.js'; +import { SlashCommandOption } from '../../lib/extensions/Util'; +import { ApplicationCommandOptionType } from 'discord-api-types'; export default class ReloadCommand extends BushCommand { constructor() { @@ -13,19 +15,36 @@ export default class ReloadCommand extends BushCommand { usage: 'reload', examples: ['reload'] }, + args: [ + { + id: 'fast', + match: 'flag', + flag: '--fast' + } + ], ownerOnly: true, - typing: true + typing: true, + slashCommandOptions: [ + { + type: ApplicationCommandOptionType.BOOLEAN, + name: 'fast', + description: 'Wheather to use esbuild for fast compiling or not', + required: false + } + ] }); } - private async getResponse(): Promise<string> { + private async getResponse(fast: boolean): Promise<string> { try { - await this.client.util.shell('yarn rimraf dist/'); - await this.client.util.shell('yarn tsc'); + const s = new Date(); + await this.client.util.shell(`yarn build-${fast ? 'esbuild' : 'tsc'}`); this.client.commandHandler.reloadAll(); this.client.listenerHandler.reloadAll(); this.client.inhibitorHandler.reloadAll(); - return '🔁 Successfully reloaded!'; + return `🔁 Successfully reloaded! (${ + new Date().getTime() - s.getTime() + }ms)`; } catch (e) { return stripIndent` An error occured while reloading: @@ -34,11 +53,17 @@ export default class ReloadCommand extends BushCommand { } } - public async exec(message: Message): Promise<void> { - await message.util.send(await this.getResponse()); + public async exec( + message: Message, + { fast }: { fast: boolean } + ): Promise<void> { + await message.util.send(await this.getResponse(fast)); } - public async execSlash(message: CommandInteraction): Promise<void> { - await message.reply(await this.getResponse()); + public async execSlash( + message: CommandInteraction, + { fast }: { fast: SlashCommandOption<boolean> } + ): Promise<void> { + await message.reply(await this.getResponse(fast?.value)); } } diff --git a/src/commands/info/pronouns.ts b/src/commands/info/pronouns.ts index c53c542..2c1d5f2 100644 --- a/src/commands/info/pronouns.ts +++ b/src/commands/info/pronouns.ts @@ -59,9 +59,17 @@ export default class PronounsCommand extends BushCommand { slashEmphemeral: true // I'll add dynamic checking to this later }); } - async sendResponse(message: Message | CommandInteraction, user: User, author: boolean): Promise<void> { + async sendResponse( + message: Message | CommandInteraction, + user: User, + author: boolean + ): Promise<void> { try { - const apiRes: { pronouns: pronounsType } = await got.get(`https://pronoundb.org/api/v1/lookup?platform=discord&id=${user.id}`).json(); + const apiRes: { pronouns: pronounsType } = await got + .get( + `https://pronoundb.org/api/v1/lookup?platform=discord&id=${user.id}` + ) + .json(); if (message instanceof Message) { message.reply( new MessageEmbed({ @@ -88,9 +96,13 @@ export default class PronounsCommand extends BushCommand { } catch (e) { if (e instanceof HTTPError && e.response.statusCode === 404) { if (author) { - await message.reply('You do not appear to have any pronouns set. Please go to https://pronoundb.org/ and set your pronouns.'); + await message.reply( + 'You do not appear to have any pronouns set. Please go to https://pronoundb.org/ and set your pronouns.' + ); } else { - await message.reply(`${user.tag} does not appear to have any pronouns set. Please tell them to go to https://pronoundb.org/ and set their pronouns.`); + await message.reply( + `${user.tag} does not appear to have any pronouns set. Please tell them to go to https://pronoundb.org/ and set their pronouns.` + ); } } else throw e; } @@ -99,7 +111,10 @@ export default class PronounsCommand extends BushCommand { const u = user || message.author; await this.sendResponse(message, u, u.id === message.author.id); } - async execSlash(message: CommandInteraction, { user }: { user?: SlashCommandOption<void> }): Promise<void> { + async execSlash( + message: CommandInteraction, + { user }: { user?: SlashCommandOption<void> } + ): Promise<void> { const u = user?.user || message.user; await this.sendResponse(message, u, u.id === message.user.id); } diff --git a/src/commands/moulberry-bush/level.ts b/src/commands/moulberry-bush/level.ts index 2b32abd..554219d 100644 --- a/src/commands/moulberry-bush/level.ts +++ b/src/commands/moulberry-bush/level.ts @@ -5,6 +5,11 @@ import { CommandInteraction } from 'discord.js'; import { User } from 'discord.js'; import { BushCommand } from '../../lib/extensions/BushCommand'; import { Level } from '../../lib/models'; +import canvas from 'canvas'; +import { MessageAttachment } from 'discord.js'; +import { join } from 'path'; +import got from 'got/dist/source'; +import { CanvasProgressBar } from '../../lib/extensions/Util'; export default class LevelCommand extends BushCommand { constructor() { @@ -38,6 +43,93 @@ export default class LevelCommand extends BushCommand { }); } + // private simplifyXP(xp: number): string { + + // } + + // private async getImage(user: User): Promise<Buffer> { + // // I added comments because this code is impossible to read + // const [userLevelRow] = await Level.findOrBuild({ + // where: { + // id: user.id + // }, + // defaults: { + // id: user.id + // } + // }); + // const userLevel = userLevelRow.level + // const currentLevelXP = Level.convertLevelToXp(userLevel); + // const currentLevelXPProgress = userLevelRow.xp - currentLevelXP; + // const xpForNextLevel = + // Level.convertLevelToXp(userLevelRow.level + 1) - currentLevelXP; + // // Load roboto font because yes + // canvas.registerFont( + // join(__dirname, '..', '..', '..', 'Roboto-Regular.ttf'), + // { + // family: 'Roboto' + // } + // ); + // // Create image canvas + // const image = canvas.createCanvas(800, 200), + // ctx = image.getContext('2d'); + // // Fill background + // ctx.fillStyle = '#00c7eb'; + // ctx.fillRect(0, 0, image.width, image.height); + // // Draw avatar + // const avatarBuffer = await got + // .get(user.displayAvatarURL({ format: 'png', size: 128 })) + // .buffer(); + // const avatarImage = new canvas.Image(); + // avatarImage.src = avatarBuffer; + // avatarImage.height = 128 + // avatarImage.width = 128 + // const imageTopCoord = (image.height / 2) - (avatarImage.height / 2) + // ctx.drawImage(avatarImage, imageTopCoord, imageTopCoord); + // // Write tag of user + // ctx.font = '30px Roboto'; + // ctx.fillStyle = 'black'; + // const measuredTag = ctx.measureText(user.tag); + // ctx.fillText(user.tag, avatarImage.width + 70, 60); + // // Draw line under tag + // ctx.fillStyle = 'yellow'; + // ctx.fillRect( + // avatarImage.width + 70, + // 65 + measuredTag.actualBoundingBoxDescent, + // measuredTag.width, + // 3 + // ); + // // Draw leveling bar + // const fullProgressBar = new CanvasProgressBar( + // ctx, + // { + // x: avatarImage.width + 70, + // y: avatarImage.height - 10, + // height: 30, + // width: 550 + // }, + // '#6e6e6e', + // 1 + // ); + // fullProgressBar.draw(); + // const progressBar = new CanvasProgressBar( + // ctx, + // { + // x: avatarImage.width + 70, + // y: avatarImage.height - 10, + // height: 30, + // width: 550 + // }, + // 'yellow', + // currentLevelXPProgress / xpForNextLevel + // ); + // progressBar.draw(); + // // Draw level data text + // ctx.fillStyle = 'black' + // ctx.fillText(`Level: ${userLevel} XP: $`, avatarImage.width + 70, avatarImage.height - 20) + // // Return image in buffer form + // return image.toBuffer(); + // } + private async getResponse(user: User): Promise<string> { const userLevelRow = await Level.findByPk(user.id); if (userLevelRow) { @@ -48,9 +140,24 @@ export default class LevelCommand extends BushCommand { } async exec(message: Message, { user }: { user?: User }): Promise<void> { + // await message.reply( + // new MessageAttachment( + // await this.getImage(user || message.author), + // 'lel.png' + // ) + // ); await message.reply(await this.getResponse(user || message.author)); } - async execSlash(message: CommandInteraction, { user }: { user?: CommandInteractionOption }): Promise<void> { + async execSlash( + message: CommandInteraction, + { user }: { user?: CommandInteractionOption } + ): Promise<void> { + // await message.reply( + // new MessageAttachment( + // await this.getImage(user?.user || message.user), + // 'lel.png' + // ) + // ); await message.reply(await this.getResponse(user?.user || message.user)); } } diff --git a/src/lib/extensions/BushClient.ts b/src/lib/extensions/BushClient.ts index 2b674b1..8a0fc8c 100644 --- a/src/lib/extensions/BushClient.ts +++ b/src/lib/extensions/BushClient.ts @@ -126,11 +126,7 @@ export class BushClient extends AkairoClient { Models.Modlog.initModel(this.db); Models.Ban.initModel(this.db); Models.Level.initModel(this.db); - try { - await this.db.sync(); // Sync all tables to fix everything if updated - } catch { - // Ignore error - } + await this.db.sync(); // Sync all tables to fix everything if updated } public async start(): Promise<void> { diff --git a/src/lib/extensions/Util.ts b/src/lib/extensions/Util.ts index 431410c..3e6882a 100644 --- a/src/lib/extensions/Util.ts +++ b/src/lib/extensions/Util.ts @@ -232,40 +232,81 @@ export class Util extends ClientUtil { let fetchedGuild: Guild; if (guild) fetchedGuild = this.client.guilds.cache.get(guild); try { - const registered = guild === undefined ? await this.client.application.commands.fetch() : await fetchedGuild.commands.fetch(); + const registered = + guild === undefined + ? await this.client.application.commands.fetch() + : await fetchedGuild.commands.fetch(); for (const [, registeredCommand] of registered) { - if (!this.client.commandHandler.modules.find((cmd) => cmd.id == registeredCommand.name)?.execSlash || force) { - guild === undefined ? await this.client.application.commands.delete(registeredCommand.id) : await fetchedGuild.commands.delete(registeredCommand.id); - this.client.logger.verbose(chalk`{red Deleted slash command ${registeredCommand.name}${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}}`); + if ( + !this.client.commandHandler.modules.find( + (cmd) => cmd.id == registeredCommand.name + )?.execSlash || + force + ) { + guild === undefined + ? await this.client.application.commands.delete( + registeredCommand.id + ) + : await fetchedGuild.commands.delete(registeredCommand.id); + this.client.logger.verbose( + chalk`{red Deleted slash command ${registeredCommand.name}${ + guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' + }}` + ); } } - for (const [, BushCommand] of this.client.commandHandler.modules) { - if (BushCommand.execSlash) { - const found = registered.find((i) => i.name == BushCommand.id); + for (const [, botCommand] of this.client.commandHandler.modules) { + if (botCommand.execSlash) { + const found = registered.find((i) => i.name == botCommand.id); const slashdata = { - name: BushCommand.id, - description: BushCommand.description.content, - options: BushCommand.options.slashCommandOptions - }; + name: botCommand.id, + description: botCommand.description.content, + options: botCommand.options.slashCommandOptions + };botCommand if (found?.id && !force) { if (slashdata.description !== found.description) { - guild === undefined ? await this.client.application.commands.edit(found.id, slashdata) : fetchedGuild.commands.edit(found.id, slashdata); - this.client.logger.verbose(chalk`{yellow Edited slash command ${BushCommand.id}${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}}`); + guild === undefined + ? await this.client.application.commands.edit( + found.id, + slashdata + ) + : fetchedGuild.commands.edit(found.id, slashdata); + this.client.logger.verbose( + chalk`{yellow Edited slash command ${botCommand.id}${ + guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' + }}` + ); } } else { - guild === undefined ? await this.client.application.commands.create(slashdata) : fetchedGuild.commands.create(slashdata); - this.client.logger.verbose(chalk`{green Created slash command ${BushCommand.id}${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}}`); + guild === undefined + ? await this.client.application.commands.create(slashdata) + : fetchedGuild.commands.create(slashdata); + this.client.logger.verbose( + chalk`{green Created slash command ${botCommand.id}${ + guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' + }}` + ); } } } - return this.client.logger.log(chalk.green(`Slash commands registered${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}`)); + return this.client.logger.log( + chalk.green( + `Slash commands registered${ + guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' + }` + ) + ); } catch (e) { console.log(chalk.red(e.stack)); - return this.client.logger.error(chalk`{red Slash commands not registered${guild !== undefined ? ` in guild ${fetchedGuild.name}` : ''}, see above error.}`); + return this.client.logger.error( + chalk`{red Slash commands not registered${ + guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' + }, see above error.}` + ); } } @@ -297,3 +338,94 @@ export class Util extends ClientUtil { { name: 'No Support', id: '790247359824396319' } ]; } + +// I just copy pasted this code from stackoverflow don't yell at me if there is issues for it +export class CanvasProgressBar { + private x: number; + private y: number; + private w: number; + private h: number; + private color: string; + private percentage: number; + private p: number; + private ctx: CanvasRenderingContext2D; + + constructor( + ctx: CanvasRenderingContext2D, + dimension: { x: number; y: number; width: number; height: number }, + color: string, + percentage: number + ) { + ({ x: this.x, y: this.y, width: this.w, height: this.h } = dimension); + this.color = color; + this.percentage = percentage; + this.p; + this.ctx = ctx; + } + + draw() { + // ----------------- + this.p = this.percentage * this.w; + if (this.p <= this.h) { + this.ctx.beginPath(); + this.ctx.arc( + this.h / 2 + this.x, + this.h / 2 + this.y, + this.h / 2, + Math.PI - Math.acos((this.h - this.p) / this.h), + Math.PI + Math.acos((this.h - this.p) / this.h) + ); + this.ctx.save(); + this.ctx.scale(-1, 1); + this.ctx.arc( + this.h / 2 - this.p - this.x, + this.h / 2 + this.y, + this.h / 2, + Math.PI - Math.acos((this.h - this.p) / this.h), + Math.PI + Math.acos((this.h - this.p) / this.h) + ); + this.ctx.restore(); + this.ctx.closePath(); + } else { + this.ctx.beginPath(); + this.ctx.arc( + this.h / 2 + this.x, + this.h / 2 + this.y, + this.h / 2, + Math.PI / 2, + (3 / 2) * Math.PI + ); + this.ctx.lineTo(this.p - this.h + this.x, 0 + this.y); + this.ctx.arc( + this.p - this.h / 2 + this.x, + this.h / 2 + this.y, + this.h / 2, + (3 / 2) * Math.PI, + Math.PI / 2 + ); + this.ctx.lineTo(this.h / 2 + this.x, this.h + this.y); + this.ctx.closePath(); + } + this.ctx.fillStyle = this.color; + this.ctx.fill(); + } + + // showWholeProgressBar(){ + // this.ctx.beginPath(); + // this.ctx.arc(this.h / 2 + this.x, this.h / 2 + this.y, this.h / 2, Math.PI / 2, 3 / 2 * Math.PI); + // this.ctx.lineTo(this.w - this.h + this.x, 0 + this.y); + // this.ctx.arc(this.w - this.h / 2 + this.x, this.h / 2 + this.y, this.h / 2, 3 / 2 *Math.PI, Math.PI / 2); + // this.ctx.lineTo(this.h / 2 + this.x, this.h + this.y); + // this.ctx.strokeStyle = '#000000'; + // this.ctx.stroke(); + // this.ctx.closePath(); + // } + + get PPercentage() { + return this.percentage * 100; + } + + set PPercentage(x) { + this.percentage = x / 100; + } +} diff --git a/src/lib/models/Ban.ts b/src/lib/models/Ban.ts index 75e38ab..66c1be9 100644 --- a/src/lib/models/Ban.ts +++ b/src/lib/models/Ban.ts @@ -77,10 +77,11 @@ export class Ban extends BaseModel<BanModel, BanModelCreationAttributes> impleme }, modlog: { type: DataTypes.STRING, - allowNull: false, - references: { - model: Models.Modlog - } + allowNull: false + // references: { + // model: Models.Modlog, + // key: 'id' + // } } }, { sequelize: sequelize } diff --git a/src/lib/models/Modlog.ts b/src/lib/models/Modlog.ts index 2b2050a..3917a88 100644 --- a/src/lib/models/Modlog.ts +++ b/src/lib/models/Modlog.ts @@ -71,10 +71,11 @@ export class Modlog extends BaseModel<ModlogModel, ModlogModelCreationAttributes allowNull: true }, guild: { - type: DataTypes.STRING, - references: { - model: Models.Guild - } + type: DataTypes.STRING + // references: { + // model: Models.Guild, + // key: 'id' + // } } }, { sequelize: sequelize } diff --git a/src/listeners/client/syncSlashCommands.ts b/src/listeners/client/syncSlashCommands.ts index 69b91bc..7835136 100644 --- a/src/listeners/client/syncSlashCommands.ts +++ b/src/listeners/client/syncSlashCommands.ts @@ -10,7 +10,10 @@ export default class SyncSlashCommandsListener extends BushListener { async exec(): Promise<void> { if (this.client.config.dev && this.client.config.devGuild) { // Use guild slash commands for instant registration in dev - await this.client.util.syncSlashCommands(false, this.client.config.devGuild); + await this.client.util.syncSlashCommands( + false, + this.client.config.devGuild + ); } else { // Use global in production await this.client.util.syncSlashCommands(); |