Reputation: 15
When I try to run the command on the powershell termianl (node .\index.js\) I get the same error every time. I changed the token multiple times but still got the same error. I run the command on the right root directory, so I don't know what to do.
Code:
import { config } from 'dotenv';
import { Client, GatewayIntentBits, Routes } from 'discord.js';
import { REST } from '@discordjs/rest';
config();
const TOKEN = process.env.BOT_TOKEN_CLOUD;
const CLIENT_ID = process.env.CLIENT_ID;
const GUILD_ID = process.env.GUILD_ID;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
]
});
const rest = new REST({ version: '10' }).setToken('TOKEN');
client.on('ready', () => {console.log(`${client.user.tag} has logged in!`);});
async function main() {
const commands = [
{
name: 'testcommand',
description: 'test command'
},
];
try {
console.log('Started refreshing application (/) commands.');
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {
body: commands,
});
client.login(TOKEN);
} catch (err) {
console.log(err);
}
}
main();
Error:
DiscordAPIError[0]: 401: Unauthorized
at SequentialHandler.runRequest (file:///C:/Users/Name/Documents/djs-v14/node_modules/@discordjs/rest/dist/lib/handlers/SequentialHandler.mjs:283:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async SequentialHandler.queueRequest (file:///C:/Users/Name/Documents/djs-v14/node_modules/@discordjs/rest/dist/lib/handlers/SequentialHandler.mjs:95:14)
at async REST.request (file:///C:/Users/Name/Documents/djs-v14/node_modules/@discordjs/rest/dist/lib/REST.mjs:48:22)
at async main (file:///C:/Users/Name/Documents/djs-v14/index.js:32:9) {
rawError: { message: '401: Unauthorized', code: 0 },
code: 0,
status: 401,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/CLIENT_ID/guilds/948742790421053482/commands',
requestBody: { files: undefined, json: [ [Object] ] }
}
Upvotes: 0
Views: 4024
Reputation: 26
You need to use the specific token for your bot instead of 'TOKEN' string
const rest = new REST({ version: '10' }).setToken('TOKEN'); here, .setToken() should be like .setToken(TOKEN)
Upvotes: 1
Reputation: 320
Almost certain you forgot the ApplicationCommands scope(remember bot as well) when adding a bot to the server. Ensure you add that scope in the developer portal. Another possible reason is your client ID not matching the token.
Upvotes: 1