无法将消息发送到特定的不和谐服务器,出现代码错误

时间:2019-11-18 16:19:59

标签: javascript node.js discord discord.js

我正在尝试使Discord机器人使用服务器ID将消息发送到特定的Discord服务器,我不知道该命令,我尝试了类似的方法,但是它没有用,出现错误,请帮助,在此先感谢!

我的代码

const Discord = require('discord.js');
const bot = new Discord.Client();


module.exports = {
    name: "inviteme",
    description: "inviteme",
    execute(message, args){
        var server = bot.guilds.get("serverid");
        var channel = server.channels.get("channelid");
        bot.guilds.forEach(guild => {
            guild.channels.first().createInvite()
                .then(inv => message(channel).send(`${guild.name} | ${inv.url}`));
        });
    }
}

错误

TypeError: Cannot read property 'channels' of undefined
    at Object.execute (d:\etc\Discord Bots\Coinflip\commands\inviteme.js:10:30)
    at Client.<anonymous> (d:\etc\Discord Bots\Coinflip\index.js:31:42)
    at Client.emit (events.js:210:5)
    at MessageCreateHandler.handle (d:\etc\Discord Bots\Coinflip\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (d:\etc\Discord Bots\Coinflip\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (d:\etc\Discord Bots\Coinflip\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (d:\etc\Discord Bots\Coinflip\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (d:\etc\Discord Bots\Coinflip\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:210:5)
    at Receiver.receiverOnMessage (d:\etc\Discord Bots\Coinflip\node_modules\ws\lib\websocket.js:789:20)

1 个答案:

答案 0 :(得分:2)

问题是bot.guilds.get("serverid");会尝试从已登录的漫游器的缓存中获取行会-但在此文件中,您将bot设置为new Discord.Client();且从未登录。

请勿登录多个机器人实例。相反,您需要做的是从传递给命令的消息对象中获取已登录的bot实例,例如:message.client.guilds.get("serverid")-基本上,使用message.client代替bot或放置{{1 }}的开头。