【发布时间】:2020-08-10 02:16:49
【问题描述】:
我是使用 webhook 的新手,我仔细阅读了 discord 文档以帮助我设置一个非常基本的机器人。在这一点上,我想做的就是让机器人上线并使用 webhook 发布帖子。我什至直接从 Discord Webhooks 文档中复制了基本代码,但我无法让它工作。
使用一些控制台日志,我确定 client.channels.cache.get(config.webhookID);返回未定义,因此 channel.fetchWebhooks() 不是函数。这仍然是获取频道中 webhook 的方法还是背后的文档?
我正在求助任何有经验的用户,他们可能会帮助我解决这个问题。如果您能提供任何帮助,我将不胜感激。
const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client();
const embed = new Discord.MessageEmbed()
.setTitle('Some Title')
.setColor('#0099ff');
client.once('ready', async () => {
const channel = client.channels.cache.get(config.webhookID); //This is the problem.
try {
const webhooks = await channel.fetchWebhooks(); //This will not execute because channel is undefined.
const webhook = webhooks.first();
await webhook.send('Webhook test', {
username: 'some-username',
avatarURL: 'https://i.imgur.com/wSTFkRM.png',
embeds: [embed],
});
} catch (error) {
console.error('Error trying to send: ', error);
}
});
client.login(config.token);
控制台中的错误是这样的:
Error trying to send: TypeError: Cannot read property 'fetchWebhooks' of undefined
at Client.<anonymous> (C:\Users\me\OneDrive\Desktop\Discord Bot\index.js:13:34)
at Object.onceWrapper (events.js:421:28)
at Client.emit (events.js:315:20)
at WebSocketManager.triggerClientReady (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:433:17)
at WebSocketManager.checkShardsReady (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:417:10) at WebSocketShard.<anonymous> (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:199:14)
at WebSocketShard.emit (events.js:315:20)
at WebSocketShard.checkReady (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:467:12)
at WebSocketShard.onPacket (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:439:16)
at WebSocketShard.onMessage (C:\Users\me\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
【问题讨论】:
-
config.webhookID有效吗?你检查了吗?? -
@Saeed,我相信是的。解决此问题后,我将创建一个新的 webhook,以便我可以在此处发布。我正在使用链接的“703706406125174825”部分。 discordapp.com/api/webhooks/703706406125174825/…
-
删除
cache并使用client.channels.get获取 -
@Saeed 这不是一个函数。我相信 cache.get() 是在 discord.js v12 中添加的
-
你检查this问题了吗?
标签: node.js discord webhooks channels