gpt4 book ai didi

javascript - Discord API 对于使用机器人 token 的所有查询返回 401

转载 作者:行者123 更新时间:2023-12-02 22:42:12 25 4
gpt4 key购买 nike

我正在尝试向 Discord Web API 发送请求,但不断收到 401 响应代码。我在网上找到的几乎所有答案都来自使用不记名 token 而不是机器人 token 的人,并且更改为机器人 token 是有效的。我正在使用机器人 token ,但仍然收到 401 错误。但是,我知道该机器人 token 是有效的,因为尝试使用无效 token 启动 node bot.js 会引发错误并且无法启动机器人。我现在的代码很简单

const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
const axios = require('axios');
const headers = {
'Authorization': `Bot ${auth.token}`
};

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {

/* If the author is a bot, do nothing */
if (msg.author.bot) {
return;
}

/* Only perform an action if the first character is ? */
if (msg.content.substring(0, 1) == '?' && msg.content.length > 1) {
var message = msg.content.substring(1).toLowerCase();

//console.log(message);
//console.log(msg);
//console.log(msg.channel.name);

switch (message) {
case 'gos':

axios.get(`https://discordapp.com/api/channels/${msg.channel.id}/messages`, headers)
.then(response => {
console.log(response);
}).catch(err => {
console.log(err);
});


break;
case 'dolphin':
msg.reply('dolphin', {files: [
"https://www.dolphinproject.com/wp-content/uploads/2019/07/Maya-870x580.jpg"
]});
break;
}

}
});

client.login(auth.token);

我尝试使用硬编码值在 postman 中执行请求,并且得到相同的响应,所以我不认为这是语法错误,但我不能确定。预先感谢您的帮助。

最佳答案

据我从您的问题中了解到,您从 postman 那里得到了相同的响应(401 Unauthorized),因此唯一的原因是访问 token 无效或者您无权对 API 进行此类调用或避免不和谐的 channel 。

您应该看到的另一件事是您在 axios 中发送 header 的方式,在这里我可以与您分享发送 header 的正确方法: How to set header and options in axios?

还要检查“auth.json”是否具有正确的 token ,因为您正在调用它(auth.token)。

关于javascript - Discord API 对于使用机器人 token 的所有查询返回 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58549110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com