feat: Proper error handling
- Bump version to 0.8.0 - Added discord error struct - All errors now implement std::error::Error - Implemented error handler instead of relying on default - Fixed bug where you could send answer on a completed quest
This commit is contained in:
parent
38e3f149b1
commit
99812c5d7c
14 changed files with 163 additions and 138 deletions
|
|
@ -21,11 +21,15 @@ pub async fn answer(
|
|||
#[description = "Attachment answer to the quest"]
|
||||
file3: Option<Attachment>,
|
||||
) -> Result<(), Error> {
|
||||
let mut account = fetch_or_init_account(&ctx.data().config, ctx.author().id.to_string());
|
||||
|
||||
if let Some(_) = account.quests_completed.iter().find(|qid| **qid == quest_id) {
|
||||
return Err(Error::QuestIsCompleted(quest_id));
|
||||
}
|
||||
|
||||
let quests = ctx.data().config.load_quests();
|
||||
let Some(quest) = quests.iter().find(|q| q.id == quest_id) else {
|
||||
let reply_string = format!("Quest #{quest_id} not found.");
|
||||
ctx.reply(reply_string).await?;
|
||||
return Ok(());
|
||||
return Err(Error::QuestNotFound(quest_id));
|
||||
};
|
||||
|
||||
let mut files: Vec<Attachment> = Vec::new();
|
||||
|
|
@ -36,9 +40,7 @@ pub async fn answer(
|
|||
}
|
||||
|
||||
if text.is_none() && files.len() == 0 {
|
||||
let reply_string = "Please specify text or at least one attachment.".to_string();
|
||||
ctx.reply(reply_string).await?;
|
||||
return Ok(());
|
||||
return Err(Error::NoContent);
|
||||
}
|
||||
|
||||
let text_ans = match text {
|
||||
|
|
@ -90,7 +92,7 @@ pub async fn answer(
|
|||
let reply_string = "Your answer has been posted.".to_string();
|
||||
ctx.reply(reply_string).await?;
|
||||
|
||||
while let Some(press) = ComponentInteractionCollector::new(ctx)
|
||||
if let Some(press) = ComponentInteractionCollector::new(ctx)
|
||||
.filter(move |press| press.data.custom_id.starts_with(&ctx_id.to_string()))
|
||||
.await
|
||||
{
|
||||
|
|
@ -108,7 +110,6 @@ pub async fn answer(
|
|||
let content: String;
|
||||
if is_approved {
|
||||
let mut no_errors = true;
|
||||
let mut account = fetch_or_init_account(&ctx.data().config, ctx.author().id.to_string());
|
||||
if let Err(error) = quest.complete_for_account(&mut account) {
|
||||
eprintln!("{error}");
|
||||
no_errors = false;
|
||||
|
|
@ -137,9 +138,6 @@ pub async fn answer(
|
|||
};
|
||||
let dm_builder = CreateMessage::new().content(content);
|
||||
ctx.author().dm(ctx, dm_builder).await?;
|
||||
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue