DiscordJS 403 Error Missing Access Error when trying to add Slash Commands - discord

I am following the docs at DiscrodJS step by step and have been able to invite my bot just fine to my test server. My problem comes in when I try to add the slash commands.
Here is my index.js file.
const { Client, GatewayIntentBits } = require('discord.js')
require('dotenv').config()
const client = new Client({
intents: [
GatewayIntentBits.GuildMessages,
],
})
client.once('ready', () => {
console.log('Ready!')
})
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return
const { commandName } = interaction
const command = interaction.client.commands.get(commandName)
if (!command) {
console.error(`No command matching ${commandName} was found.`)
return
}
try {
await command.execute(interaction)
}
catch (error) {
console.error(error)
await interaction.reply({ ontent: 'There was an error while executing this command', ephemeral: true })
}
})
client.login(process.env.DISCORD_PRIVATE_TOKEN)
Here is my deploy-commands.js file.
const fs = require('node:fs')
const { Routes } = require('discord.js')
const { REST } = require('#discordjs/rest')
require('dotenv').config()
const {
DISCORD_PRIVATE_TOKEN,
DISCORD_CLIENT_ID,
DISCORD_GUILD_ID,
} = process.env
const commands = []
// Grab all the command files from the commands directory you created earlier
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'))
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const command = require(`./commands/${file}`)
commands.push(command.data.toJSON())
}
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(DISCORD_PRIVATE_TOKEN)
// and deploy your commands!
const deployCommand = async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`)
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(DISCORD_CLIENT_ID, DISCORD_GUILD_ID),
{ body: commands },
)
console.log(`Successfully reloaded ${data.length} application (/) commands.`)
}
catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error)
}
}
deployCommand()
There error that I get back from this is the following:
requestBody: { files: undefined, json: [ [Object] ] },
rawError: { message: 'Missing Access', code: 50001 },
code: 50001,
status: 403,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/[client_id]/guilds/[guild_id]/commands'
}
In the bot settings
I've set auth method to In-app Authorization.
I've set the scope to bot and applications.commands then included Use Slash Commands (also tried this with ALL including Administrator bot permissions.
I've also done this through the URL Generator path.
In my discord server
I've put my Discord server into Developer mode to test my bot.
I've invited my bot to my test server.
In my code
I've ran the one time script for deploy-commands.js.

Related

Why isn't my slash command working? discord.js

I was trying to create a bot that with a slash command can create a azure ad account and message it to the user. However when I run the code it doesn't create a slash command. How would I fix this.
Code:
// import dependencies
const Discord = require('discord.js');
const axios = require('axios');
// create a new Discord client
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
],
});
// listen for the ready event
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
// listen for the interactionCreate event
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'create-account') {
// retrieve user input from slash command
const displayName = interaction.options.getString('displayname');
const password = interaction.options.getString('password');
// retrieve sensitive information from Replit secrets
const tenantId = process.env.AZURE_TENANT_ID;
const clientId = process.env.AZURE_CLIENT_ID;
const clientSecret = process.env.AZURE_CLIENT_SECRET;
try {
// create Azure AD account using Microsoft Graph API
const response = await axios.post(`https://graph.microsoft.com/v1.0/${tenantId}/users`, {
displayName: displayName,
passwordProfile: {
password: password
},
accountEnabled: true
}, {
headers: {
'Authorization': `Bearer ${await getAccessToken(clientId, clientSecret, tenantId)}`
}
});
// send success message to Discord
await interaction.reply(`Account with display name "${displayName}" created on Azure AD.`);
// log created account to Discord webhook
const webhook = new Discord.WebhookClient({
id: process.env.DISCORD_WEBHOOK_ID,
token: process.env.DISCORD_WEBHOOK_TOKEN
});
await webhook.send(`Account created on Azure AD:\nDisplay name: ${displayName}\nObject ID: ${response.data.id}`);
} catch (error) {
console.error(error);
// send error message to Discord
await interaction.reply('An error occurred while creating the account. Please try again later.');
}
}
});
// start the Discord client
client.login(process.env.DISCORD_TOKEN);
// helper function to get access token for Microsoft Graph API
async function getAccessToken(clientId, clientSecret, tenantId) {
const response = await axios.post(`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`, {
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
scope: 'https://graph.microsoft.com/.default'
});
return response.data.access_token;
}
I want it to create a slash command which I can run that'll let me create an azure ad account but the slash command does not show up when I try using it.

DiscordAPIError: Unknown application command

