【问题标题】:how to make command that lists all of the servers the bot is in如何制作列出机器人所在的所有服务器的命令
【发布时间】:2021-04-21 15:11:31
【问题描述】:

我尝试制作一个命令,可以用来向我显示机器人所在的服务器,但是当我开始制作命令时,它并不能以任何方式工作,我尝试它并且总是告诉我其中一件事是未定义或无法读取属性,它主要告诉我机器人或公会未定义或无法正确读取。

我想发出一个只有我可以在不和谐中使用的命令来查看我的机器人所在的所有服务器。

我使用命令处理程序,这意味着每个命令都在单独的 js 文件中。

我的 discord 客户端变量设置为“bot”(如代码所示)。

这是命令(servers.js):

module.exports = {
    name: 'servers',
    execute(bot, message, args) {
        bot.guilds.cache.forEach(guild => {
            message.channel.send(`Servers: ${guild.name}`);
        })
    }
}

这是命令处理程序/主文件(index.js):

const Discord = require('discord.js');
const bot = new Discord.Client();
const fs = require('fs');
const { prefix, token } = require('./config.json');

    bot.commands = new Discord.Collection();
    const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const command = require(`./commands/${file}`);
        bot.commands.set(command.name, command);
    }
    const cooldowns = new  Discord.Collection();
    
    bot.once('ready', () => {
        console.log('Online!');
    });
    
    bot.on('message', message => {
        if (!message.content.startsWith(prefix) || message.author.bot) return;
            const args = message.content.slice(prefix.length).trim().split(/ +/);
            const commandName = args.shift().toLowerCase();
    
            if (!bot.commands.has(commandName)) return;
            const command = bot.commands.get(commandName);
    
            if (command.args && !args.length) {
                    let reply = `You didn't provide any arguments, ${message.author}!`;
    
                    if (command.usage) {
                        reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
                    }
                    return message.channel.send(reply);
            }
            try {
                command.execute(message, args);
            }
            catch (error) {
                console.error(error);
                    message.reply('there was an error trying to execute that command!').then(msg => {
                        msg.delete({timeout:6000});
                    });
            }
    });

还有错误:

TypeError: Cannot read property 'cache' of undefined
    at Object.execute (D:\Users\USER\Desktop\Bot\commands\serversinfo.js:4:20)
    at Client.<anonymous> (D:\Users\USER\Desktop\Bot\index.js:64:21)
    at Client.emit (node:events:376:20)
    at MessageCreateAction.handle (D:\Users\USER\Desktop\Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (D:\Users\USER\Desktop\Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    列出机器人所在的所有服务器的最简单方法是像这样映射所有服务器:

    message.channel.send(client.guilds.cache.map(s => s.name))
    

    然后您可以根据需要将其放入您的命令中 - 如果您有任何问题或它不起作用,请随时回复。

    module.exports = {
    name: 'servers',
    execute(bot, message, args) {
        message.channel.send(client.guilds.cache.map(s => s.name))
    }
    

    如果这不起作用或您有任何其他问题,请随时回复我!

    【讨论】:

    • 我试过了,但它仍然不起作用它告诉我无法读取属性'发送'
    • 您的建议也很有帮助,感谢您的帮助!
    • @TMG 如果它无法读取 send 的属性,那么很可能当您输入 execute (bot, message, args) 时,您并没有使消息成为实际消息。如果这有意义
    【解决方案2】:

    您的执行函数需要 3 个参数 execute(bot, message, args),但是当您在主文件中使用它时只给它 2 个参数:command.execute(message, args);

    添加缺少的参数应该可以解决这个问题:

    command.execute(bot, message, args)
    

    【讨论】:

    • 它工作了,我只是在主文件的括号中添加了 bot 并尝试了它并工作了。谢谢你的帮助,我真的很感激!
    猜你喜欢
    • 2021-08-21
    • 2021-02-02
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2021-07-09
    • 2021-02-13
    • 2020-10-17
    • 2021-05-05
    相关资源
    最近更新 更多