import { ApplicationCommandOptionType, ChannelType, ChatInputApplicationCommandData, ChatInputCommandInteraction, EmbedBuilder, Guild } from "discord.js"; import { client } from "../index"; import { Command } from "../types/Command"; import { clearAllMsg } from "../utils/music/Utils"; import { default_content, default_embed, default_image, getButtons } from "../utils/music/Config"; import { DB } from "../utils/Database"; import { Logger } from "../utils/Logger"; /** channel 명령어 */ export default class implements Command { /** 해당 명령어 설명 */ name = "channel"; visible = true; aliases: string[] = []; description: string = "채널 생성 또는 연결"; metaData: ChatInputApplicationCommandData = { name: this.name, description: this.description, options: [ { type: ApplicationCommandOptionType.Subcommand, name: "make", description: "채널 만들기", }, { type: ApplicationCommandOptionType.Subcommand, name: "register", description: "기존 채팅채널 등록", options: [ { type: ApplicationCommandOptionType.Channel, name: "channel", description: "등록할 채널 (선택)", channel_types: [ChannelType.GuildText], }, { type: ApplicationCommandOptionType.String, name: "channel_id", description: "채널 ID 또는 #멘션 (선택)", }, ] }, ], }; /** 실행되는 부분 */ async slashRun(interaction: ChatInputCommandInteraction) { const cmd = interaction.options.getSubcommand(); if (cmd === "make") { await interaction.editReply({ embeds: [ await this.channelCreate(interaction.guild) ] }); return; } if (cmd === "register") { const channel = interaction.options.getChannel("channel"); const channelId = interaction.options.getString("channel_id"); await interaction.editReply({ embeds: [ await channelRegister(interaction.guild, channel?.id || channelId) ] }); return; } } async channelCreate(guild: Guild | null): Promise { if (!guild) return client.mkembed({ title: "guild를 가져올수 없습니다.", color: "DarkRed", }); const channel = await guild.channels.create({ name: "MUSIC채널", type: ChannelType.GuildText, topic: "채팅에 노래제목 또는 주소를 입력해 사용하세요.", }).catch(() => null); if (!channel) return client.mkembed({ title: "채널 생성 오류", color: "DarkRed" }); return channelRegister(guild, channel.id); } } export async function channelRegister(guild: Guild | null, channelId: string | null): Promise { if (!guild) return client.mkembed({ title: "guild를 가져올수 없습니다.", color: "DarkRed", }); if (!channelId) return client.mkembed({ title: "채널 아이디 가져오기 오류", color: "DarkRed", }); const channel = guild.channels.cache.get(channelId.replace(/\<|\#|\!|\>/g,"").trim()); if (!channel?.id) return client.mkembed({ title: `${channelId} 채널 가져오기 오류`, color: "DarkRed", }); if (channel.type !== ChannelType.GuildText) return client.mkembed({ title: `<#${channelId}> 채팅 채널이 아닙니다.`, color: "DarkRed", }); await clearAllMsg(channel); const msg = await channel.send({ content: default_content, embeds: [ default_embed(guild.id) ], components: [ getButtons() ], files: [ default_image ], }).catch((err) => { Logger.error(`[Channel] 메세지 생성 실패: ${String(err)}`); return null; }); if (!msg) return client.mkembed({ title: "메세지 생성 오류", color: "DarkRed" }); if (!DB.guild.get(guild.id)) DB.guild.set({ id: guild.id, name: guild.name, channel_id: "", msg_id: "", options: { recommend: false } }); const gdb = DB.guild.get(guild.id); if (!gdb) return client.mkembed({ title: "DB 생성 오류", color: "DarkRed", }); if (!DB.guild.update({ ...gdb, channel_id: channel.id, msg_id: msg.id, })) return client.mkembed({ title: "DB 등록 오류", color: "DarkRed", }); return client.mkembed({ title: `채널(${channel.name}) 생성/등록 완료`, description: `채널: <#${channel.id}>`, }); }