When I try to execute a slash command it crashes with an 404 error.
I already tried to delete all commands but it didn't work.
Here's the code I'm using
const commands = [
{
name: 'ping',
description: 'Replies with Pong!'
}];
const rest = new REST({ version: '9' }).setToken('token');
(async () => {
try {
console.log('Reloading slash commands');
await rest.put(
Routes.applicationGuildCommands("client_id", "server_id"),
{ body: commands },
);
console.log("Reloaded slash commands");
} catch (error) {
console.error(error);
}
})();
Here's the error
node_modules/discord.js/src/rest/RequestHandler.js:350
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Unknown application command
at RequestHandler.execute (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
at async GuildApplicationCommandManager.fetch (/home/container/node_modules/discord.js/src/managers/ApplicationCommandManager.js:93:23)
at async Client.<anonymous> (/home/container/index.js:101:17) {
method: 'get',
path: '/applications/client_id/guilds/guild_id/commands/command_id',
code: 10063,
httpStatus: 404,
requestData: { json: undefined, files: [] }
}
if you want to register your Slash Commands with the moderately recent Discord.js Rest library you have to use the builder library as well. You are also not passing JSON to the body you are passing an Array of Objects.
From Discord.js Guide on Making Slash Commands
const { SlashCommandBuilder } = require('#discordjs/builders');
const { REST } = require('#discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with pong!'),
new SlashCommandBuilder().setName('server').setDescription('Replies with server info!'),
new SlashCommandBuilder().setName('user').setDescription('Replies with user info!'),
]
.map(command => command.toJSON());
const rest = new REST({ version: '9' }).setToken(token);
rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);

Slash commands, Echo and Hello randomly added to my bot

For some reason, when using/checking the available slash commands of my bot. I have an echo and hello command, that I have not coded in AT ALL, that just appears. I was wondering if there is anything I can do to remove these?
Here are my packages I am using for this project
"#discordjs/builders": "^0.9.0",
"#discordjs/rest": "^0.1.0-canary.0",
"discord-api-types": "^0.25.2",
"discord.js": "^13.3.1",
"dotenv": "^10.0.0",
"fs": "^0.0.1-security",
"yarn": "^1.22.17"
Here is the interaction Creator
module.exports = {
name: 'interactionCreate',
async execute(interaction, client) {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
});
}
},
};
Here is the command handler
const {
REST
} = require('#discordjs/rest');
const {
Routes
} = require('discord-api-types/v9');
const fs = require('fs');
// Place your client and guild ids here
const clientId = '904224432338399243';
const guildId = '922603289898528768';
module.exports = (client) => {
client.handleCommands = async(commandFolders, path) => {
client.commandArray = [];
for (folder of commandFolders) {
const commandFiles = fs.readdirSync(`${path}/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`../commands/${folder}/${file}`);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
client.commandArray.push(command.data.toJSON());
}
}
const rest = new REST({
version: '9'
}).setToken(process.env.token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: client.commandArray },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
};
};
Mintels!
Try those solutions:
Registering your bot in a single guild (not globally)
Kicking the bot out of the guild and re-inviting it
Leave your code running for some time
Hope this helped and have a great day!

Trouble making slash commands global

Since my bots new update is releasing soon I have been trying to figure out how to make the slash commands go global. I keep hitting bumps and I have no idea what to do next. Below is some of my code of creating the commands.
ready.js
const {prefix} = require("../../botconfig.json")
const {createCmd} = require("../../dataHandler.js")
module.exports = bot => {
console.log(`${bot.user.username} is online`);
setInterval(() => bot.user.setActivity(`!help | Switch Support`, { type: "WATCHING" }), 15000)
createCmd(bot, '781631298749726730') //second param is guildId
};
DataHandler(Creates the data for the comamnds)
async function createCmd(Client, guildId) {
const data = [
//ping cmd
{
name: 'test',
description: 'test slash command'
}
]
await Client.guilds.cache.get(guildId).commands.set(data)
}
module.exports = {createCmd}
How will I use these to make the commands global so every server can use these commands when the update is released.
See the documentation here. All the code below is taken from there.
In your createCmd function add
const rest = new REST({ version: '9' }).setToken("your discord token");
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationCommands("REPLACE_WITH_CLIENT_ID"),
{ body: data },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
Require these at the beginning of your file
const { REST } = require('#discordjs/rest');
const { Routes } = require('discord-api-types/v9');
Just a note: Global commands take up to an hour to refresh across Discord. Keep that in mind when updating.

My discord bot isn't working after 13.0.0

I am trying out discord.js version 13.1.0 and I am following discord's guide on how to setup a bot. I have followed all the steps. When I have the bot running, and I send a message or a command into the server, nothing gets printed to the console. Any ideas?
My code:
const { Client, Intents } = require("discord.js");
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.once("ready", () => {
console.log("Ready!");
});
client.on("interactionCreate", (interaction) => {
console.log(interaction);
});
client.login(process.env.TOKEN);
From the Discord Developer Portal:
An Interaction is the message that your application receives when a user uses an application command or a message component.
By an application command is meant a special type of command, that you have to register against the Discord API. To learn more about application commands, read this.
From the discord.js docs main page, how to register an application (slash) command:
const { REST } = require('#discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const commands = [{
name: 'ping',
description: 'Replies with Pong!'
}];
const rest = new REST({ version: '9' }).setToken('token');
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
However if you are looking for a way to reply to (or just capture) a normal message sent in a TextChannel, you are probably looking for the messageCreate event.
client.on("messageCreate", (message) => {
console.log(message);
});
Also note that to receive messageCreate event, you need GUILD_MESSAGES intent.
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });

Resources