在创建channel1
、channel2
、channel3
和channel4
之前,我需要检查它们是否存在。
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' });
}
}