开发者问题收集

Discord.js serverinfo 离线 在线 dnd 错误

2020-05-30
143
const Command = require('../../Structures/Command');
const Discord = require('discord.js');

module.exports = class extends Command {

    constructor(...args) {
        super(...args, {
            aliases: ['si']
        });
    }

    async run(message) {
            let sicon = message.guild.iconURL;
            const embed = new Discord.MessageEmbed()
            .setTitle(`Name Of Server:  ${message.guild}`)
            .setColor(0xFF0000)
            .addField('Name', message.guild.name, true)
            .addField('ID', message.guild.id, true)
            .addField('Owner', message.guild.owner, true)
            .addField('Owner ID', message.guild.owner.id, true)
            .addField('Region', message.guild.region, true)
            .addField('Members', message.guild.memberCount, true)
            .addField("Users Online:", `${guild.members.filter(m => m.user.presence.status === "online").size}`)
            .addField("Users DND:", `${guild.members.filter(m => m.user.presence.status === "dnd").size}`)
            .addField("Users Offline:", `${guild.members.filter(m => m.user.presence.status === "offline").size}`)
            .addField('You Joined', message.member.joinedAt, true)
            .addField('Created At', message.guild.createdAt, true)
            .setThumbnail(sicon);
            message.channel.send(embed);
    }

};

// 嘿,我需要帮助,有人能告诉我这里出了什么问题吗?这是错误消息 =>

// 以 Zorek 身份登录!

(node:29228) UnhandledPromiseRejectionWarning: ReferenceError: guild is not defined
    at module.exports.run (C:\Users\xdswe\Desktop\Zero\src\commands\Utilities\Serverinfo.js:23:43)
    at MenuDocsClient.<anonymous> (C:\Users\xdswe\Desktop\Zero\src\Structures\MenuDocsClient.js:38:13)
    at MenuDocsClient.emit (events.js:311:20)
    at MessageCreateAction.handle (C:\Users\xdswe\Desktop\Zero\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\xdswe\Desktop\Zero\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\xdswe\Desktop\Zero\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\xdswe\Desktop\Zero\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\xdswe\Desktop\Zero\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\xdswe\Desktop\Zero\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:311:20)
2个回答

guild 实际上并未定义,您需要指定要从哪个 guild 获取 members 集合

.addField("Users Online:", `${message.guild.members.filter(m => m.user.presence.status === "online").size}`)
.addField("Users DND:", `${message.guild.members.filter(m => m.user.presence.status === "dnd").size}`)
.addField("Users Offline:", `${message.guild.members.filter(m => m.user.presence.status === "offline").size}`)
Syntle
2020-05-30

不要使用 guild.members.filter() ,而是使用例如 message.guild.members.filter()

const Command = require('../../Structures/Command');
const Discord = require('discord.js');

module.exports = class extends Command {

    constructor(...args) {
        super(...args, {
            aliases: ['si']
        });
    }

    async run(message) {
            let sicon = message.guild.iconURL;
            const embed = new Discord.MessageEmbed()
            .setTitle(`Name Of Server:  ${message.guild}`)
            .setColor(0xFF0000)
            .addField('Name', message.guild.name, true)
            .addField('ID', message.guild.id, true)
            .addField('Owner', message.guild.owner, true)
            .addField('Owner ID', message.guild.owner.id, true)
            .addField('Region', message.guild.region, true)
            .addField('Members', message.guild.memberCount, true)
            .addField("Users Online:", `${message.guild.members.filter(m => m.user.presence.status === "online").size}`)
            .addField("Users DND:", `${message.guild.members.filter(m => m.user.presence.status === "dnd").size}`)
            .addField("Users Offline:", `${message.guild.members.filter(m => m.user.presence.status === "offline").size}`)
            .addField('You Joined', message.member.joinedAt, true)
            .addField('Created At', message.guild.createdAt, true)
            .setThumbnail(sicon);
            message.channel.send(embed);
    }

};
Hao C.
2020-05-30