feat: Implementation config
- Bump version to 0.7.0 - Added Config::init_path - Added Error::IsNotImplemented - discord: added implementation config init/load - discord: added /init - discord: added /quest update
This commit is contained in:
parent
b92eaa1241
commit
520992187d
10 changed files with 147 additions and 12 deletions
38
discord/src/commands/init.rs
Normal file
38
discord/src/commands/init.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use std::path::Path;
|
||||
|
||||
use poise::serenity_prelude::{ChannelId};
|
||||
use squad_quest::SquadObject;
|
||||
|
||||
use crate::{commands::ERROR_MSG, Context, Error};
|
||||
|
||||
|
||||
#[poise::command(
|
||||
prefix_command,
|
||||
slash_command,
|
||||
required_permissions = "ADMINISTRATOR",
|
||||
guild_only,
|
||||
)]
|
||||
pub async fn init(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Channel to post quests to"]
|
||||
quests_channel: ChannelId,
|
||||
#[description = "Channel to post answers to check"]
|
||||
answers_channel: ChannelId,
|
||||
) -> Result<(), Error> {
|
||||
let mut dc = ctx.data().discord.clone();
|
||||
let guild = ctx.guild_id().unwrap();
|
||||
dc.quests_channel = quests_channel;
|
||||
dc.answers_channel = answers_channel;
|
||||
dc.guild = guild;
|
||||
let path = &ctx.data().config.full_impl_path().unwrap();
|
||||
let reply_string = match dc.save(path.parent().unwrap_or(Path::new("")).to_owned()) {
|
||||
Ok(_) => "Please restart bot to apply changes".to_string(),
|
||||
Err(error) => {
|
||||
eprintln!("{error}");
|
||||
ERROR_MSG.to_string()
|
||||
},
|
||||
};
|
||||
ctx.reply(reply_string).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -3,7 +3,9 @@
|
|||
use crate::{Context, Error};
|
||||
|
||||
pub mod quest;
|
||||
pub mod init;
|
||||
|
||||
pub const ERROR_MSG: &str = "Server error :(";
|
||||
|
||||
#[poise::command(prefix_command)]
|
||||
pub async fn register(ctx: Context<'_>) -> Result<(), Error> {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ const ERROR_MSG: &str = "Server error :(";
|
|||
#[poise::command(
|
||||
prefix_command,
|
||||
slash_command,
|
||||
subcommands("list", "create"),
|
||||
subcommands("list", "create", "update"),
|
||||
)]
|
||||
pub async fn quest(
|
||||
_ctx: Context<'_>,
|
||||
|
|
@ -146,3 +146,82 @@ pub async fn create(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[poise::command(
|
||||
prefix_command,
|
||||
slash_command,
|
||||
required_permissions = "ADMINISTRATOR",
|
||||
guild_only,
|
||||
)]
|
||||
pub async fn update(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Quest identifier"]
|
||||
id: u16,
|
||||
#[description = "Quest difficulty"]
|
||||
difficulty: Option<DifficultyWrapper>,
|
||||
#[description = "Reward for the quest"]
|
||||
reward: Option<u32>,
|
||||
#[description = "Quest name"]
|
||||
name: Option<String>,
|
||||
#[description = "Quest description"]
|
||||
description: Option<String>,
|
||||
#[description = "Quest answer, visible to admins"]
|
||||
answer: Option<String>,
|
||||
#[description = "Date of publication (in format of YYYY-MM-DD, e.g. 2025-12-24)"]
|
||||
available: Option<DateWrapper>,
|
||||
#[description = "Quest deadline (in format of YYYY-MM-DD, e.g. 2025-12-24)"]
|
||||
deadline: Option<DateWrapper>,
|
||||
#[description = "Clear availability and deadline if checked"]
|
||||
#[rename = "override"]
|
||||
should_override: Option<bool>,
|
||||
) -> Result<(), Error> {
|
||||
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(());
|
||||
};
|
||||
|
||||
let difficulty = match difficulty {
|
||||
Some(d) => d.into(),
|
||||
None => quest.difficulty
|
||||
};
|
||||
|
||||
let available_on: Option<Date>;
|
||||
let dead_line: Option<Date>;
|
||||
|
||||
match should_override.unwrap_or(false) {
|
||||
true => {
|
||||
available_on = available.map(|v| v.into());
|
||||
dead_line = deadline.map(|v| v.into());
|
||||
},
|
||||
false => {
|
||||
available_on = available.map_or_else(|| quest.available_on.clone(), |v| Some(v.into()));
|
||||
dead_line = deadline.map_or_else(|| quest.deadline.clone(), |v| Some(v.into()));
|
||||
},
|
||||
}
|
||||
|
||||
let new_quest = Quest {
|
||||
id,
|
||||
difficulty,
|
||||
reward: reward.unwrap_or(quest.reward),
|
||||
name: name.unwrap_or(quest.name.clone()),
|
||||
description: description.unwrap_or(quest.description.clone()),
|
||||
answer: answer.unwrap_or(quest.answer.clone()),
|
||||
public: quest.public,
|
||||
available_on,
|
||||
deadline: dead_line,
|
||||
};
|
||||
|
||||
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}"),
|
||||
};
|
||||
ctx.reply(reply_string).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue