I have a basic role creation bot that allows users to create and manage their own role. When the command is executed, discord responds with the "application failed to respond" error. When executed a second time, the command works, but creates two of the same roles. What am I missing here?
There are no errors in the console just the one listed above in the discord client.
Code
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('createtribe')
.setDescription('Creates a custom role for you and your tribemates')
.addStringOption(option =>
option.setName('tribename')
.setDescription('The name of your tribe')
.setRequired(true))
.addStringOption(option =>
option.setName('rolecolor')
.setDescription('The color you wish your role to have')
.setRequired(true)),
async execute(interaction, command) {
const guild = interaction.guild;
const role = guild.roles.cache.find(role => role.name === interaction.options.getString("tribename"));
const member = interaction.member;
if (command ==="createtribe");
interaction.guild.roles.create({name: interaction.options.getString("tribename")});
await member.roles.add(role);
role.edit({name: interaction.options.getString("tribename"), color: interaction.options.getString("rolecolor")});
await interaction.reply({ content: 'Your tribe has been successfully registered!', ephemeral: true })
}
}
I've tried removing the following lines, but nothing happened
if (command ==="createtribe");
interaction.guild.roles.create({name: interaction.options.getString("tribename")});
await member.roles.add(role);
role.edit({name: interaction.options.getString("tribename"), color: interaction.options.getString("rolecolor")});
Currently, your code searches for a role with the name of the tribe, creates a role with the name of the tribe, and adds the role that it searched for. This means that if the role doesn't already exist, you create the role and add nothing, and if it does exist, you create a duplicate role and add the first role. To fix this, add some additional logic to handle these cases.
async execute(interaction, command) {
const guild = interaction.guild;
const member = interaction.member;
if (command ==="createtribe");
let role = guild.roles.cache.find(role => role.name === interaction.options.getString("tribename"));
if (role === undefined) {
// If the role doesn't exist:
role = await guild.roles.create({name: interaction.options.getString("tribename")});
await role.edit({name: interaction.options.getString("tribename"), color: interaction.options.getString("rolecolor")});
await member.roles.add(role);
await interaction.reply({ content: 'Your tribe has been successfully registered!', ephemeral: true })
} else {
// If the role already exists:
await interaction.reply({ content: 'This tribe already exists.', ephemeral: true })
}
}
}