【问题标题】:Unable to delete message无法删除消息
【发布时间】:2021-04-25 22:17:29
【问题描述】:
const Discord = require('discord.js')

const fetch = require('node-fetch')

const bot = new Discord.Client()
const fetch = require('node-fetch')

const token = 'TOKEN'

bot.on('ready' , () =>{
  console.log('Bot has logged in!')
})

const prefix = "!"
bot.on('message' , async (msg) =>{
  if(msg.content[0] !== prefix){
    console.log('No prefix given')
    return
  }
  const args = msg.content.slice(prefix.length).trim().split(' ')
  console.log(args)
  const command = args.shift().toLowerCase()
  console.log(command)

  let num1 = 2;
  if(args[0]){
    num1 = parseInt(args[0]) + 1;
  }


  if(command === 'clear'){
    let num = 2;
    if(args[0]){
      num = parseInt(args[0]) + 1;
    }
    msg.reply(`Are you sure u want to delete ${args[0]} 
    messages Yes or No`)
    command.clear()
  } 
  if(command === 'yes'){
    
    msg.channel.bulkDelete(num1);
    msg.channel.send(`deleted ${args[0]} posts for u`);
  }




})
bot.login(token)

我想知道我的代码中的问题,因为 clear 和 yes 命令似乎可以正常工作,因为它们输出了所需的回复,但似乎没有按预期删除消息。 ..................................................... ......................................

【问题讨论】:

  • 请在discord.com/developers/applications 重新生成您的机器人令牌,正如您在这篇文章中所拥有的那样,任何人都可以使用它充当您的机器人。

标签: javascript node.js discord.js


【解决方案1】:

在这种情况下,您可能希望使用message collector。它将等待频道中的消息。

if (command === 'clear') {
    let num = 2;
    if (args[0]) {
        num = parseInt(args[0]) + 1;
    }
    msg.reply(`Are you sure u want to delete ${args[0]} 
    messages Yes or No`)
    const collector = new Discord.MessageCollector(message.channel, m => m.author.id === message.author.id, { max: 1, time: 60000 });
    collector.on('collect', message => {
        if (message.content === 'yes') {
            message.channel.bulkDelete(num1);
            message.channel.send(`deleted ${args[0]} posts for u`);
        }
    });
}

【讨论】:

  • 谢谢!它确实奏效了,但我在声明收集器声明时有点困惑,你能解释一下吗。所以我可以在我的旅程后期解决它:)谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
相关资源
最近更新 更多