Discord bot-如何在javascript中创建新类和新命令

时间:2018-04-07 21:37:03

标签: javascript bots discord discord.js

我最近使用javascript创建了一个discord bot,当我这样做时!清除,用"未知命令响应,do!help查看命令列表"但在说完之后,它就是删除消息。我不知道有什么问题请帮帮我

const commando = require('discord.js-commando');
const bot = new commando.Client();
const prefix = '!';

bot.on('message', message => {

    let msg = message.content.toUpperCase();
    let sender = message.author; 
    let cont = message.content.slice(prefix.length).split(" "); 
    let args = cont.slice(1); 

    if (msg.startsWith(prefix + 'PURGE')) {        
        async function purge() {
            message.delete();

            if (isNaN(args[0])) {
                message.channel.send('Please input a number of messages to be deleted \n Syntax: ' + prefix + 'purge <amount>'); 
                return;
            }

            const fetched = await message.channel.fetchMessages({limit: args[0]}); 
            console.log(fetched.size + ' messages found, deleting...'); 

            // Deleting the messages
            message.channel.bulkDelete(fetched)
                .catch(error => message.channel.send(`Error: ${error}`));

        } 
        purge(); 
    }
});

bot.login('MY BOT TOKEN HERE');

2 个答案:

答案 0 :(得分:1)

现在您正在使用class task(var title:String, var time:String)库。您决定使用该库的任何原因?您似乎只使用标准discord.js-commando函数,例如discord.jsbot.onmessage.channel.sendmessage.channel.fetchMessages ...

你应该好好使用标准的message.channel.bulkDelete库,用这个开始你的代码:

discord.js

您可以在主要&#34;欢迎&#34;上找到此代码。页面Discord.js

修改 我还不确定你为什么要使用const Discord = require('discord.js'); const bot = new Discord.Client(); ,但这并不重要。这是我使用discord.js-commando库提出的示例命令:

discord.js-commando

我还建议您使用新的const commando = require('discord.js-commando'); class PurgeCommand extends commando.Command { constructor(client) { super(client, { name: 'purge', group: 'random', // like your !roll command memberName: 'purge', description: 'Purge some messages from a Text Channel.', examples: ['purge 5'], args: [ { key: 'numToPurge', label: 'number', prompt: 'Please input a number ( > 0) of messages to be deleted.', type: 'integer' } ] }); } run(msg, { numToPurge }) { let channel = msg.channel; // fail if number of messages to purge is invalid if (numToPurge <= 0) { return msg.reply('Purge number must be greater than 0'); } // channel type must be text for .bulkDelete to be available else if (channel.type === 'text') { return channel.fetchMessages({limit: numToPurge}) .then(msgs => channel.bulkDelete(msgs)) .then(msgs => msg.reply(`Purge deleted ${msgs.size} message(s)`)) .catch(console.error); } else { return msg.reply('Purge command only available in Text Channels'); } } }; module.exports = PurgeCommand 代替type,这样您就可以验证用户的回复,并确保他们输入的数字大于0。

如果您需要有关设置初始integer脚本的帮助,请查看Discord小组提供的此回购:https://github.com/discordjs/Commando/tree/master/test

答案 1 :(得分:0)

你可能想用这个。此清除命令适用于 discord.js v11.5.1,我尚未测试它是否适用于 v12,但我认为这可能对您有用。我应该说这会删除频道内的所有内容(嵌套命令)

exports.run = (bot, message, args) => {
  let filter = m => message.author.id === message.author.id;
    
  message.channel.send("Are you sure you wanna delete all messages? (y/n)").then(() => {
      message.channel.awaitMessages(filter, {
        max: 1,
        time: 30000,
        errors: ['time']
      })
        .then(message => {
          message = message.first();
        
          if (message.content.toUpperCase() == "YES" || message.content.toUpperCase() == "Y") {
            message.channel.bulkDelete(100);
          } else if (message.content.toUpperCase() == "NO" || message.content.toUpperCase() == "N") {
            message.channel.send("Terminated").then(() => {message.delete(2000)});
          } else {
            message.delete();
          }
        })
        .catch(collected => {
        message.channel.send("Timeout").then(() => {message.delete(2000)});
      });
    }).catch(error => {
    message.channel.send(error);
  });
};