feat: Initialized Discord bot
- Bump version to 0.6.0 - discord: Added /quest list - discord: Added /quest create (admin)
This commit is contained in:
parent
2960b6dfc4
commit
5fa2ac330f
8 changed files with 2334 additions and 12 deletions
148
discord/src/commands/quest.rs
Normal file
148
discord/src/commands/quest.rs
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use squad_quest::{SquadObject, quest::{Quest, QuestDifficulty}};
|
||||
use toml::value::Date;
|
||||
use crate::{Context, Error};
|
||||
|
||||
const ERROR_MSG: &str = "Server error :(";
|
||||
|
||||
#[poise::command(
|
||||
prefix_command,
|
||||
slash_command,
|
||||
subcommands("list", "create"),
|
||||
)]
|
||||
pub async fn quest(
|
||||
_ctx: Context<'_>,
|
||||
) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[poise::command(
|
||||
prefix_command,
|
||||
slash_command,
|
||||
)]
|
||||
pub async fn list(
|
||||
ctx: Context<'_>,
|
||||
) -> Result<(), Error> {
|
||||
let conf = &ctx.data().config;
|
||||
let quests = conf.load_quests();
|
||||
let mut reply_string = format!("Listing {} quests:", quests.len());
|
||||
for quest in quests {
|
||||
reply_string.push_str(format!("\n#{}: {}\n\tDescription: {}",
|
||||
quest.id,
|
||||
quest.name,
|
||||
quest.description,
|
||||
).as_str());
|
||||
}
|
||||
ctx.reply(reply_string).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, poise::ChoiceParameter)]
|
||||
pub enum DifficultyWrapper {
|
||||
Easy,
|
||||
Normal,
|
||||
Hard,
|
||||
Secret,
|
||||
}
|
||||
|
||||
|
||||
impl From<DifficultyWrapper> for QuestDifficulty {
|
||||
fn from(value: DifficultyWrapper) -> Self {
|
||||
match &value {
|
||||
DifficultyWrapper::Easy => Self::Easy,
|
||||
DifficultyWrapper::Normal => Self::Normal,
|
||||
DifficultyWrapper::Hard => Self::Hard,
|
||||
DifficultyWrapper::Secret => Self::Secret,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct DateWrapper {
|
||||
date: Date,
|
||||
}
|
||||
|
||||
impl FromStr for DateWrapper {
|
||||
type Err = toml::de::Error;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let toml_str = format!("date = {s}");
|
||||
let wrapper: Self = toml::from_str(&toml_str)?;
|
||||
Ok(wrapper)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DateWrapper> for Date {
|
||||
fn from(value: DateWrapper) -> Self {
|
||||
value.date
|
||||
}
|
||||
}
|
||||
|
||||
#[poise::command(
|
||||
prefix_command,
|
||||
slash_command,
|
||||
required_permissions = "ADMINISTRATOR",
|
||||
guild_only,
|
||||
)]
|
||||
pub async fn create(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Quest difficulty"]
|
||||
difficulty: DifficultyWrapper,
|
||||
#[description = "Reward for the quest"]
|
||||
reward: u32,
|
||||
#[description = "Quest name"]
|
||||
name: String,
|
||||
#[description = "Quest description"]
|
||||
description: String,
|
||||
#[description = "Quest answer, visible to admins"]
|
||||
answer: String,
|
||||
#[description = "Optional date of publication (in format of YYYY-MM-DD, e.g. 2025-12-24)"]
|
||||
available: Option<DateWrapper>,
|
||||
#[description = "Optional deadline (in format of YYYY-MM-DD, e.g. 2025-12-24)"]
|
||||
deadline: Option<DateWrapper>,
|
||||
) -> Result<(), Error> {
|
||||
let conf = &ctx.data().config;
|
||||
let mut quests = conf.load_quests();
|
||||
quests.sort_by(|a,b| a.id.cmp(&b.id));
|
||||
let next_id = match quests.last() {
|
||||
Some(quest) => quest.id + 1u16,
|
||||
None => 0u16
|
||||
};
|
||||
|
||||
let available_on = match available {
|
||||
Some(avail) => Some(avail.into()),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let deadline = match deadline {
|
||||
Some(dl) => Some(dl.into()),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let quest = Quest {
|
||||
id: next_id,
|
||||
difficulty: difficulty.into(),
|
||||
reward,
|
||||
name,
|
||||
description,
|
||||
answer,
|
||||
public: false,
|
||||
available_on,
|
||||
deadline,
|
||||
};
|
||||
|
||||
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}")
|
||||
},
|
||||
};
|
||||
|
||||
ctx.reply(reply_string).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue