【问题标题】:DiscordJS - Send MessageDiscordJS - 发送消息
【发布时间】:2021-12-29 03:45:42
【问题描述】:

我正在尝试通过 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

【问题讨论】:

  • 你的client.json文件的内容是什么?
  • 他们的不和谐服务器通常很有帮助discord.gg/djs

标签: node.js discord.js


【解决方案1】:

在尝试发送消息/获取频道之前,您应该等待您的客户登录。您可以通过使用 ready 事件来做到这一点。例如:

bot.on('ready', async () => {
    console.log('Logged in as: ' + bot.user.tag);
    const channel = await bot.channels.fetch(guild);
    await channel.send('content');
})

我注意到的另一件事是您使用客户端而不是机器人。 client 是一个 json 对象,但您将 discord bot 对象定义为 bot 变量。所以使用 bot 变量而不是客户端变量。

确保公会是有效的公会 ID。我不知道您的客户端 json 文件中有什么,但您似乎没有使用它。

【讨论】:

    猜你喜欢
    • 2018-12-26
    • 2021-05-05
    • 2021-09-13
    • 2020-10-19
    • 2018-05-31
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    相关资源
    最近更新 更多