From 6f94785a7ba1a1a1492bc46cb9b22776a20faf13 Mon Sep 17 00:00:00 2001 From: space2lim Date: Wed, 29 Jan 2025 00:50:23 +0900 Subject: [PATCH] minor fix --- app.js | 30 ++++++++++++++++++++++++++++++ commands/color.js | 2 -- commands/ping.js | 2 -- commands/role_id.js | 11 +++++++++++ events/interactionCreate.js | 18 +++++++++++++++++- events/messageCreate.js | 27 ++++++++++++++++++++++++--- 6 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 commands/role_id.js diff --git a/app.js b/app.js index f07ebe6..c8d0bf7 100644 --- a/app.js +++ b/app.js @@ -25,4 +25,34 @@ client.on('interactionCreate', interactionCreateEvent); client.on('messageCreate', messageCreateEvent); client.on('messageReactionAdd', messageReactionAddEvent); + +import { REST, Routes } from 'discord.js'; + +const commands = [ + { + name: 'ping', + description: 'Pong! 으로 대답합니다.', + }, + { + name: 'color', + description: '아이디 색상을 변경합니다.', + }, + { + name: 'role-id', + description: '서버 역할 목록과 ID를 확인합니다. (Dev)', + }, +]; + +const rest = new REST({ version: '10' }).setToken(process.env.TOKEN); +(async () => { + try { + console.log('Started refreshing application (/) commands.'); + await rest.put(Routes.applicationCommands(process.env.CLI_ID), { body: commands }); + console.log('Successfully reloaded application (/) commands.'); + } catch (error) { + console.error(error); + } +})(); + + client.login(TOKEN); \ No newline at end of file diff --git a/commands/color.js b/commands/color.js index 73be9f0..dfc7205 100644 --- a/commands/color.js +++ b/commands/color.js @@ -11,8 +11,6 @@ const roles = { export default { - name: 'color', - description: '아이디 색상을 변경합니다.', async execute(interaction) { const sentMessage = await interaction.reply({ content: '아이디로 보여질 색상을 선택하세요.', diff --git a/commands/ping.js b/commands/ping.js index a83d9fd..b505f89 100644 --- a/commands/ping.js +++ b/commands/ping.js @@ -1,6 +1,4 @@ export default { - name: 'ping', - description: 'Pong! 으로 대답합니다.', async execute(interaction) { await interaction.reply('Pong!'); }, diff --git a/commands/role_id.js b/commands/role_id.js new file mode 100644 index 0000000..efc490f --- /dev/null +++ b/commands/role_id.js @@ -0,0 +1,11 @@ +export default { + async execute(interaction) { + // 명령어가 실행된 서버의 역할 목록 가져오기 + const roles = interaction.guild.roles.cache.map( + role => `${role.name}: ${role.id}` + ); + + // 응답으로 역할 목록 전송 + await interaction.reply(`서버 역할 목록:\n${roles.join('\n')}`); + }, +}; diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 9009f60..7b2b01a 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,16 +1,32 @@ import pingCommand from '../commands/ping.js'; import colorCommand from '../commands/color.js'; +import roleIDCommand from '../commands/role_id.js' const commands = { ping: pingCommand, color: colorCommand, + 'role-id': roleIDCommand, // 문자열로 키 작성 }; export default async function interactionCreateEvent(interaction) { if (!interaction.isChatInputCommand()) return; + // 대괄호 표기법으로 명령어 찾기 const command = commands[interaction.commandName]; if (command) { - await command.execute(interaction); + try { + await command.execute(interaction); + } catch (error) { + console.error(`Error executing ${interaction.commandName}:`, error); + await interaction.reply({ + content: '명령어 실행 중 오류가 발생했습니다!', + ephemeral: true, + }); + } + } else { + await interaction.reply({ + content: '알 수 없는 명령어입니다!', + ephemeral: true, + }); } } \ No newline at end of file diff --git a/events/messageCreate.js b/events/messageCreate.js index 771e603..9936cea 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -1,3 +1,6 @@ +import { EmbedBuilder } from 'discord.js'; +import { getMetaData } from '../feature/utility.js'; // 메타데이터 추출 함수 + export default async function messageCreateEvent(message) { const badWords = ['권태웅', '태웅', '웅태']; @@ -5,8 +8,26 @@ export default async function messageCreateEvent(message) { await message.channel.send('어허 나쁜말 쓰지 마세요'); } - if (message.content === '!역할확인') { - const roles = message.guild.roles.cache.map(role => `${role.name}: ${role.id}`); - await message.channel.send(`서버 역할 목록:\n${roles.join('\n')}`); + + // URL 감지 및 메타데이터 처리 + const urlPattern = /https?:\/\/(www\.)?(m\.)?gall\.dcinside\.com\/[^\s]*/g; + const urls = message.content.match(urlPattern); + + if (urls && urls.length > 0) { + try { + const metaData = await getMetaData(urls[0]); // 첫 번째 URL의 메타데이터 추출 + + const embed = new EmbedBuilder() + .setColor(0x0099FF) + .setTitle(metaData.title || '제목 없음') + .setURL(urls[0]) + .setDescription(metaData.description || '설명 없음') + .setImage(metaData.image || null); + + await message.channel.send({ embeds: [embed] }); + } catch (error) { + console.error('메타데이터 처리 중 오류:', error); + await message.channel.send('URL에서 메타데이터를 가져오는 중 오류가 발생했습니다.'); + } } } \ No newline at end of file