-
Notifications
You must be signed in to change notification settings - Fork 28
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
Perguntas Anónimas #62
base: main
Are you sure you want to change the base?
Changes from all commits
25b6cbf
4f191cf
f4cad35
430ed44
2322d4c
a4e875d
f1c3cc5
a97b0ad
c7e1138
0636364
9aa17d6
ece7de0
d24b8ae
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Message, Client, MessageEmbed, MessageActionRow, MessageButton } from "discord.js"; | ||
import { ChannelSlug } from "../../types"; | ||
import QuestionChatService from "./sendMessageToChannel/sendPerguntaToChannel"; | ||
import ChatService from "../../domain/service/chatService"; | ||
import ChannelResolver from "../../domain/service/channelResolver"; | ||
|
||
const askedRecently = new Set(); | ||
const messageSize = 1500; | ||
class ReadDirectMessageUseCase { | ||
constructor( | ||
private message: Message, | ||
private client: Client, | ||
private channelResolver: ChannelResolver, | ||
private chatService: ChatService, | ||
private questionChatService: QuestionChatService | ||
) {} | ||
|
||
async isValid() { | ||
if (askedRecently.has(this.message.author.id) && this.message.channel.type === "DM") { | ||
this.message.channel.send("Ainda não podes enviar outra pergunta. Tenta mais tarde."); | ||
return false; | ||
} | ||
if (this.message.author.id === this.client.user?.id || this.message.channel.type !== "DM") { | ||
return false; | ||
} | ||
if ( | ||
this.message.channel.type === "DM" && | ||
this.message.content.startsWith("!pergunta") && | ||
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. ☝️ |
||
this.message.content.split(" ").length > 1 && | ||
this.message.content.length <= messageSize | ||
) { | ||
askedRecently.add(this.message.author.id); | ||
setTimeout(() => { | ||
askedRecently.delete(this.message.author.id); | ||
}, 60000); | ||
return true; | ||
} | ||
this.chatService.sendDirectMessageToUser( | ||
this.message.author.id, | ||
"Por favor usa o seguinte formato:\n!pergunta <mensagem>" | ||
); | ||
|
||
return false; | ||
} | ||
|
||
async approveMessage(): Promise<void> { | ||
const channelAnonQuestion = this.client.channels.cache.get(this.channelResolver.getBySlug(ChannelSlug.QUESTION)); | ||
// const questionChatService: QuestionChatService = new DiscordEmbedService(this.client); | ||
const originalUserId = this.message.author.id; | ||
const sentence = this.message.content.split(" ").slice(1).join(" "); | ||
const buttons = new MessageActionRow().addComponents( | ||
new MessageButton().setLabel("Aprovar").setStyle("SUCCESS").setCustomId(`apr${originalUserId}`), | ||
new MessageButton().setLabel("Eliminar").setStyle("DANGER").setCustomId(`rej${originalUserId}`) | ||
); | ||
const mensagem = new MessageEmbed().setColor("#0099ff").setTitle("Pergunta Anónima").setDescription(sentence); | ||
|
||
await this.questionChatService.sendEmbedToChannel( | ||
mensagem, | ||
buttons, | ||
this.channelResolver.getBySlug(ChannelSlug.MOD_CHANNEL) | ||
); | ||
|
||
await this.chatService.sendDirectMessageToUser( | ||
this.message.author.id, | ||
`A tua pergunta foi colocada com sucesso.\nApós aprovação poderás visualizar no ${channelAnonQuestion} ` | ||
); | ||
} | ||
} | ||
|
||
export default ReadDirectMessageUseCase; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { MessageEmbed, MessageActionRow } from "discord.js"; | ||
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. Está a importar coisas específicas do Discord.js. Aqui deveria estar a importar alguma abstração. Podemos discutir isto com o @Adjilino que ele tem lá uns pontos semelhantes também no MR dele. |
||
|
||
export default interface SendMessageToChannel { | ||
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. O nome do ficheiro está diferente do nome da classe e presumo que o queiras deixar todo em inglês? |
||
sendEmbedToChannel(message: MessageEmbed, row: MessageActionRow, channelId: string): void; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export default interface ChatService { | ||
sendMessageToChannel(message: string, channelId: string): void; | ||
deleteMessageFromChannel(messageId: string, channelId: string): void; | ||
sendDirectMessageToUser(message: string, userId: string): void; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Client, MessageEmbed, MessageActionRow } from "discord.js"; | ||
|
||
export default class DiscordEmbedService { | ||
constructor(private client: Client) {} | ||
|
||
async sendEmbedToChannel(embed: MessageEmbed, row: MessageActionRow, 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.isText()) { | ||
throw new Error(`Channel with id ${channelId} is not a text channel!`); | ||
} | ||
|
||
channel.send({ embeds: [embed], components: [row] }); | ||
} | ||
} |
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.
i think there should be a ENUM here for types