嗨,我想制作一个discord.js-commando命令,如果选择一个频道,则bot删除了它在那里拥有的webhook,并且如果它命名为 Marker
,并且是否检测到是否没有。那里的webhook拥有名为 Marker
it return message.channel.send("Hey! There's no webhook I own in this channel!")
尽管没有制成,但该机器人也会删除Webhook,并且不在我提到的频道中。我该如何解决?
在Google上搜索它,什么都没有。除了discord.js docs。
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。
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/reference/poperators/logical_operators
!watchChannel.id
是布尔人;除非后者是false
,否则它将永远不会等于webhook.channelID
。代码中的所有三个条件也是如此。因此,您的机器人正在删除不是它自己的webhooks,因为当您期望它们是时,if
语句是不正确的。
!==
被称为非身份/严格的不等式操作员。
... [r] eturns
true
如果操作数不相等和/或不相同类型。
https://developer.mozilla.org/en-us/docs/web/javascript/reference/referenty/parison_operators
此(或与双胞胎一起使用的不等式操作员!=
(是您想要使用的操作员。它将正确比较属性。
改进您当前的代码,我们可以...
- 仅从指定的频道获取Webhooks。
- 在循环之前过滤集合。
- 使用现代的
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);
}
您是否查看了Discord.js文档?它提供了您需要知道的所有内容,例如对象,类别,方法/属性之类的东西,类似物品。无论如何,我认为问题是,当您尝试删除使用webhook.delete
的Webhook时,但是当您使用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