client.guilds显示为“未定义”

时间:2019-08-15 10:49:22

标签: discord discord.io

好的,所以我正在编写一个不和谐的bot,它将所有用户名输出到公会(服务器)中。

这是我到目前为止的代码。

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
    colorize: true
});
logger.level = 'debug';
// Initialize Discord client
var client = new Discord.Client({
   token: auth.token,
   autorun: true
});
client.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(client.username + ' - (' + client.id + ')');
    logger.info(client.guilds);
});
client.on('message', function (user, userID, channelID, message, evt) {
    // Our client needs to know if it will execute a command
    // It will listen for messages that will start with `!`
    if (message.substring(0, 1) == '.') {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);
        switch(cmd) {
            case 'members':
                var memberList = [];
                const guild = client.guilds.get("610719306678009856");
                guild.members.forEach(member => memberList.push((member.user.username)));  
                client.sendMessage({
                    to: channelID,
                    message: memberList, 
                });
            break;
            // Just add any case commands if you want to..
         }
     }
});

我从client.guilds.get收到一个错误,说TypeError: Cannot read property 'get' of undefined

然后我添加了logger.info(client.guilds)来查看它是否可以输出,并在启动时输出{"message":"undefined","level":"info"}

我一直在检查文档,但是有0条线索为什么它不起作用。

1 个答案:

答案 0 :(得分:1)

文档所述的properties of Client对象下没有guilds,但是servers下有一个Client属性。

因此您可以改为使用client.servers.get(idOfServer)