feat!: implemented answer buttons
- Also you can /init without restarting bot BREAKING CHANGE: Changed type of Data::discord to Arc<Mutex<DiscordConfig>, removed field pending_answers from DiscordConfig
This commit is contained in:
parent
3f7e6313b0
commit
1ae57ad358
4 changed files with 70 additions and 23 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use poise::serenity_prelude::{Attachment, CreateAttachment, CreateMessage, Mentionable};
|
||||
use poise::serenity_prelude::{Attachment, ComponentInteractionCollector, CreateActionRow, CreateAttachment, CreateButton, CreateMessage, EditMessage, Mentionable};
|
||||
|
||||
use crate::{Context, Error};
|
||||
|
||||
|
|
@ -14,7 +14,11 @@ pub async fn answer(
|
|||
#[description = "Text answer to the quest"]
|
||||
text: Option<String>,
|
||||
#[description = "Attachment answer to the quest"]
|
||||
files: Vec<Attachment>,
|
||||
file1: Option<Attachment>,
|
||||
#[description = "Attachment answer to the quest"]
|
||||
file2: Option<Attachment>,
|
||||
#[description = "Attachment answer to the quest"]
|
||||
file3: Option<Attachment>,
|
||||
) -> Result<(), Error> {
|
||||
let quests = ctx.data().config.load_quests();
|
||||
let Some(quest) = quests.iter().find(|q| q.id == quest_id) else {
|
||||
|
|
@ -22,6 +26,13 @@ pub async fn answer(
|
|||
ctx.reply(reply_string).await?;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let mut files: Vec<Attachment> = Vec::new();
|
||||
for file in [file1, file2, file3] {
|
||||
if let Some(f) = file {
|
||||
files.push(f);
|
||||
}
|
||||
}
|
||||
|
||||
if text.is_none() && files.len() == 0 {
|
||||
let reply_string = "Please specify text or at least one attachment.".to_string();
|
||||
|
|
@ -38,7 +49,7 @@ pub async fn answer(
|
|||
"\nPassed answer has attachments.".to_string()
|
||||
};
|
||||
|
||||
let content = format!("# From: {user}\n\
|
||||
let content = format!("## From: {user}\n\
|
||||
### Quest #{quest_id}: {quest_name}\n\
|
||||
### Expected answer:\n\
|
||||
||{quest_answer}||{text_ans}{attachment_notice}",
|
||||
|
|
@ -53,16 +64,47 @@ pub async fn answer(
|
|||
let attachment = CreateAttachment::url(ctx, &file.url).await?;
|
||||
attachments.push(attachment);
|
||||
}
|
||||
|
||||
let ctx_id = ctx.id();
|
||||
let approve_id = format!("{ctx_id}approve");
|
||||
let reject_id = format!("{ctx_id}reject");
|
||||
|
||||
let ans_channel = ctx.data().discord.answers_channel;
|
||||
let message = CreateMessage::new()
|
||||
.content(content)
|
||||
.files(attachments);
|
||||
let components = CreateActionRow::Buttons(vec![
|
||||
CreateButton::new(&approve_id).label("Approve".to_string()),
|
||||
CreateButton::new(&reject_id).label("Reject".to_string()),
|
||||
]);
|
||||
|
||||
ans_channel.send_message(ctx, message).await?;
|
||||
let ans_channel = {
|
||||
let discord = ctx.data().discord.clone();
|
||||
let guard = discord.lock().expect("should not be locked");
|
||||
guard.answers_channel
|
||||
};
|
||||
let builder = CreateMessage::new()
|
||||
.content(content.clone())
|
||||
.files(attachments)
|
||||
.components(vec![components]);
|
||||
|
||||
let mut message = ans_channel.send_message(ctx, builder).await?;
|
||||
|
||||
let reply_string = "Your answer has been posted.".to_string();
|
||||
ctx.reply(reply_string).await?;
|
||||
|
||||
while let Some(press) = ComponentInteractionCollector::new(ctx)
|
||||
.filter(move |press| press.data.custom_id.starts_with(&ctx_id.to_string()))
|
||||
.await
|
||||
{
|
||||
let admin = press.user.mention();
|
||||
let is_approved = press.data.custom_id == approve_id;
|
||||
let content = if is_approved {
|
||||
format!("{content}\nApproved by: {admin}")
|
||||
} else {
|
||||
format!("~~{content}~~\nRejected by: {admin}")
|
||||
};
|
||||
|
||||
let builder = EditMessage::new().content(content).components(Vec::new());
|
||||
message.edit(ctx, builder).await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue