-
Notifications
You must be signed in to change notification settings - Fork 30
refactor: use embed in !oc command + add sendEmbedToChannel to ChatService #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<void> { | ||
| 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("🚫 Canal 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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<void> { | ||
| 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<void> { | ||
| const channel = await this.client.channels.fetch(channelId); | ||
|
|
||
| if (!channel || !channel.isTextBased()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's code duplication in l.20-22 and l.10-12 Could improve with a function that always returns a |
||
| 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, | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We normally use this command when someone joins and starts asking if someone can help them with some PC problem or GTA Cheat or whatever.
If you want to think of a styling improvement, maybe markdown in the message body would be enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes Markdown is another easy solution, i usually use embeds because you can input footers and/or icons on them, i usually produce hundreds of them and they are not THAT heavy, but they are simple to edit and manipulate
The idea was to get rid of simple texting, which can introduce ping flaws, but Markdown also solves it yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding
!oc @someUserToBeMentionedwould be a lukewarm feature but I can see some general value in case the chat is too long already.Maybe it's a nice-to-have and nice-to-have's aren't that problematic for this community-based bot.
As for the embed, shouldn't be a problem as long as it doesn't occupy a lot of the user-space.
Just my 2 cents. I'm not a maintainer, just a casual contributor