首页 > 解决方案 > Discord.js 通过现有函数删除消息

问题描述

问题 执行我的代码时,我目前确实没有收到任何错误,但想添加到一个函数中,但几乎尝试了所有处理它的方法。此时,由于混乱和沮丧,该变量已被删除。

需要发生的是,发起命令的用户,他们的消息在短暂的延迟后被删除。我已经尝试了 message.delete(1000) 和 v12 的其他变体,但没有运气。无法从我的活动函数中传递“消息”变量?

也许我完全离开了,只是让自己很难,我不知道。老实说很尴尬,我无法弄清楚消息。删除。请帮忙。为无知道歉。

const Discord = require('discord.js');
const bot = new Discord.Client();
const token = "";
const PREFIX = "!";

const fs = require('fs');
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);
}


bot.on('ready', () => {
    console.log("The bot is active and ready to go!");
});

      bot.on('message', function(message) {
        if(message.content[0] === PREFIX) {
            let command = message.content.substring(message.content.indexOf(" ") + 1, message.content.length);

        }
    });

bot.on('message', message => {
    let args = message.content.substring(PREFIX.length).split(" ");

    switch (args[0]) {

        case "crash":
            bot.commands.get('crash').execute(message, args);
        break;

        case "hello":
            bot.commands.get('hello').execute(message, args);
        break;

        case "purge":
            bot.commands.get('purge').execute(message, args);
        break;
    }

});

bot.login(token);

这是一个“crash.js”的示例供参考。不知道我是否需要从那里执行删除?

module.exports = {
    name: 'crash',
    description: "Crash",
    execute(message, args){
        message.author.send("Here is the link you requested");
    }
}

标签: javascriptdiscorddiscord.jsmessage

解决方案


您可以从模块中执行删除。消息作为完整对象传递,因此您只需对其调用 delete 方法。但是,选项是一个对象,这意味着它需要这样定义。为清楚起见,我将使用另一个变量,但这可以内联完成。

let options = {
    timeout: 1000,
    reason: 'Because I said so.'
}

message.delete(options);

或内联...

message.delete({timeout: 1000});

推荐阅读