-
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
Open
MrProSpectrumPT
wants to merge
4
commits into
devpt-org:main
Choose a base branch
from
MrProSpectrumPT:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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("🚫 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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