Discord.JS Purge.js 命令问题

Posted

技术标签:

【中文标题】Discord.JS Purge.js 命令问题【英文标题】:Discord.JS Purge.js command issue 【发布时间】:2020-04-06 07:37:58 【问题描述】:

好的,所以我的机器人使用稍微不同的代码进行了重建。

我正在使用更简化的fs 命令和事件处理程序。我的命令按预期工作。

但我想将修剪的数量添加到 RichEmbed 的字段中,并且一直出错。

这是我的purge.js 文件

const Discord = require('discord.js')


module.exports = 
    name: 'purge',
    description: 'Purge up to 99 messages.',
    execute(message, args) 
        console.log("purging messages")


        const embed = new Discord.RichEmbed()
            .setTitle("Success")
            .setColor(0x00AE86)
            .setFooter("Guardian", "https://raw.githubusercontent.com/phantomdev-github/Resources/master/Discord%20Bots/Guardian/src/avatar.png")
            .setThumbnail("https://raw.githubusercontent.com/phantomdev-github/Resources/master/Discord%20Bots/Guardian/src/avatar.png")
            .setTimestamp()
            .setURL("https://github.com/phantomdev-github/Resources/tree/master/Discord%20Bots/Guardian")
            .addField("Bot Messages Purged", "missing code here", false)
            .addField("User Pins Purged", "missing code here", false)
            .addField("User Messages Purged", "missing code here", false)
            .addField("Total Messages Purged", "missing code here", false)

        message.channel.send( embed );

        const amount = parseInt(args[0]) + 1;

        if (isNaN(amount)) 
            return message.reply('that doesn\'t seem to be a valid number.');
         else if (amount <= 1 || amount > 100) 
            return message.reply('you need to input a number between 1 and 99.');
        

        message.channel.bulkDelete(amount, true).catch(err => 
            console.error(err);
            message.channel.send('there was an error trying to prune messages in this channel!');
        );
    ,
;

如果有帮助,我的index.js

const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
const  token  = require('./token.json');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) 
    const command = require(`./commands/$file`);
    client.commands.set(command.name, command);
    console.log(file,command)


fs.readdir('./events/', (err, files) => 
    if (err) return console.error(err);
    files.forEach(file => 
        if(!file.endsWith('.js')) return;
        const eventFunction = require(`./events/$file`);
        console.log(eventFunction)
        eventFunction.execute(client)
    );
);

client.login(token);

这是我的message.js

const  prefix  = require('./prefix.json');

module.exports = 
    name: 'message',
    description: '',
    execute:function(client) 
        client.on('message',message => 
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
        if (!client.commands.has(command)) return;

        try 
            client.commands.get(command).execute(message, args);
             catch (error) 
                console.error(error);
                message.reply('there was an error trying to execute that command!');
                
)

;

基本上,我试图弄清楚将什么放入"missing code here" 部分。此外,任何将其锁定给仅具有管理员权限的人的方法也很有用。我尝试过,但它无法与嵌入一起使用。

【问题讨论】:

【参考方案1】:

如果我理解正确,您想知道如何获取已清除引脚、机器人消息和用户消息的数量。为此,您需要在删除消息后进行嵌入。

purge.js

const Discord = require('discord.js')


module.exports = 
    name: 'purge',
    description: 'Purge up to 99 messages.',
    execute(message, args) 
        console.log("purging messages")

        const amount = parseInt(args[0]) + 1;

        if (isNaN(amount)) 
            return message.reply('that doesn\'t seem to be a valid number.');
         else if (amount <= 1 || amount > 100) 
            return message.reply('you need to input a number between 1 and 99.');
        

        message.channel.bulkDelete(amount, true).then(deletedMessages => 
            // Filter the deleted messages with .filter()
            var botMessages = deletedMessages.filter(m => m.author.bot);
            var userPins = deletedMessages.filter(m => m.pinned);
            var userMessages = deletedMessages.filter(m => !m.author.bot);

            const embed = new Discord.RichEmbed()
                .setTitle("Success")
                .setColor(0x00AE86)
                .setFooter("Guardian", "https://raw.githubusercontent.com/phantomdev-github/Resources/master/Discord%20Bots/Guardian/src/avatar.png")
                .setThumbnail("https://raw.githubusercontent.com/phantomdev-github/Resources/master/Discord%20Bots/Guardian/src/avatar.png")
                .setTimestamp()
                .setURL("https://github.com/phantomdev-github/Resources/tree/master/Discord%20Bots/Guardian")
                .addField("Bot Messages Purged", botMessages.size, false)
                .addField("User Pins Purged", userPins.size, false)
                .addField("User Messages Purged", userMessages.size, false)
                .addField("Total Messages Purged", deletedMessages.size, false);

            message.channel.send(embed);
        ).catch(err => 
            console.error(err);
            message.channel.send('there was an error trying to prune messages in this channel!');
        );
    ,
;

【讨论】:

以上是关于Discord.JS Purge.js 命令问题的主要内容,如果未能解决你的问题,请参考以下文章

嵌入命令(颜色)(discord.js-commando)

杀死 discord.js 中的命令处理程序脚本

我不知道如何在 discord.js 中执行可配置的命令

Discord.js 机器人的 Mass Role Assigner 命令

为啥我在 discord.js 中收到客户端错误

使用客户端的参数根据用户留在命令中的参数创建自定义嵌入消息(Discord JS)