discord.js 创建文本频道时出现问题
discord.js problem with creating text channel
今天我创建了一个使用“!new”命令的机器人,但后来我遇到了问题。
“!new”命令创建一个名为 "support-1" 的频道,但是当您重新输入“!new”时,它会再次创建一个同名的频道。现在我有一个问题:如何创建一个命令来创建最终按升序编号的 "support" 频道? ("support-1"、"support-2" 等)我的代码:
message.guild.createChannel(`ticket-${message.author.id}`, "text")
问题是我不知道要制作 ir 所以它按从 0 到 ∞ 的顺序创建频道!
如果你想真正正确地做到这一点,你可以从公会中获取所有频道。过滤掉那些不是文本通道的。从该集合中,过滤掉没有名称 support-(number)
的频道。从您留下的集合中,找到编号最大的那个,然后用该编号 + 1 创建一个新频道。
这是一些示例代码。对其进行测试以确保其正常工作。
bot.on("message", async message => {
if (message.author.bot) return;
if (message.content.startsWith('!new')) {
// Fetch all the channels in the guild.
let allChannels = message.guild.channels;
// Filter out all the non-text channels.
let textChannels = allChannels.filter((channel) => {
return channel.type === "text";
});
// Filter out all the text channels whose name isn't 'support-(number)'.
let supportChannels = textChannels.filter((textChannel) => {
// Checks whether a channel name has format 'support-(number)'. Look into Regex for more info.
return textChannel.name.match(/^(support-)\d+$/g);
});
// Check if there are any support channels.
if (supportChannels.length) {
// Get the numbers from the channel name.
let numbers = supportChannels.map((supportChannel) => {
return parseInt(supportChannel.name.split('-')[1]);
});
// Get the highest number from the array.
let highestNumber = Math.max(...numbers);
// Create a new support channel with the highest number + 1.
message.guild.createChannel(`support-${highestNumber+1}`, 'text');
} else {
// There are no support channels, thus create the first one.
message.guild.createChannel('support-1', 'text');
}
}
});
今天我创建了一个使用“!new”命令的机器人,但后来我遇到了问题。 “!new”命令创建一个名为 "support-1" 的频道,但是当您重新输入“!new”时,它会再次创建一个同名的频道。现在我有一个问题:如何创建一个命令来创建最终按升序编号的 "support" 频道? ("support-1"、"support-2" 等)我的代码:
message.guild.createChannel(`ticket-${message.author.id}`, "text")
问题是我不知道要制作 ir 所以它按从 0 到 ∞ 的顺序创建频道!
如果你想真正正确地做到这一点,你可以从公会中获取所有频道。过滤掉那些不是文本通道的。从该集合中,过滤掉没有名称 support-(number)
的频道。从您留下的集合中,找到编号最大的那个,然后用该编号 + 1 创建一个新频道。
这是一些示例代码。对其进行测试以确保其正常工作。
bot.on("message", async message => {
if (message.author.bot) return;
if (message.content.startsWith('!new')) {
// Fetch all the channels in the guild.
let allChannels = message.guild.channels;
// Filter out all the non-text channels.
let textChannels = allChannels.filter((channel) => {
return channel.type === "text";
});
// Filter out all the text channels whose name isn't 'support-(number)'.
let supportChannels = textChannels.filter((textChannel) => {
// Checks whether a channel name has format 'support-(number)'. Look into Regex for more info.
return textChannel.name.match(/^(support-)\d+$/g);
});
// Check if there are any support channels.
if (supportChannels.length) {
// Get the numbers from the channel name.
let numbers = supportChannels.map((supportChannel) => {
return parseInt(supportChannel.name.split('-')[1]);
});
// Get the highest number from the array.
let highestNumber = Math.max(...numbers);
// Create a new support channel with the highest number + 1.
message.guild.createChannel(`support-${highestNumber+1}`, 'text');
} else {
// There are no support channels, thus create the first one.
message.guild.createChannel('support-1', 'text');
}
}
});