admin管理员组

文章数量:1405170

Hi I want to make a Discord.JS-Commando mand where if you select a channel, the bot removes a webhook it owns there and if it's named Marker and if it detects if there's no webhook there that it owns named Marker it just return message.channel.send("Hey! There's no webhook I own in this channel!")

The bot deletes a webhook even though it didn't make it, and it's not in the channel I mention. How do I fix this?

Searching it up on Google, there was nothing. There wasn't anything on deleting webhooks except 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}`);
});

I expect the bot to know how to delete the webhook that it made, in a mentioned channel, but the bot doesn't know what webhook to delete.

Hi I want to make a Discord.JS-Commando mand where if you select a channel, the bot removes a webhook it owns there and if it's named Marker and if it detects if there's no webhook there that it owns named Marker it just return message.channel.send("Hey! There's no webhook I own in this channel!")

The bot deletes a webhook even though it didn't make it, and it's not in the channel I mention. How do I fix this?

Searching it up on Google, there was nothing. There wasn't anything on deleting webhooks except 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}`);
});

I expect the bot to know how to delete the webhook that it made, in a mentioned channel, but the bot doesn't know what webhook to delete.

Share Improve this question edited Jul 6, 2019 at 9:14 SomePerson asked Jul 5, 2019 at 0:08 SomePersonSomePerson 1,3095 gold badges19 silver badges48 bronze badges 4
  • First, a little advice: use === instead of == (you can see the difference there and add bracket for your if. Yes one line if doesn't need them, however it's easier to read and way better if you need to edit your code (example, you want to add a console.log? If you put it like this, the second statement won't be in the if. You'll have to add the bracket, but you can forget, leading to wrong debugging or behaviour in your code) – JackRed Commented Jul 5, 2019 at 11:36
  • Do you have an error? Are you sure the webhook exist? Are you sure the owner is the good id? Did you try to log webhook? – JackRed Commented Jul 5, 2019 at 11:40
  • 1. Error in Discord: TypeError: Cannot read property 'id' of undefined, 2. Yes, it is named "Marker" and owned by 595840576386236437. 3. I made sure the bot's ID and the owner.ID matched. 4. Not yet. – SomePerson Commented Jul 5, 2019 at 18:42
  • Anyways, I got the answer to just put it as webhook.delete() and now the problem is that it deletes all webhooks it sees, even if I make it. – SomePerson Commented Jul 6, 2019 at 9:17
Add a ment  | 

2 Answers 2

Reset to default 3
if (!watchChannel.id == webhook.channelID) return
if (!webhook.owner.id == `595840576386236437`) return
if (!webhook.name == `Marker`) return

None of these lines are working as you expect them to.

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


! acts as the logical NOT operator.

Returns false if its single operand can be converted to true; otherwise, returns true.

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

!watchChannel.id is a Boolean; it will never equal webhook.channelID unless the latter is false. The same goes for all three conditions in your code. Therefore, your bot is deleting Webhooks that aren't its own because the if statements are not true when you expected them to be.


!== is known as the non-identity/strict inequality operator.

...[R]eturns true if the operands are not equal and/or not of the same type.

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

This (or the inequality operator != to go along with the twins) is the operator your want to be using. It will correctly pare the properties.


Improving your current code, we can...

  • Fetch the Webhooks only from the specified channel.
  • Filter the Collection before looping through.
  • Use a modern for...of loop which will work correctly with asynchronous code.
  • Make sure to catch all rejected promises.
  • Get into the habit of using the identity operator === instead of the equality operator ==. See here for reasoning.
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);
}

Have you looked at the discord.js documentation? It provides everything that you need to know, such as things like objects, classes, methods/properties for the objects and classes, stuff like that. Anyway, I think the problem is that when you try to delete the webhook you are using webhook.delete but when you use delete without the parentheses means that you are trying to access the property delete, not the method. The correct way to do it would be to call webhook.delete(); as this calls the delete() method from the Webhook class.

It's right here on the docs:

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

Delete method: https://discord.js/#/docs/main/stable/class/Webhook?scrollTo=delete

本文标签: javascriptHow to make a bot know how to delete webhooks that it made and by channel mentionsStack Overflow