401 error on get request

I'm trying to get a list of all of my guild application commands for this bot but I'm getting a 401 error on this rest request and i haven't the slightest idea why. Code:
import { REST, Routes } from 'discord.js';

const rest = new REST().setToken(process.env.TEMPLATE_TOKEN);
var data;

try{
data = await rest.get(
Routes.applicationGuildCommands(
process.env.TEMPLATE_CLIENT_ID,
process.env.TEMPLATE_GUILD_ID
)
);
console.log(data);
}
catch(error){
console.log(error);
}
import { REST, Routes } from 'discord.js';

const rest = new REST().setToken(process.env.TEMPLATE_TOKEN);
var data;

try{
data = await rest.get(
Routes.applicationGuildCommands(
process.env.TEMPLATE_CLIENT_ID,
process.env.TEMPLATE_GUILD_ID
)
);
console.log(data);
}
catch(error){
console.log(error);
}
Error:
DiscordAPIError[0]: 401: Unauthorized
at handleErrors (C:\code\ts-discord-bot\node_modules\@discordjs\rest\dist\index.js:722:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (C:\code\ts-discord-bot\node_modules\@discordjs\rest\dist\index.js:1120:23)
at async SequentialHandler.queueRequest (C:\code\ts-discord-bot\node_modules\@discordjs\rest\dist\index.js:953:14)
at async _REST.request (C:\code\ts-discord-bot\node_modules\@discordjs\rest\dist\index.js:1266:22)
at async file:///C:/code/ts-discord-bot/testing.js:8:12 {
requestBody: { files: undefined, json: undefined },
rawError: { message: '401: Unauthorized', code: 0 },
code: 0,
status: 401,
method: 'GET',
url: 'https://discord.com/api/v10/applications/1128470111607607307/guilds/667849155627974656/commands'
}
DiscordAPIError[0]: 401: Unauthorized
at handleErrors (C:\code\ts-discord-bot\node_modules\@discordjs\rest\dist\index.js:722:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (C:\code\ts-discord-bot\node_modules\@discordjs\rest\dist\index.js:1120:23)
at async SequentialHandler.queueRequest (C:\code\ts-discord-bot\node_modules\@discordjs\rest\dist\index.js:953:14)
at async _REST.request (C:\code\ts-discord-bot\node_modules\@discordjs\rest\dist\index.js:1266:22)
at async file:///C:/code/ts-discord-bot/testing.js:8:12 {
requestBody: { files: undefined, json: undefined },
rawError: { message: '401: Unauthorized', code: 0 },
code: 0,
status: 401,
method: 'GET',
url: 'https://discord.com/api/v10/applications/1128470111607607307/guilds/667849155627974656/commands'
}
I'm loading the environment variables using dotenv (which I've confirmed loads them). I've tried regenerating the token and giving my bot application-commands scope in OAuth but neither worked. I'm using node v21.5.0 and djs v14.14.1. Probably an easy answer, but i appreciate any guidance you can give, I'm quite new to this bot stuff :D
4 Replies
d.js toolkit
d.js toolkit10mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
fyrebirdd
fyrebirddOP10mo ago
heres the full code, i just left out the dotenv because i didnt think it was relevant to the issue
import { REST, Routes } from 'discord.js';
import 'dotenv/config.js';


try{
const rest = new REST().setToken(process.env.TEMPLATE_TOKEN);
var data;
data = await rest.get(
Routes.applicationCommands(process.env.TEMPLATE_CLIENT_ID)
);
console.log(data);
}
catch(error){
console.log(error);
}
import { REST, Routes } from 'discord.js';
import 'dotenv/config.js';


try{
const rest = new REST().setToken(process.env.TEMPLATE_TOKEN);
var data;
data = await rest.get(
Routes.applicationCommands(process.env.TEMPLATE_CLIENT_ID)
);
console.log(data);
}
catch(error){
console.log(error);
}
I confirmed it by console logging my whole process.env object before the try catch block and all of the variables in my env file appear in the output ive reset the token twice and it still hasn't worked. I've also made sure the bot is in the guild that im quering. I've even tried it with global commands and it still 401s yep, they are from the same application in the dev portal
fyrebirdd
fyrebirddOP10mo ago
heres the 4 entires i add to the env outputed with this.
import { REST, Routes } from 'discord.js';
import 'dotenv/config.js';

console.log(process.env);

try{
//.... other code from above
import { REST, Routes } from 'discord.js';
import 'dotenv/config.js';

console.log(process.env);

try{
//.... other code from above
No description
fyrebirdd
fyrebirddOP10mo ago
also ignore my horrid touchpad paint job :Hehe: they've been the same because ive had this bot running in the past, and i've even put commands on the bot before. For whatever reason it just works now though so idk, maybe i did do something wrong
Want results from more Discord servers?
Add your server