diff --git a/application/command/onlyCodeQuestionsCommand.ts b/application/command/onlyCodeQuestionsCommand.ts index 2aea596..4d63369 100644 --- a/application/command/onlyCodeQuestionsCommand.ts +++ b/application/command/onlyCodeQuestionsCommand.ts @@ -1,19 +1,31 @@ import { Command, Context } from "../../types"; import ChatService from "../../domain/service/chatService"; +import { EmbedBuilder } from "discord.js"; export default class OnlyCodeQuestionsCommand implements Command { readonly name = "!oc"; private chatService: ChatService; - private readonly message: string = - ":warning: Este servidor é APENAS para questões relacionadas com programação! :warning:"; - constructor(chatService: ChatService) { this.chatService = chatService; } async execute(context: Context): Promise { - await this.chatService.sendMessageToChannel(this.message, context.channelId); + const { message, channelId } = context; + + const mentionedUser = message.mentions.users.first(); + const userIdMatch = message.content.match(/<@!?(\d+)>|(\d{17,20})/); + const userId = mentionedUser?.id || userIdMatch?.[1] || userIdMatch?.[2]; + const content = userId ? `<@${userId}>` : undefined; + + const embed = new EmbedBuilder() + .setTitle("🚫 Servidor Exclusivo para Programação") + .setDescription("Este servidor é **APENAS** para questões relacionadas com **programação**!") + .setColor(0xFF0000) + .setFooter({ text: "Por favor mantém o foco no tema certo." }) + .setTimestamp(); + + await this.chatService.sendEmbedToChannel(embed, channelId, content); } } diff --git a/domain/service/chatService.ts b/domain/service/chatService.ts index 0133401..5ea749a 100644 --- a/domain/service/chatService.ts +++ b/domain/service/chatService.ts @@ -1,3 +1,6 @@ +import { EmbedBuilder } from "discord.js"; + export default interface ChatService { sendMessageToChannel(message: string, channelId: string): void; + sendEmbedToChannel(embed: EmbedBuilder, channelId: string, content?: string): void; } diff --git a/infrastructure/service/discordChatService.ts b/infrastructure/service/discordChatService.ts index 90228eb..255c029 100644 --- a/infrastructure/service/discordChatService.ts +++ b/infrastructure/service/discordChatService.ts @@ -1,4 +1,4 @@ -import { Client } from "discord.js"; +import { Client, EmbedBuilder, TextChannel } from "discord.js"; import ChatService from "../../domain/service/chatService"; export default class DiscordChatService implements ChatService { @@ -7,14 +7,23 @@ export default class DiscordChatService implements ChatService { async sendMessageToChannel(message: string, channelId: string): Promise { const channel = await this.client.channels.fetch(channelId); - if (channel === null) { - throw new Error(`Channel with id ${channelId} not found!`); + if (!channel || !channel.isTextBased()) { + throw new Error(`Channel with id ${channelId} is not a text channel or was not found!`); } - if (!channel.isText()) { - throw new Error(`Channel with id ${channelId} is not a text channel!`); + await (channel as TextChannel).send(message); + } + + async sendEmbedToChannel(embed: EmbedBuilder, channelId: string, content?: string): Promise { + const channel = await this.client.channels.fetch(channelId); + + if (!channel || !channel.isTextBased()) { + throw new Error(`Channel with id ${channelId} is not a text channel or was not found!`); } - channel.send(message); + await (channel as TextChannel).send({ + embeds: [embed], + content: content ?? undefined, + }); } }