我无法为我的机器人用户行会(我刚开始使用discord.js)

时间:2019-10-01 10:58:46

标签: discord.js

当我尝试运行此代码时:

bot.sendMessage({
  to: channelID,
  message: message.guild.name
});

我无法读取未定义的属性“名称”。 我尝试使用公会时总是会发生此错误。

这是我完整的机器人代码:

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
var verificationUser;
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
    colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});
bot.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
    if (message.substring(0, 21) == '<@' + bot.id + '>') {
        var args = message.substring(22).split(' ');
        var cmd = args[0];
        switch(cmd) {
            case 'help':
                bot.sendMessage({
                    to: channelID,
                    message: "Istruction for make the verification:\n1 - Mention me and write 'verification'.\n2 - Mention me your Mazebert id.\n3 - Play a suicide play without build tower, just waiting the death.\n4 - Mention me and text 'check'.\n5 - Finish!"
                });
                break;
            case 'verification':
                bot.sendMessage({
                    to: channelID,
                    message: "<@"+ userID + ">For starting the verification you have to write your Mazebert id. You can take it in your profile, is the numerical code in the url."
                });
                bot.sendMessage({
                    to: channelID,
                    message: message.guild.name
                });
                verificationUser = userID;
            break;
         }
     }
});

我刚开始使用js中的Discord机器人,所以我不明白为什么会出现此错误。

1 个答案:

答案 0 :(得分:1)

在消息上使用的正确方法是这样的

bot.on("message", async message => {

不是

bot.on('message', function (user, userID, channelID, message, evt) {

您要指定额外的参数,如果以后尝试访问它们,这些参数将返回undefined。

您可以检查消息对象here

的属性