【问题标题】:How to make a bot know how to delete webhooks that it made and by channel mentions如何让机器人知道如何删除它创建的 webhook 以及通过频道提及
【发布时间】:2019-11-15 15:00:15
【问题描述】:

您好,我想创建一个 Discord.JS-Commando 命令,如果您选择一个频道,机器人会删除它在那里拥有的 webhook,如果它被命名为 Marker,并且如果它检测到那里是否没有它拥有的 webhook Marker 就是return message.channel.send("Hey! There's no webhook I own in this channel!")

机器人删除了一个 webhook,即使它没有成功,而且它不在我提到的频道中。我该如何解决这个问题?

在谷歌上搜索,什么都没有。 除了 discord.js 文档之外,没有任何关于删除 webhook 的内容。

const hooks1 = await message.guild.fetchWebhooks();
await hooks1.forEach(async webhook => {
    if (!watchChannel.id == webhook.channelID) return
    if (!webhook.owner.id == `595840576386236437`) return
    if (!webhook.name == `Marker`) return message.channel.send(`**${message.author.username}**, Nothing was found. You or someone else may have renamed the webhook. Please delete the webhook manually. Sorry for the inconvenience`);
    else
message.channel.send(`Deleted successfully.`).then(msg => {message.delete(4000)}).catch(error => console.log(error))
webhook.delete(`Requested per ${message.author.username}#${message.author.discriminator}`);
});

我希望机器人知道如何删除它在提到的频道中创建的 webhook,但机器人不知道要删除哪个 webhook。

【问题讨论】:

  • 首先,一个小建议:使用=== 而不是==(您可以看到there 的区别,并为您的if 添加括号。是的一行if 没有需要它们,但是如果您需要编辑代码(例如,您想添加console.log?如果您这样写,第二个语句将不会出现在 if 中。您会更容易阅读并且更好)必须添加括号,但您可能会忘记,从而导致错误的调试或代码行为)
  • 您有错误吗?你确定 webhook 存在吗?你确定主人是好身份证吗?你尝试登录webhook吗?
  • 1. Discord 中的错误:TypeError: Cannot read property 'id' of undefined, 2. 是的,它被命名为“Marker”并归 595840576386236437 所有。3. 我确保机器人的 ID 和 owner.ID 匹配。 4. 还没有。
  • 无论如何,我得到了答案,只需将其设为webhook.delete(),现在的问题是它会删除它看到的所有 webhook,即使我成功了。

标签: javascript discord.js commando


【解决方案1】:
if (!watchChannel.id == webhook.channelID) return
if (!webhook.owner.id == `595840576386236437`) return
if (!webhook.name == `Marker`) return

这些行都没有像您期望的那样工作。

const id = '189855563893571595';

console.log(id === '189855563893571595');

console.log(id !== '1234');  // id is not equal to '1234': true
console.log(!id === '1234'); // false is equal to '1234' : false

! 充当 逻辑非 运算符。

如果其单个操作数可以转换为true,则返回false;否则,返回true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

!watchChannel.idBoolean;它永远不会等于webhook.channelID,除非后者是false。代码中的所有三个条件也是如此。因此,您的机器人正在删除不属于自己的 Webhook,因为 if 语句在您预期的情况下并不正确。


!== 被称为 非身份/严格不等式 运算符。

...[R] 如果操作数不相等和/或类型不同,则返回 true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

这(或不等式运算符!= 与双胞胎一起使用)是您想要使用的运算符。它会正确比较属性。


改进您当前的代码,我们可以...

  • 仅从指定通道获取 Webhook。
  • 在循环之前过滤Collection
  • 使用现代的for...of 循环,它可以与异步代码正常工作。
  • 确保捕获所有被拒绝的承诺。
  • 养成使用标识运算符=== 而不是相等运算符== 的习惯。推理见here
const webhooks = await watchChannel.fetchWebhooks();
const myWebhooks = webhooks.filter(webhook => webhook.owner.id === client.user.id && webhook.name === 'Marker');

try {
  if (myWebhooks.size === 0) return await message.channel.send('I don\'t own any Webhooks there...');

  for (let [id, webhook] of myWebhooks) await webhook.delete(`Requested by ${message.author.tag}`);

  await message.channel.send('Successfully deleted all of my Webhooks from that channel.');
} catch(err) {
  console.error(err);

  await message.channel.send('Something went wrong.')
    .catch(console.error);
}

【讨论】:

    【解决方案2】:

    您是否查看过 discord.js 文档?它提供了您需要了解的所有内容,例如对象、类、对象和类的方法/属性等。无论如何,我认为问题在于,当您尝试删除 webhook 时,您使用的是webhook.delete,但是当您使用不带括号的delete 时,意味着您正在尝试访问属性delete,而不是方法。正确的做法是调用webhook.delete();,因为这会调用Webhook 类中的delete() 方法。

    就在文档中:

    Webhook 类:https://discord.js.org/#/docs/main/stable/class/Webhook

    删除方法:https://discord.js.org/#/docs/main/stable/class/Webhook?scrollTo=delete

    【讨论】:

      猜你喜欢
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2016-07-22
      • 2016-11-07
      相关资源
      最近更新 更多