feat(arabot): add ability to check other user for ranks

This commit is contained in:
smyalygames 2023-03-15 00:12:27 +00:00
parent f304b9ecac
commit f1453d5691

View File

@ -17,7 +17,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Command, RegisterBehavior } from '@sapphire/framework'; import { Args, Command, RegisterBehavior } from '@sapphire/framework';
import type { User, Guild, Message } from 'discord.js'; import type { User, Guild, Message } from 'discord.js';
import { EmbedBuilder } from 'discord.js'; import { EmbedBuilder } from 'discord.js';
import { getRank } from '#utils/database/xp'; import { getRank } from '#utils/database/xp';
@ -36,7 +36,9 @@ export class RankCommand extends Command {
registry.registerChatInputCommand( registry.registerChatInputCommand(
(builder) => builder (builder) => builder
.setName(this.name) .setName(this.name)
.setDescription(this.description), .setDescription(this.description)
.addUserOption((option) => option.setName('user')
.setDescription('User to show rank for')),
{ {
behaviorWhenNotIdentical: RegisterBehavior.Overwrite, behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
}, },
@ -45,7 +47,8 @@ export class RankCommand extends Command {
// Command run // Command run
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const { user, guild } = interaction; let user = interaction.options.getUser('user');
const { guild } = interaction;
if (guild === null) { if (guild === null) {
await interaction.reply({ await interaction.reply({
@ -55,6 +58,10 @@ export class RankCommand extends Command {
return; return;
} }
if (user === null) {
user = interaction.user;
}
await interaction.deferReply(); await interaction.deferReply();
const info = await this.rank(user, guild); const info = await this.rank(user, guild);
@ -65,16 +72,16 @@ export class RankCommand extends Command {
}); });
} }
public async messageRun(message: Message) { public async messageRun(message: Message, args: Args) {
const user = message.member?.user; let user: User;
const { guild } = message; try {
user = await args.pick('user');
if (user === undefined) { } catch {
await message.react('❌'); user = message.author;
await message.reply('Could not find your user!');
return;
} }
const { guild } = message;
if (guild === null) { if (guild === null) {
await message.react('❌'); await message.react('❌');
await message.reply('Could not find the guild!'); await message.reply('Could not find the guild!');
@ -99,12 +106,16 @@ export class RankCommand extends Command {
success: false, success: false,
}; };
const member = guild.members.cache.get(user.id); let member = guild.members.cache.get(user.id);
if (member === undefined) { if (member === undefined) {
info.message = 'Could not find your guild member!'; member = await guild.members.fetch(user.id)
.catch(() => undefined);
if (member === undefined) {
info.message = 'The user is not on this server!';
return info; return info;
} }
}
const rank = await getRank(user.id); const rank = await getRank(user.id);