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
|
|
@ -3,12 +3,12 @@ use std::{future, path::Path, str::FromStr};
|
|||
use poise::serenity_prelude::{CreateMessage, EditMessage, Message, futures::StreamExt};
|
||||
use squad_quest::{SquadObject, quest::{Quest, QuestDifficulty}};
|
||||
use toml::value::Date;
|
||||
use crate::{Context, Error, commands::ERROR_MSG};
|
||||
use crate::{Context, Error};
|
||||
|
||||
async fn find_quest_message(ctx: Context<'_>, id: u16) -> Option<Message>{
|
||||
let _ = ctx.defer().await;
|
||||
async fn find_quest_message(ctx: Context<'_>, id: u16) -> Result<Option<Message>, Error>{
|
||||
ctx.defer().await?;
|
||||
let dc = ctx.data().discord.clone();
|
||||
let channel = {
|
||||
let dc = ctx.data().discord.clone();
|
||||
let guard = dc.lock().expect("shouldn't be locked");
|
||||
guard.quests_channel
|
||||
};
|
||||
|
|
@ -21,7 +21,7 @@ async fn find_quest_message(ctx: Context<'_>, id: u16) -> Option<Message>{
|
|||
future::ready(m.content.contains(&format!("#{id}")))
|
||||
})
|
||||
.collect::<Vec<_>>().await;
|
||||
messages.first().cloned()
|
||||
Ok(messages.first().cloned())
|
||||
}
|
||||
|
||||
fn make_quest_message_content(quest: &Quest) -> String {
|
||||
|
|
@ -162,14 +162,9 @@ pub async fn create(
|
|||
};
|
||||
|
||||
let path = conf.full_quests_path();
|
||||
|
||||
let reply_string = match quest.save(path) {
|
||||
Ok(_) => format!("Created quest #{}", quest.id),
|
||||
Err(error) => {
|
||||
eprintln!("{error}");
|
||||
format!("{ERROR_MSG}")
|
||||
},
|
||||
};
|
||||
|
||||
quest.save(path)?;
|
||||
let reply_string = format!("Created quest #{}", quest.id);
|
||||
|
||||
ctx.reply(reply_string).await?;
|
||||
|
||||
|
|
@ -207,9 +202,7 @@ pub async fn update(
|
|||
let conf = &ctx.data().config;
|
||||
let quests = conf.load_quests();
|
||||
let Some(quest) = quests.iter().find(|q| q.id == id) else {
|
||||
let reply_string = format!("Quest #{id} not found");
|
||||
ctx.reply(reply_string).await?;
|
||||
return Ok(());
|
||||
return Err(Error::QuestNotFound(id));
|
||||
};
|
||||
|
||||
let difficulty = match difficulty {
|
||||
|
|
@ -248,8 +241,8 @@ pub async fn update(
|
|||
let content = make_quest_message_content(&new_quest);
|
||||
let builder = EditMessage::new().content(content);
|
||||
|
||||
let message = find_quest_message(ctx, id);
|
||||
if let Some(mut message) = message.await {
|
||||
let message = find_quest_message(ctx, id).await?;
|
||||
if let Some(mut message) = message {
|
||||
message.edit(ctx, builder).await?;
|
||||
} else {
|
||||
let reply_string = format!("Quest #{id} is public, but its message was not found in the quest channel",
|
||||
|
|
@ -259,13 +252,8 @@ pub async fn update(
|
|||
}
|
||||
|
||||
let path = conf.full_quests_path();
|
||||
let reply_string = match new_quest.save(path) {
|
||||
Err(error) => {
|
||||
eprintln!("{error}");
|
||||
ERROR_MSG.to_string()
|
||||
},
|
||||
Ok(_) => format!("Updated quest #{id}"),
|
||||
};
|
||||
new_quest.save(path)?;
|
||||
let reply_string = format!("Updated quest #{id}");
|
||||
ctx.reply(reply_string).await?;
|
||||
|
||||
Ok(())
|
||||
|
|
@ -285,15 +273,11 @@ pub async fn publish(
|
|||
let mut quests = ctx.data().config.load_quests();
|
||||
|
||||
let Some(quest) = quests.iter_mut().find(|q| q.id == id) else {
|
||||
let reply_string = format!("Quest #{id} not found");
|
||||
ctx.reply(reply_string).await?;
|
||||
return Ok(());
|
||||
return Err(Error::QuestNotFound(id));
|
||||
};
|
||||
|
||||
if quest.public {
|
||||
let reply_string = format!("Quest #{id} is already public");
|
||||
ctx.reply(reply_string).await?;
|
||||
return Ok(());
|
||||
return Err(Error::QuestIsPublic(id));
|
||||
}
|
||||
|
||||
quest.public = true;
|
||||
|
|
@ -311,27 +295,15 @@ pub async fn publish(
|
|||
|
||||
let message = channel.send_message(ctx, builder).await?;
|
||||
|
||||
let result = {
|
||||
{
|
||||
let mut guard = dc.lock().expect("shouldn't be locked");
|
||||
guard.quests_messages.push(message.id);
|
||||
let path = ctx.data().config.full_impl_path().unwrap();
|
||||
guard.save(path.parent().unwrap_or(Path::new("")).to_owned())
|
||||
guard.save(path.parent().unwrap_or(Path::new("")).to_owned())?
|
||||
};
|
||||
|
||||
if let Err(error) = result {
|
||||
eprintln!("{error}");
|
||||
let reply_string = ERROR_MSG.to_string();
|
||||
ctx.reply(reply_string).await?;
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
let quests_path = ctx.data().config.full_quests_path();
|
||||
if let Err(error) = quest.save(quests_path) {
|
||||
eprintln!("{error}");
|
||||
let reply_string = ERROR_MSG.to_string();
|
||||
ctx.reply(reply_string).await?;
|
||||
return Ok(())
|
||||
}
|
||||
quest.save(quests_path)?;
|
||||
|
||||
let reply_string = format!("Published quest #{id}");
|
||||
ctx.reply(reply_string).await?;
|
||||
|
|
@ -349,18 +321,13 @@ pub async fn delete(
|
|||
ctx: Context<'_>,
|
||||
id: u16,
|
||||
) -> Result<(), Error> {
|
||||
if let Some(msg) = find_quest_message(ctx, id).await {
|
||||
if let Some(msg) = find_quest_message(ctx, id).await? {
|
||||
msg.delete(ctx).await?;
|
||||
}
|
||||
|
||||
let mut path = ctx.data().config.full_quests_path();
|
||||
path.push(format!("{id}.toml"));
|
||||
if let Err(error) = Quest::delete(path) {
|
||||
eprintln!("{error}");
|
||||
let reply_string = ERROR_MSG.to_string();
|
||||
ctx.reply(reply_string).await?;
|
||||
return Ok(());
|
||||
}
|
||||
Quest::delete(path)?;
|
||||
|
||||
let mut accounts = ctx.data().config.load_accounts();
|
||||
let accounts_path = ctx.data().config.full_accounts_path();
|
||||
|
|
@ -368,9 +335,7 @@ pub async fn delete(
|
|||
for account in accounts.iter_mut().filter(|a| a.quests_completed.contains(&id)) {
|
||||
let index = account.quests_completed.iter().position(|qid| *qid == id).expect("We just filtered it");
|
||||
account.quests_completed.remove(index);
|
||||
if let Err(error) = account.save(accounts_path.clone()) {
|
||||
eprintln!("{error}");
|
||||
}
|
||||
account.save(accounts_path.clone())?;
|
||||
}
|
||||
|
||||
let reply_string = format!("Successfully deleted quest #{id}");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue