【发布时间】: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