我目前正在为discord制作discord.js机器人,但我一直都会遇到同样的问题:
ReferenceError: guild is not defined
at Client.client.on.message (C:\Users\Valentin\Documents\JsBot_dev\bot.js:27:30)
at emitOne (events.js:101:20)
at Client.emit (events.js:188:7)
at MessageCreateHandler.handle (C:\Users\Valentin\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\Users\Valentin\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:120:65)
at WebSocketManager.eventMessage (C:\Users\Valentin\node_modules\discord.js\src\client\websocket\WebSocketManager.js:273:31)
at WebSocket.onMessage (C:\Users\Valentin\node_modules\ws\lib\WebSocket.js:442:14)
at emitTwo (events.js:106:13)
at WebSocket.emit (events.js:191:7)
at Receiver.ontext (C:\Users\Valentin\node_modules\ws\lib\WebSocket.js:841:10)
我尝试更新我的discord.js并重新安装node.js但没有任何效果。
感谢您的帮助!
机器人代码:
const Discord = require("discord.js");
const client = new Discord.Client();
myid = "your id is :"
const hook = new Discord.WebhookClient('NoIdForYou', 'NoTokenForYou');
client.on('ready', () => {
console.log(`Logged in as ${client.user.username}!`);
});
client.on('message', message => {
if (message.content === '-avatar') {
message.reply(message.author.avatarURL);
}
});
client.on('hook', hook => {
if (message.content === '-test') {
hook.sendMessage('Ok.')
}
});
client.on('message', message => {
if (message.content === '-react') {
const emoji = guild.emojis.first();
client.react(emoji.name.ballot_box_with_check)
}
});
client.login('NoTokenForYou');
答案 0 :(得分:1)
const emoji = guild.emojis.first();
首先看看这个。
guild
基本上是我们在不和谐服务器中所称的一个类别'。但基本上你只是调用了一个服务器,但你没有指定它来自哪个服务器。所以实际上,做const emoji = message.guild.emojis.first();
其次:client.react(emoji.name.ballot_box_with_check)
反应被添加到消息中。 client
是机器人本身
message.react(/*emoji*/);
将是正确的。
答案 1 :(得分:0)
我遇到了同样的问题,我下载了discord.js而不是discord.io解决了它
npm install discord.js --save
^
not .io
答案 2 :(得分:0)
错误是guild.emojis.first()
您需要写message.guild.emojis.first()