Discordjs 令牌对客户端不可用
Posted
技术标签:
【中文标题】Discordjs 令牌对客户端不可用【英文标题】:Discordjs token unavailable to the client 【发布时间】:2022-01-18 05:34:43 【问题描述】:我正在使用 Discordjs 在 typescript 中编写一个机器人,以向指定的频道发送消息。我收到了错误
HTTPError [DiscordjsError]: Request to use token, but token was unavailable to the client.
在尝试发送之前,我正在等待客户端准备好,我检查了 .env 中的变量,从服务器中删除了机器人,使用新机器人创建了新应用程序,重做 OAuth2 并赋予它管理员权限并再次添加,客户端.token 返回 DISCORD_TOKEN 的值。我到底错过了什么?
代码:
import Client, TextChannel from "discord.js";
import dotenv from "dotenv";
dotenv.config();
//get and check env vars present
const token = process.env.DISCORD_TOKEN;
if (!token)
throw new Error("provide DISCORD_TOKEN as env variable");
const channelId = process.env.DISCORD_CHANNEL_ID;
if (!channelId)
throw new Error("provide DISCORD_CHANNEL_ID as env variable");
//create and login discord client instance
const client = new Client( intents: ["GUILDS", "GUILD_MESSAGES"] );
try
client.login(token)
catch (err)
console.log(`problem on client login: $err`)
//send notification
client.on("ready", async () =>
console.log(client.isReady())
console.log(client.token)
console.log("discord client ready!");
const channel = (await client.channels.fetch(channelId)) as TextChannel;
channel.send('hello channel')
client.destroy()
console.log('done')
);
完整输出
rue
OT########################## //discord token
discord client ready!
done
/Users/.../node_modules/discord.js/src/rest/RequestHandler.js:200
throw new HTTPError(error.message, error.constructor.name, error.status, request);
^
HTTPError [DiscordjsError]: Request to use token, but token was unavailable to the client.
at RequestHandler.execute (/Users/.../node_modules/discord.js/src/rest/RequestHandler.js:200:15)
at RequestHandler.execute (/Users/.../node_modules/discord.js/src/rest/RequestHandler.js:204:19)
at RequestHandler.push (/Users/.../node_modules/discord.js/src/rest/RequestHandler.js:50:25)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async TextChannel.send (/Users/.../node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:172:15)
code: 500,
method: 'post',
path: '/channels/905578571374952478/messages',
requestData:
json:
content: 'hello channel',
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
,
files: []
编辑:记录 client.token 以在需要时显示它存在并添加完整输出
【问题讨论】:
表示该令牌在登录前(或销毁后)使用。 我们能知道记录了什么吗? 谢谢我加了, 【参考方案1】:当然……
await channel.send('hello channel')
修复它
【讨论】:
以上是关于Discordjs 令牌对客户端不可用的主要内容,如果未能解决你的问题,请参考以下文章
Discord JS:UnhandledPromiseRejectionWarning:错误:Shard 的客户端准备就绪时间过长
如何在 Keycloak 中添加 ipAddres o clientAddress 令牌