aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-08-30 11:09:36 -0400
committerIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-08-30 11:09:36 -0400
commitfb73d1b421638ea695eeafeee2215a1e923c4d4a (patch)
tree57c631f590478014f9ba1739a0d0c19ec05df09d /src
parent48661511dd485957506e7900c8e86916aa3ea3af (diff)
downloadtanzanite-fb73d1b421638ea695eeafeee2215a1e923c4d4a.tar.gz
tanzanite-fb73d1b421638ea695eeafeee2215a1e923c4d4a.tar.bz2
tanzanite-fb73d1b421638ea695eeafeee2215a1e923c4d4a.zip
leaderboard command and some fixes
Diffstat (limited to 'src')
-rw-r--r--src/commands/config/config.ts41
-rw-r--r--src/commands/config/joinRoles.ts2
-rw-r--r--src/commands/leveling/leaderboard.ts52
-rw-r--r--src/commands/leveling/level.ts42
-rw-r--r--src/lib/extensions/discord-akairo/BushClientUtil.ts5
-rw-r--r--src/lib/models/Guild.ts18
6 files changed, 111 insertions, 49 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;
+ }
}
}
diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts
index 4d565e4..9a5a07f 100644
--- a/src/lib/extensions/discord-akairo/BushClientUtil.ts
+++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts
@@ -673,7 +673,8 @@ export class BushClientUtil extends ClientUtil {
message: BushMessage | BushSlashMessage,
embeds: MessageEmbed[],
text: string | null = null,
- deleteOnExit?: boolean
+ deleteOnExit?: boolean,
+ startOn?: number
): Promise<void> {
const paginateEmojis = this.#paginateEmojis;
if (deleteOnExit === undefined) deleteOnExit = true;
@@ -687,7 +688,7 @@ export class BushClientUtil extends ClientUtil {
});
const style = Constants.MessageButtonStyles.PRIMARY;
- let curPage = 0;
+ let curPage = startOn ? startOn - 1 : undefined ?? 0;
if (typeof embeds !== 'object') throw new Error('embeds must be an object');
const msg = (await message.util.reply({
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
diff --git a/src/lib/models/Guild.ts b/src/lib/models/Guild.ts
index 4a5ede4..1974725 100644
--- a/src/lib/models/Guild.ts
+++ b/src/lib/models/Guild.ts
@@ -41,49 +41,49 @@ export interface GuildModelCreationAttributes {
export const guildSettingsObj = {
prefix: {
name: 'Prefix',
- description: 'description goes here',
+ description: 'The phrase required to trigger text commands in this server.',
type: 'string',
configurable: true
},
autoPublishChannels: {
name: 'Auto Publish Channels',
- description: 'description goes here',
+ description: 'Channels were every message is automatically published.',
type: 'channel-array',
configurable: true
},
welcomeChannel: {
name: 'Welcome Channel',
- description: 'description goes here',
- type: 'channel-array',
+ description: 'The channel where the bot will send join and leave message.',
+ type: 'channel',
configurable: true
},
muteRole: {
name: 'Mute Role',
- description: 'description goes here',
+ description: 'The role assigned when muting someone.',
type: 'role',
configurable: true
},
punishmentEnding: {
name: 'Punishment Ending',
- description: 'description goes here',
+ description: 'The message after punishment information to a user in a dm.',
type: 'string',
configurable: true
},
lockdownChannels: {
name: 'Lockdown Channels',
- description: 'description goes here',
+ description: 'Channels that are locked down when a mass lockdown is specified.',
type: 'channel-array',
configurable: false // not implemented yet
},
joinRoles: {
name: 'Join Roles',
- description: 'description goes here',
+ description: 'Roles assigned to users on join who do not have sticky role information.',
type: 'role-array',
configurable: true
},
automodLogChannel: {
name: 'Automod Log Channel',
- description: 'description goes here',
+ description: 'The channel where all automod information is sent.',
type: 'channel',
configurable: true
}