如何让机器人知道如何删除它创建的 webhook 以及通过频道提及的内容
How to make a bot know how to delete webhooks that it made and by channel mentions
你好,我想创建一个 Discord.JS-Commando 命令,如果你 select 一个频道,机器人会删除它在那里拥有的一个 webhook,如果它被命名为 Marker
并且如果它检测是否没有它拥有的名为 Marker
的 webhook 它只是 return message.channel.send("Hey! There's no webhook I own in this channel!")
机器人删除了一个 webhook,即使它没有成功,而且它不在我提到的频道中。我该如何解决这个问题?
在 Google 上搜索,没有任何内容。
除了 discord.js 文档之外,没有任何关于删除 webhooks 的内容。
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。
您看过 discord.js 文档了吗?它提供了您需要知道的一切,例如对象、classes、对象的 methods/properties 和 classes 之类的东西。无论如何,我认为问题在于,当您尝试删除正在使用 webhook.delete
的 webhook 时,但是当您使用 delete
而不带括号时,这意味着您正在尝试访问 属性 delete
,不是方法。正确的方法是调用 webhook.delete();
,因为这会调用 Webhook
class.
中的 delete()
方法
文档就在这里:
Webhook class:https://discord.js.org/#/docs/main/stable/class/Webhook
删除方法:https://discord.js.org/#/docs/main/stable/class/Webhook?scrollTo=delete
if (!watchChannel.id == webhook.channelID) return
if (!webhook.owner.id == `595840576386236437`) return
if (!webhook.name == `Marker`) return
None 这些行正在按您预期的方式工作。
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 运算符。
Returns false
if its single operand can be converted to true
; otherwise, returns true
.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
!watchChannel.id
是一个Boolean;它永远不会等于 webhook.channelID
除非后者是 false
。代码中的所有三个条件也是如此。因此,您的机器人正在删除不属于它自己的 Webhook,因为 if
语句并不像您预期的那样。
!==
被称为 non-identity/strict 不等式 运算符。
...[R]eturns true
if the operands are not equal and/or not of the same type.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
这(或与双胞胎一起使用的不等式运算符 !=
)是您要使用的运算符。它将正确比较属性。
改进您当前的代码,我们可以...
- 仅从指定渠道获取 Webhooks。
- 在循环之前过滤 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);
}
你好,我想创建一个 Discord.JS-Commando 命令,如果你 select 一个频道,机器人会删除它在那里拥有的一个 webhook,如果它被命名为 Marker
并且如果它检测是否没有它拥有的名为 Marker
的 webhook 它只是 return message.channel.send("Hey! There's no webhook I own in this channel!")
机器人删除了一个 webhook,即使它没有成功,而且它不在我提到的频道中。我该如何解决这个问题?
在 Google 上搜索,没有任何内容。 除了 discord.js 文档之外,没有任何关于删除 webhooks 的内容。
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。
您看过 discord.js 文档了吗?它提供了您需要知道的一切,例如对象、classes、对象的 methods/properties 和 classes 之类的东西。无论如何,我认为问题在于,当您尝试删除正在使用 webhook.delete
的 webhook 时,但是当您使用 delete
而不带括号时,这意味着您正在尝试访问 属性 delete
,不是方法。正确的方法是调用 webhook.delete();
,因为这会调用 Webhook
class.
delete()
方法
文档就在这里:
Webhook class:https://discord.js.org/#/docs/main/stable/class/Webhook
删除方法:https://discord.js.org/#/docs/main/stable/class/Webhook?scrollTo=delete
if (!watchChannel.id == webhook.channelID) return if (!webhook.owner.id == `595840576386236437`) return if (!webhook.name == `Marker`) return
None 这些行正在按您预期的方式工作。
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 运算符。
Returns
false
if its single operand can be converted totrue
; otherwise, returnstrue
.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
!watchChannel.id
是一个Boolean;它永远不会等于 webhook.channelID
除非后者是 false
。代码中的所有三个条件也是如此。因此,您的机器人正在删除不属于它自己的 Webhook,因为 if
语句并不像您预期的那样。
!==
被称为 non-identity/strict 不等式 运算符。
...[R]eturns
true
if the operands are not equal and/or not of the same type.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
这(或与双胞胎一起使用的不等式运算符 !=
)是您要使用的运算符。它将正确比较属性。
改进您当前的代码,我们可以...
- 仅从指定渠道获取 Webhooks。
- 在循环之前过滤 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);
}