I know this is a duplicate but it is a specific issue i think.
I am getting an error while performing a slash command: „The application did not respond“.
What I am trying to do is an Slash Command, that can only be performed by someone with the Administrator Role. The command should be give the User role to the mentioned user.
If someone knows a better way to do that or a GitHub Link etc. I would like to see this as well. Anyway - here is my code:
const DiscordJS = require('discord.js');
const { Routes } = require('discord-api-types/v9');
const { Intents, Client, MessageEmbed } = require('discord.js');
const { token, client_id, guild_id, admin_role_id, user_role_id } = require('./config.json')
const client = new DiscordJS.Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
})
client.on('ready', () => {
console.log('The bot is ready')
const guild = client.guilds.cache.get(guild_id)
let commands
if (guild) {
commands = guild.commands
}
else {
commands = client.application?.commands
}
commands?.create({
name: 'user',
description: 'Makes the mentioned member a user.',
requireRoles: true,
options: [{
name: 'user',
description: 'The user the role should be given to.',
required: true,
type: DiscordJS.Constants.ApplicationCommandOptionTypes.USER
}]
})
})
client.on('interactionCreate, a', async (interaction) => {
if (!interaction.isCommand()) {
return
}
const { commandName, options } = interaction
if (commandName === 'user') {
const user_id = options.get('user')?.value;
const guild = client.guilds.cache.get(guild_id);
if (!guild) {
return console.log(`Can't find the guild with ID ${server_id}.`);
}
guild.members.fetch(user_id)
.then(member => {
console.log(member.roles.cache)
const embedSuccess = new MessageEmbed()
.setColor('#5ee067')
.setDescription('<@' + user_id + '> was given the <@&' + user_role_id + '> role.');
const embedUserInfo = new MessageEmbed()
.setDescription('Dear <@' + user_id + '>, \n you are able…');
const embedFailure = new MessageEmbed()
.setColor('#ffa2a2')
.setDescription("Could not perform command because the user that requested it doesn't have the required roles to execute it.")
if (interaction.user.roles.cache.has(admin_role_id)) {
const member = interaction.options.getMember('target');
member.roles.add(user_role_id)
interaction.reply({
content: '<@' + user_id + '>',
embeds: [ embedSuccess, embedUserInfo ]
})
}
else {
interaction.reply({
embeds: [ embedFailure ]
})
}
})
.catch(console.error);
}
})
client.login(token)
Thanks.