diff --git a/src/commands/index.ts b/src/commands/index.ts index d5e75ca..92f2449 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -37,6 +37,7 @@ import { useOnboardingQuit } from './onboarding/onboarding_quit' import { useOnboardingWhy } from './onboarding/onboarding_why' import { useSpecial } from './special' import { useDynamicVoiceTitle } from './dynamic_voice/dynamic_voice_title' +import { useMedal } from './medal' const registerHooks = (client: He4rtClient, commands: Command[]) => { commands.forEach(([data, cb]) => { @@ -82,6 +83,7 @@ export const registerCommands = async ({ client, rest }: Context) => { useOnboardingQuit(), useOnboardingWhy(), useSpecial(), + useMedal() // useReputation() ]) diff --git a/src/commands/medal.ts b/src/commands/medal.ts new file mode 100644 index 0000000..acac08b --- /dev/null +++ b/src/commands/medal.ts @@ -0,0 +1,43 @@ +import { ChannelType, CommandInteractionOption, GuildMember, SlashCommandBuilder } from 'discord.js' +import { Command } from '@/types' +import { MEDAL } from '@/defines/commands.json' +import medals from '@/defines/medals.json' +import { MEDAL_OPTION, NOT_EXIST, DO_NOT_HAVE, ALREADY, SUCCESS } from '-/commands/medal.json' +import { hasRole } from '../utils' + +export const useMedal = (): Command => { + const data = new SlashCommandBuilder() + .setName(MEDAL.TITLE) + .setDescription(MEDAL.DESCRIPTION) + .setDMPermission(false) + .addStringOption((option) => option.addChoices({name: medals[0].name, value: medals[0].medal_role_id}).setName('medal').setDescription(MEDAL_OPTION).setRequired(true)) + + return [ + data, + async (interaction, _) => { + const member = interaction.member as GuildMember + + const medalArg = interaction.options.get('medal') as CommandInteractionOption + const medalSelected = medals.find(medal => medal.medal_role_id === medalArg.value); + + if(!medalSelected) { + await interaction.reply({ content: NOT_EXIST, ephemeral: true }) + return + } + if(!medalSelected.members_id.includes(member.id)) { + await interaction.reply({ content: DO_NOT_HAVE, ephemeral: true }) + return + } + if(hasRole(member, medalSelected.medal_role_id)) { + await interaction.reply({ content: ALREADY, ephemeral: true }) + return + } + + await member.roles.remove(medals.map(medal => medal.medal_role_id).filter(role_id => role_id !== medalSelected.medal_role_id)) + await member.roles.add(medalSelected.medal_role_id) + await interaction.reply({ content: SUCCESS, ephemeral: true }) + + return + }, + ] +} diff --git a/src/defines/commands.json b/src/defines/commands.json index 1851833..21316b1 100644 --- a/src/defines/commands.json +++ b/src/defines/commands.json @@ -154,5 +154,9 @@ "DYNAMIC_VOICE_TITLE": { "TITLE": "sala-titulo", "DESCRIPTION": "(DONO): Altere o título da sala por outro." + }, + "MEDAL": { + "TITLE": "medalha", + "DESCRIPTION": "Selecione sua medalha de contribuição com a comunidade." } } diff --git a/src/defines/localisation/commands/medal.json b/src/defines/localisation/commands/medal.json new file mode 100644 index 0000000..339249d --- /dev/null +++ b/src/defines/localisation/commands/medal.json @@ -0,0 +1,7 @@ +{ + "MEDAL_OPTION": "Selecione sua medalha que deseja ficar ao lado do seu nick.", + "NOT_EXIST": "Medalha não encontrada.", + "DO_NOT_HAVE": "Você não possui essa medalha. :(", + "ALREADY": "Você já está com essa medalha selecionada.", + "SUCCESS": "Medalha selecionada com sucesso." +} diff --git a/src/defines/medals.json b/src/defines/medals.json new file mode 100644 index 0000000..081d9a8 --- /dev/null +++ b/src/defines/medals.json @@ -0,0 +1,7 @@ +[ + { + "name": "He4rt", + "medal_role_id": "1107790862869614684", + "members_id": ["426540070028443688", "418886000144678922", "279344988532506624", "259446744675254273", "241070790383108096", "360275732099760138", "155085013342224384", "253744065043103747", "310240335924297728", "349402457178374144", "763563330849210368", "480894052787683348", "232906166059925506", "295011455802015745", "315633936942235649", "415553620268941332", "466357449939484675", "283347054426718221", "410870008919621635", "264931012272717824", "1014231037036994673", "547097212770058243", "290680405106098176", "522080894513709089", "261991713541718017", "166340136399798272", "228956489690251264"] + } +] \ No newline at end of file