我试图用slash命令(Lastest Discord JS)构建一个discord bot,但遇到了一些问题。每当我运行我的bot时,它都会显示discord API错误未授权401。
下面是我的bot代码:
import { config } from 'dotenv';
import {
ActionRowBuilder,
Client,
GatewayIntentBits,
InteractionType,
ModalBuilder,
Routes,
// SelectMenuBuislder,
TextInputBuilder,
TextInputStyle,
} from 'discord.js';
import { REST } from '@discordjs/rest';
import pingCommand from './commands/ping.js'
config();
const TOKEN = process.env.BOT_TOKEN
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!`));
client.on('interactionCreate', (interaction) => {
if (interaction.isChatInputCommand()) {
console.log('Chat Command');
if (interaction.commandName === 'ping') {
interaction.reply("Pong")
}
}
});
async function main() {
const commands = [
pingCommand
];
try {
console.log('Started refreshing application (/) commands.');
await rest.put(Routes.applicationCommands(CLIENT_ID), {
body: commands,
});
client.login(TOKEN);
} catch (err) {
console.log(err);
}
}
main();
my ping command (for test) :
import { SlashCommandBuilder } from '@discordjs/builders';
const pingCommand = new SlashCommandBuilder()
.setName('ping')
.setDescription('ping')
.addRoleOption((option) =>
option
.setName('eh')
.setDescription('huh')
.setRequired(false)
);
export default pingCommand.toJSON();
我试着只使用应用程序命令模块来防止这种情况,因为它只需要客户端id而不是客户端和公会id(从applicaitonguildcommand开始),但是问题仍然是一样的。
1条答案
按热度按时间4c8rllxm1#
您可能是从OAuth2页面而不是Bot页面获取token。
转到:https://discord.com/developers/applications/{YOUR_APP_ID}/bot并单击按钮
Reset token
。令牌长度将采用以下格式:xxxxxxxxxxxxxxxxxxxxxxxxxx.yyyyyy.zzzzzzzzzzzzzzzzzzzzzzzzzzzz-qqqqqqqqq
.