require('dotenv').config();
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const channel = client.channels.cache.get("967808235048423484");
client.on('ready',() => {
console.log(`${client.user.tag} logged in`);
console.log(channel);
});
client.login(process.env.DISCORDJS_BOT_TOKEN);
输出: -
Anonymous HELPER 😶 #4205 logged in
undefined
我已经检查了其他解决方案
- https://discordjs.guide/popular-topics/faq.html how-do-i-set-both-status-and-activity-in-one-go
- command .js: TypeError: client.channels.get is not a function
- client.channels.cache.get('id')返回空Map
- 用Discord.js发送消息
- 无法使用client.channels.cache.get(")
const channel = await client.channels.fetch(CHANNEL_ID)
error:-SyntaxError: await is only valid in async functions and the top level bodies of modules
const channel = client.channels.fetch(channelID)
版本——
"discord.js": "^13.6.0",
"dotenv": "^16.0.0"
"node.js": "^16.14.2"
客户端当时还没有准备好,所以缓存是空的。把它放到ready
事件中
client.on('ready',() => {
const channel = client.channels.cache.get("967808235048423484");
console.log(`${client.user.tag} logged in`);
console.log(channel);
});