如何使用discord.js删除var名称的通道



我正在创建一个discord bot,我想创建一个命令来按频道名称删除频道(0,1,2,3,4,5,6,7,8,9,…(。我想删除的频道将有一个单独的数字作为名称,所以我使用了for循环。这是我的密码;我不知道为什么不起作用,提前谢谢你。

const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = {
name: 'delchannels',
execute(message) {
for (let i = 0; i < 10; i++) {
let fetchedChannel = message.guild.channels.cache.find(i);
fetchedChannel.delete();
}
},
};

哦,是的,我有这个错误:

TypeError: fn is not a function
at Map.find (C:UsersBillyLeBossWoulaDocumentsGitHubMrSmithnode_modules@discordjscollectiondistindex.js:161:17)
at Object.execute (C:UsersBillyLeBossWoulaDocumentsGitHubMrSmithcommandesdelchannels.js:7:63)
at Client.<anonymous> (C:UsersBillyLeBossWoulaDocumentsGitHubMrSmithindex.js:26:38)
at Client.emit (events.js:314:20)
at MessageCreateAction.handle (C:UsersBillyLeBossWoulaDocumentsGitHubMrSmithnode_modulesdiscord.jssrcclientactionsMessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:UsersBillyLeBossWoulaDocumentsGitHubMrSmithnode_modulesdiscord.jssrcclientwebsockethandlersMESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:UsersBillyLeBossWoulaDocumentsGitHubMrSmithnode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:UsersBillyLeBossWoulaDocumentsGitHubMrSmithnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:UsersBillyLeBossWoulaDocumentsGitHubMrSmithnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:301:10)
at WebSocket.onMessage (C:UsersBillyLeBossWoulaDocumentsGitHubMrSmithnode_moduleswslibevent-target.js:125:16)

Collection.find()需要一个函数参数。它将在数组的每个元素上运行给定的函数,并返回满足该函数的第一个元素。此外,i是一个数字,因此您也必须将频道名称转换为数字。

for (let i = 0; i < 10; i++) {
let fetchedChannel = message.guild.channels.cache.find(
(channel) => +channel.name === i
);
fetchedChannel.delete();
}

const channels = [
{
name: '0',
id: 123,
},
{
name: '1',
id: 234,
},
{
name: '2',
id: 345,
},
{
name: '3',
id: 456,
},
];
for (var i = 0; i < channels.length; i++) {
console.log(channels.find((channel) => +channel.name === i).id);
}


作为一个提示,我相信你可以这样缩短你的函数:

for (let i = 0; i < 10; i++) {
let fetchedChannel = message.guild.channels.cache.find((c) => +c.name === i);
fetchedChannel.delete();
}
// change to:
message.guild.channels.cache
.filter((c) => /^[0-9]$/.test(c.name))
.forEach((c) => c.delete());

最新更新