diff options
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/config/config.ts | 41 | ||||
-rw-r--r-- | src/commands/config/joinRoles.ts | 2 | ||||
-rw-r--r-- | src/commands/leveling/leaderboard.ts | 52 | ||||
-rw-r--r-- | src/commands/leveling/level.ts | 42 |
4 files changed, 99 insertions, 38 deletions
diff --git a/src/commands/config/config.ts b/src/commands/config/config.ts index df650fe..8362144 100644 --- a/src/commands/config/config.ts +++ b/src/commands/config/config.ts @@ -117,9 +117,11 @@ export default class SettingsCommand extends BushCommand { } }; - const actionType = guildSettingsObj[setting as unknown as GuildSettings].type.includes('-array') - ? ['view', 'add', 'remove'] - : ['view', 'set']; + const actionType = setting + ? guildSettingsObj[setting as unknown as GuildSettings]?.type.includes('-array') + ? ['view', 'add', 'remove'] + : ['view', 'set'] + : undefined; const action = setting ? yield { @@ -127,11 +129,11 @@ export default class SettingsCommand extends BushCommand { type: actionType, prompt: { start: `Would you like to ${util.oxford( - actionType.map((a) => `\`${a}\``), + actionType!.map((a) => `\`${a}\``), 'or' )} the \`${setting}\` setting?`, retry: `{error} Choose one of the following actions to perform on the ${setting} setting: ${util.oxford( - actionType.map((a) => `\`${a}\``), + actionType!.map((a) => `\`${a}\``), 'or' )}`, optional @@ -139,16 +141,18 @@ export default class SettingsCommand extends BushCommand { } : undefined; - const valueType = guildSettingsObj[setting as unknown as GuildSettings].type.replace('-array', '') as - | 'string' - | 'channel' - | 'role'; + const valueType = + setting && action && action !== 'view' + ? (guildSettingsObj[setting as unknown as GuildSettings].type.replace('-array', '') as 'string' | 'channel' | 'role') + : undefined; const grammar = - (action as unknown as 'add' | 'remove' | 'set') === 'add' - ? `to the ${setting} setting` - : (action as unknown as 'remove' | 'set') === 'remove' - ? `from the ${setting} setting` - : `the ${setting} setting to`; + setting && action && action !== 'view' + ? (action as unknown as 'add' | 'remove' | 'set') === 'add' + ? `to the ${setting} setting` + : (action as unknown as 'remove' | 'set') === 'remove' + ? `from the ${setting} setting` + : `the ${setting} setting to` + : undefined; const value = setting && action && action !== 'view' @@ -186,10 +190,6 @@ export default class SettingsCommand extends BushCommand { const action = message.util.isSlash ? args.subcommand! : args.action!; const value = args.value; - client.console.debug(setting); - client.console.debug(action); - client.console.debug(value); - let msg; if (!setting || action === 'view') { @@ -269,7 +269,7 @@ export default class SettingsCommand extends BushCommand { const settingsEmbed = new MessageEmbed().setColor(util.colors.default); if (!setting) { settingsEmbed.setTitle(`${message.guild!.name}'s Settings`); - const desc = settingsArr.map((s) => `**${guildSettingsObj[s].name}**`).join('\n'); + const desc = settingsArr.map((s) => `:wrench: **${guildSettingsObj[s].name}**`).join('\n'); settingsEmbed.setDescription(desc); const selMenu = new MessageActionRow().addComponents( @@ -293,8 +293,7 @@ export default class SettingsCommand extends BushCommand { type: 'string' | 'channel' | 'channel-array' | 'role' | 'role-array' ): Promise<string> => { const feat = await message.guild!.getSetting(setting); - console.debug(feat); - console.debug(type.replace('-array', '')); + switch (type.replace('-array', '') as 'string' | 'channel' | 'role') { case 'string': { return Array.isArray(feat) diff --git a/src/commands/config/joinRoles.ts b/src/commands/config/joinRoles.ts index 9507d4b..0b9ac21 100644 --- a/src/commands/config/joinRoles.ts +++ b/src/commands/config/joinRoles.ts @@ -40,10 +40,8 @@ export default class JoinRolesCommand extends BushCommand { public override async exec(message: BushMessage | BushSlashMessage, { role }: { role: Role }): Promise<unknown> { const joinRoles = await message.guild!.getSetting('joinRoles'); const includes = joinRoles.includes(role.id); - client.console.debug(joinRoles); const newValue = util.addOrRemoveFromArray(includes ? 'remove' : 'add', joinRoles, role.id); await message.guild!.setSetting('joinRoles', newValue); - client.console.debug(joinRoles); return await message.util.reply({ content: `${util.emojis.success} Successfully ${includes ? 'removed' : 'added'} <@&${role.id}> ${ includes ? 'from' : 'to' diff --git a/src/commands/leveling/leaderboard.ts b/src/commands/leveling/leaderboard.ts index e69de29..b8838b7 100644 --- a/src/commands/leveling/leaderboard.ts +++ b/src/commands/leveling/leaderboard.ts @@ -0,0 +1,52 @@ +import { BushCommand, BushMessage, BushSlashMessage, Level } from '@lib'; +import { MessageEmbed } from 'discord.js'; + +export default class LeaderboardCommand extends BushCommand { + public constructor() { + super('leaderboard', { + aliases: ['leaderboard', 'lb'], + category: 'leveling', + description: { + content: 'Allows you to see the users with the highest levels in the server.', + usage: 'leaderboard [page]', + examples: ['leaderboard 5'] + }, + args: [ + { + id: 'page', + type: 'integer', + prompt: { + start: 'What would you like to set your first argument to be?', + retry: '{error} Pick a valid argument.', + optional: true + } + } + ], + slash: true, + slashOptions: [ + { + name: 'page', + description: 'What would you like to set your first argument to be?', + type: 'INTEGER', + required: false + } + ], + channel: 'guild', + clientPermissions: ['SEND_MESSAGES'], + userPermissions: ['SEND_MESSAGES'] + }); + } + + public override async exec(message: BushMessage | BushSlashMessage, args: { page: number }): Promise<unknown> { + if (!message.guild) return await message.util.reply(`${util.emojis.error} This command can only be run in a server.`); + const ranks = (await Level.findAll({ where: { guild: message.guild.id } })).sort((a, b) => b.xp - a.xp); + const mapedRanks = ranks.map( + (val, index) => `\`${index + 1}\` <@${val.user}> - Level ${val.level} (${val.xp.toLocaleString()} xp)` + ); + const chunked = util.chunk(mapedRanks, 25); + const embeds = chunked.map((c) => + new MessageEmbed().setTitle(`${message.guild!.name}'s Leaderboard`).setDescription(c.join('\n')) + ); + return await util.buttonPaginate(message, embeds, null, true, args?.page ?? undefined); + } +} diff --git a/src/commands/leveling/level.ts b/src/commands/leveling/level.ts index 35a0a3e..6640744 100644 --- a/src/commands/leveling/level.ts +++ b/src/commands/leveling/level.ts @@ -1,4 +1,13 @@ -import { BushCommand, BushGuild, BushMessage, BushSlashMessage, BushUser, CanvasProgressBar, Level } from '@lib'; +import { + AllowedMentions, + BushCommand, + BushGuild, + BushMessage, + BushSlashMessage, + BushUser, + CanvasProgressBar, + Level +} from '@lib'; import canvas from 'canvas'; import { MessageAttachment } from 'discord.js'; import got from 'got/dist/source'; @@ -41,17 +50,10 @@ export default class LevelCommand extends BushCommand { private async getImage(user: BushUser, guild: BushGuild): Promise<Buffer> { // I added comments because this code is impossible to read - const [userLevelRow] = await Level.findOrBuild({ - where: { - user: user.id, - guild: guild.id - }, - defaults: { - user: user.id, - guild: guild.id - } - }); - const rank = (await Level.findAll({ where: { guild: guild.id } })).sort((a, b) => b.xp - a.xp); + const guildRows = await Level.findAll({ where: { guild: guild.id } }); + const rank = guildRows.sort((a, b) => b.xp - a.xp); + const userLevelRow = guildRows.find((a) => a.user === user.id); + if (!userLevelRow) throw new Error('User does not have a level'); const userLevel = userLevelRow.level; const currentLevelXP = Level.convertLevelToXp(userLevel); const currentLevelXPProgress = userLevelRow.xp - currentLevelXP; @@ -124,8 +126,18 @@ export default class LevelCommand extends BushCommand { } public override async exec(message: BushMessage | BushSlashMessage, args: { user?: BushUser }): Promise<unknown> { - return await message.reply({ - files: [new MessageAttachment(await this.getImage(args.user ?? message.author, message.guild!), 'level.png')] - }); + const user = args.user ?? message.author; + try { + return await message.util.reply({ + files: [new MessageAttachment(await this.getImage(user, message.guild!), 'level.png')] + }); + } catch (e) { + if (e instanceof Error && e.message === 'User does not have a level') { + return await message.util.reply({ + content: `${util.emojis.error} ${user} does not have a level.`, + allowedMentions: AllowedMentions.none() + }); + } else throw e; + } } } |