使用discord.js运行函数后如何删除消息

时间:2019-06-05 20:39:40

标签: discord discord.js

我正在为我的机器人执行音乐搜索命令,并尝试在完成其他功能后删除一条消息。

我尝试了此操作,但并未将其删除。它带有未知消息。

const search = require('yt-search')

module.exports.run = async (bot, message ,args ,ops) =>{

message.delete()

search(args.join(' '), function(err, res) {

    if(err) return message.channel.send("Sorry, something went wrong.")

    let videos = res.videos.slice(0, 10);

    let resp = '';

    let resp = '';

    let n

    for( var i in videos){
        resp += `**[${parseInt(i)+1}]:**\`${videos[i].title}\`\n `
    }

    resp += `\n**Choose a number between \` 1-${videos.length}\`**`

    message.channel.send(resp).then(msg =>{
        n = msg.id
    })

    const filter = m => !isNaN(m.content) && m.content < videos.length +1 && m.content > 0;

    const collector = message.channel.createMessageCollector(filter);

    collector.videos = videos

    collector.once("collect", function(m, n){

       message.channel.fetchMessages(n).then(mess =>mess.delete());   

       let commandFile = require(`./play.js`)
       commandFile.run(bot, message, [this.videos[parseInt(m.content)-1].url], ops)
    })

1 个答案:

答案 0 :(得分:0)

根据您显示的代码,消息变量几乎没有上下文。它是未知的,您将一个函数作为回调发送,这一事实使我想到了该函数的范围。您可以尝试的一件事是在此处更改功能:

collector.once("collect", function(m,n){

           message.channel.fetchMessages(n).then(mess =>mess.delete());   

            let commandFile = require(`./play.js`)
            commandFile.run(bot, message, [this.videos[parseInt(m.content)-1].url], ops)
        }) 

到箭头功能:

collector.once("collect", (m,n) => {
   // rest of the code 
});