【发布时间】:2021-01-04 04:38:31
【问题描述】:
基本上正如标题所说。我想知道是否有办法删除所有名称中包含特定字符串的频道
例如18876557 -老
在命令中,删除名称中包含字符串 -old 的所有通道。
【问题讨论】:
-
循环浏览频道并检查他们的名字,我会给你写一个答案
标签: javascript node.js discord discord.js
基本上正如标题所说。我想知道是否有办法删除所有名称中包含特定字符串的频道
例如18876557 -老
在命令中,删除名称中包含字符串 -old 的所有通道。
【问题讨论】:
标签: javascript node.js discord discord.js
您可以使用Array.prototype.forEach() 和Channel.delete()
// iterate a function through all channels in the guild
guild.channels.cache.forEach((channel) => {
if (guild.name.includes('-old')) // if the string '-old' is found within the channel name
channel.delete() // delete the channel
.then(() => console.log(`Deleted ${channel.name}`))
.catch((e) => console.log(`Could not delete ${channel.name} because of ${e}`)) // handle any errors
});
【讨论】:
guild 应该是您想要在其上执行此操作的任何公会。你不能创建一个新的公会。如果这是在命令中,您可以使用message.guild。否则:client.guilds.cache.get('Guild ID').
这很简单,你只需要循环通过公会频道:
for (let channel of guild.channels.cache) {
if (channel.name.includes("-old")) channel.delete();
}
【讨论】: