From 7a7ffcc8056dc6ce97105cffda0b59c710a39b3b Mon Sep 17 00:00:00 2001 From: janderedev Date: Sat, 9 Apr 2022 13:45:51 +0200 Subject: [PATCH] add /avatar and /nick --- bot/src/bot/commands/avatar.ts | 64 ++++++++++++++++++++++++++++++++++ bot/src/bot/commands/nick.ts | 58 ++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 bot/src/bot/commands/avatar.ts create mode 100644 bot/src/bot/commands/nick.ts diff --git a/bot/src/bot/commands/avatar.ts b/bot/src/bot/commands/avatar.ts new file mode 100644 index 0000000..c96b1ea --- /dev/null +++ b/bot/src/bot/commands/avatar.ts @@ -0,0 +1,64 @@ +import { Member } from "@janderedev/revolt.js/dist/maps/Members"; +import axios from "axios"; +import CommandCategory from "../../struct/commands/CommandCategory"; +import SimpleCommand from "../../struct/commands/SimpleCommand"; +import MessageCommandContext from "../../struct/MessageCommandContext"; +import { hasPerm, isModerator, NO_MANAGER_MSG, parseUser } from "../util"; + +export default { + name: 'avatar', + aliases: [ 'pfp' ], + description: 'Get or clear someone\'s avatar', + category: CommandCategory.Moderation, + run: async (message: MessageCommandContext, args: string[]) => { + try { + const targetStr = args.shift(); + if (!targetStr) return message.reply('No target user specified.'); + const targetUser = await parseUser(targetStr); + if (!targetUser) return message.reply('Couldn\'t find the specified user.'); + const target = await message.channel?.server?.fetchMember(targetUser); + if (!target) return message.reply('The target is not part of this server.'); + + if (args[0]?.toLowerCase() == 'reset' + || args[0]?.toLowerCase() == 'clear') { + // Clear server avatar + if (!message.member) return; + if (!hasPerm(message.member, 'RemoveAvatars') + && !await isModerator(message)) return message.reply(NO_MANAGER_MSG); + + if (!target.avatar) { + await message.reply(`\`@${targetUser.username}\` does not currently have an avatar set for this server.`); + } else { + await clearAvatar(target); + await message.reply(`\`@${targetUser.username}\`'s server avatar has been cleared.`); + } + } else { + // Print server and global avatar + + await message.reply( + `### \`@${targetUser.username}\`'s avatar\n` + + (targetUser.avatar ? `[\\[Global\\]](<${targetUser.generateAvatarURL()}>)` : '[No global avatar]') + + ' | ' + + (target.avatar ? `[\\[Server\\]](<${target.generateAvatarURL()}>)` : '[No server avatar]') + ); + } + } catch(e) { + console.error('' + e); + message.reply('Something went wrong: ' + e); + } + } +} as SimpleCommand; + +async function clearAvatar(member: Member) { + await axios.patch( + `${member.client.apiURL}/servers/${member.server!._id}/members/${member._id.user}`, + { + remove: "Avatar", + }, + { + headers: { + 'x-bot-token': process.env['BOT_TOKEN']! + } + } + ); +} diff --git a/bot/src/bot/commands/nick.ts b/bot/src/bot/commands/nick.ts new file mode 100644 index 0000000..671a2e7 --- /dev/null +++ b/bot/src/bot/commands/nick.ts @@ -0,0 +1,58 @@ +import { Member } from "@janderedev/revolt.js/dist/maps/Members"; +import axios from "axios"; +import CommandCategory from "../../struct/commands/CommandCategory"; +import SimpleCommand from "../../struct/commands/SimpleCommand"; +import MessageCommandContext from "../../struct/MessageCommandContext"; +import { hasPerm, isModerator, NO_MANAGER_MSG, parseUser } from "../util"; + +export default { + name: 'nick', + aliases: [ 'setnick' ], + description: 'Set or clear someone\'s nickname', + category: CommandCategory.Moderation, + run: async (message: MessageCommandContext, args: string[]) => { + try { + if (!message.member) return; + if (!hasPerm(message.member, 'ManageNicknames') + && !await isModerator(message)) return message.reply(NO_MANAGER_MSG); + + const targetStr = args.shift(); + if (!targetStr) return message.reply('No target user specified.'); + const targetUser = await parseUser(targetStr); + if (!targetUser) return message.reply('Couldn\'t find the specified user.'); + const target = await message.channel?.server?.fetchMember(targetUser); + if (!target) return message.reply('The target is not part of this server.'); + + const newName = args.join(' '); + + if (!newName) { + // Reset name + await setNick(target, null); + await message.reply(`Nickname of \`@${targetUser.username}\` has been cleared.`); + } else { + // Set new name + await setNick(target, newName); + await message.reply(`Nickname of \`@${targetUser.username}\` has been changed to ` + + `"${newName.replace(/`/g, '\\`')}".`); + } + } catch(e) { + console.error('' + e); + message.reply('Something went wrong: ' + e); + } + } +} as SimpleCommand; + +async function setNick(member: Member, newName: string|null) { + await axios.patch( + `${member.client.apiURL}/servers/${member.server!._id}/members/${member._id.user}`, + { + nickname: newName || undefined, + remove: !newName ? "Nickname" : undefined, + }, + { + headers: { + 'x-bot-token': process.env['BOT_TOKEN']! + } + } + ); +}