【问题标题】:Discord JS delete webhook on channelsDiscord JS 删除频道上的 webhook
【发布时间】:2020-12-06 00:52:10
【问题描述】:

我用 java 脚本和 discord.js 学了一点,但我有一个小问题

如果我提到某人和消息,我会发出一个有趣的命令,它会使用他的用户名 + 头像创建一个 webhook 并发送我想要的消息

代码工作,但创建 10 个 webhook 后我无法继续使用命令,是否可以在使用后删除 webhook 或删除频道上的所有 webhook?

我正在使用节点,并且我已经安装了 hookcord 以使用 webhooks 发送消息

我的代码:

  if (message.content.startsWith(prefix + "say")) //say something like if it's a member    
            {    
              message.delete()   
              args[0] = message.mentions.members.first()  
              var usermentions = args[0]    
              let msg = args[1];
              message.channel.createWebhook(usermentions.displayName, usermentions.user.displayAvatarURL).then(wb =>            
              {       
                var hookcord = require('hookcord');
                var Hook = new hookcord.Hook()
                  .setLink(`https://discordapp.com/api/webhooks/${wb.id}/${wb.token}`)
                  .setPayload({
                    'title': usermentions.displayName,
                    'avatar': usermentions.user.displayAvatarURL,
                    'content': msg
                  })
                  .fire()
                  .then(function(response) {})
                  .catch(function(e) {})
              })
            }

提前感谢您的时间和帮助!

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    是的,您可以使用channel.fetchWebhooks(),然后使用 forEach 循环将它们全部删除:

    if(message.content === 'delete-webbooks'){
       message.channel.fetchWebhooks().then((webhooks) => {
           webhooks.forEach((wh) => wh.delete());
       });
    }
    

    你也可以替换:

    .then(function(response) {})
    

    作者:

    .then(function(response) {
        wb.delete();
    }) 
    

    我认为这是最好的解决方案。

    【讨论】:

    • 谢谢你,它的工作!但是当我编写代码时,我又遇到了一个问题:/say @user 你好,我是一个机器人,只有消息你好显示,而不是我是一个机器人,你知道为什么吗?
    • let msg = args.slice(1).join(" ");替换let msg = args[1];
    猜你喜欢
    • 2021-09-21
    • 2023-01-10
    • 2021-01-12
    • 1970-01-01
    • 2021-11-11
    • 2020-04-18
    • 1970-01-01
    • 2020-10-01
    • 2021-07-15
    相关资源
    最近更新 更多