需要检查类别中是否存在频道,如果没有,则在 Discord 中创建一个新频道.js



在创建channel1channel2channel3channel4之前,我需要检查它们是否存在。

const Discord = require('discord.js');
const disbut = require('discord-buttons');
const client = new Discord.Client();
const guild = new Discord.Guild();
const { prefix, token, categoryID } = require('./config.json');
const { MessageEmbed } = require('discord.js');
const command = require('./command');
client.on('ready', () => {
console.log('The client is ready!')
});
client.login(token);
const embed = new MessageEmbed()
.setTitle("Title")
.setColor("#ff00ff")
.setDescription("Description")
.setFooter("Footer")
.setImage("myimage")
.setTimestamp()
client.on("message", async (message) => {
if (message.content == "server") {
try {
await message.guild.setIcon('./icon.png');
await message.guild.setName("My Server");
await message.guild.channels.create("channel1", { type: 'text', parent: categoryID });
await message.guild.channels.create("channel2", { type: 'text', parent: categoryID });
await message.guild.channels.create("channel3", { type: 'text', parent: categoryID });
await message.guild.channels.create("channel4", { type: 'text', parent: categoryID });
message.channel.send({embed});
} catch {
message.channel.send("Unknown error occurred.");
}
}
});

请帮忙!

使用此代码:

//msg is an instance of Message
const { guild } = msg;
await guild.channels.fetch();
for (let i = 1; i < 5; i++) {
const channel = await guild.channels.cache.find(c => c.name === `channel${i}`)
if (!channel) {
//channel not found
}
if (channel.parentId !== 'CATEGORY_ID') {
//not in the category
}
}

这将检查该频道是否存在于类别中。

如果未找到,此代码将创建通道

const { guild } = msg;
await guild.channels.fetch();
for (let i = 1; i < 5; i++) {
const channel = await guild.channels.cache.find(c => c.name === `channel${i}`)
if (!channel || channel.parentId !== 'categoryId') {
guild.channels.create(`channel${i}`, { type: 'text' });
}
}

相关内容

  • 没有找到相关文章

最新更新