1

嗨,我想创建一个 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。

4

2 回答 2

3
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


!充当逻辑 NOT运算符。

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

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

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


!==被称为非恒等式/严格不等式算子。

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

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

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


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

  • 仅从指定通道获取 Webhook。
  • 在循环之前过滤集合。
  • 使用for...of可以与异步代码正常工作的现代循环。
  • 确保捕获所有被拒绝的承诺。
  • 养成使用恒等运算符===而不是相等运算符的习惯==。见这里推理。
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);
}
于 2019-07-06T15:39:56.973 回答
1

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

它就在文档上:

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

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

于 2019-07-06T00:24:57.383 回答