我正在尝试通过DiscordJS发送消息
有人能帮帮我吗?
我试过这个代码
const channel = client.channels.cache.get('example discord guild');
channel.send('content');
但它确实起作用了
index.js
// Credetials
const { token } = require('./json/token.json');
const { guild } = require('./json/guild.json');
const { client } = require('./json/client.json')
// Init
const { Client, Intents } = require('discord.js');
const bot = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_>
bot.login(guild);
console.log("Logged In As DeltaBOT");
const channel = client.channels.cache.get(guild);
channel.send('content');
错误
const channel = client.channels.cache.get(guild);
^
TypeError: Cannot read properties of undefined (reading 'cache')
at Object.<anonymous> (/storage/emulated/0/Download/node/index.js:15:33)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
发布于 2021-11-18 10:54:28
您应该等待客户端登录,然后再尝试发送消息/获取通道。您可以使用ready事件来完成此操作。例如:
bot.on('ready', async () => {
console.log('Logged in as: ' + bot.user.tag);
const channel = await bot.channels.fetch(guild);
await channel.send('content');
})
我注意到的另一件事是你使用的是客户端而不是机器人。客户端是一个json对象,但是您将discord bot对象定义为bot变量。所以使用bot变量,而不是client变量。
确保行会是一个有效的行会ID。我不知道您的客户端json文件中有什么,但您似乎没有使用它。
https://stackoverflow.com/questions/70016693
复制相似问题