Update invites command to cover new API
This commit is contained in:
parent
ff4b227a64
commit
5948d4ada9
|
@ -1,32 +1,27 @@
|
|||
use std::fmt::format;
|
||||
|
||||
use crate::{Context, Error};
|
||||
use serde::Deserialize;
|
||||
|
||||
// pub mod invites;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct InviteUser {
|
||||
// #[serde(rename = "_id")]
|
||||
// id: String,
|
||||
#[serde(rename = "showUsername")]
|
||||
name: String,
|
||||
struct Message {
|
||||
message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct InviteInfo {
|
||||
// #[serde(rename = "_id")]
|
||||
// id: String,
|
||||
#[serde(rename = "fromUser")]
|
||||
from_user: InviteUser,
|
||||
#[serde(rename = "usedBy")]
|
||||
used_by: Option<InviteUser>,
|
||||
struct InviteData {
|
||||
id: String,
|
||||
token: String,
|
||||
high_tier: bool,
|
||||
custom: bool,
|
||||
#[serde(rename = "isUsed")]
|
||||
used: bool,
|
||||
isUsed: bool,
|
||||
isHighTier: bool,
|
||||
isCustom: bool,
|
||||
}
|
||||
|
||||
async fn get_invite(invite: &str) -> Result<Option<InviteInfo>, Error> {
|
||||
#[poise::command(slash_command, ephemeral)]
|
||||
pub async fn invites (
|
||||
ctx: Context<'_>,
|
||||
#[description = "The invite to check"] invite: String,
|
||||
) -> Result<(), Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let req = client
|
||||
.get(format!(
|
||||
|
@ -35,119 +30,30 @@ async fn get_invite(invite: &str) -> Result<Option<InviteInfo>, Error> {
|
|||
))
|
||||
.send()
|
||||
.await?;
|
||||
if req.status().as_u16() == 404 {
|
||||
// debug!("Error 404 on getting the invites, this just means invite not found");
|
||||
Ok(None)
|
||||
} else if req.status().as_u16() == 200 {
|
||||
let data: InviteInfo = req.json().await?;
|
||||
// debug!("invite data: {:#?}", data);
|
||||
Ok(Some(data))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, poise::SlashChoiceParameter)]
|
||||
pub enum InviteActions {
|
||||
#[name = "Does this invite exist?"]
|
||||
Exist,
|
||||
#[name = "Is this invite valid?"]
|
||||
Valid,
|
||||
// If no name is given, the variant name is used
|
||||
#[name = "Get invite information"]
|
||||
Info,
|
||||
#[name = "Which user used the invite?"]
|
||||
User,
|
||||
}
|
||||
|
||||
/// Information about invites
|
||||
#[poise::command(slash_command, ephemeral)]
|
||||
pub async fn invites(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Action to perform with invite"] action: InviteActions,
|
||||
#[description = "The invite to check"] invite: String,
|
||||
) -> Result<(), Error> {
|
||||
let invite_res = get_invite(&invite).await;
|
||||
match invite_res {
|
||||
Err(e) => {
|
||||
ctx.say(format!("Error getting invite: {}", e)).await?;
|
||||
let result = match req.status().as_u16() {
|
||||
404 | 410 => {
|
||||
let data: Message = req.json().await?;
|
||||
format!(
|
||||
"Invite `{}` is unavailable because: {}",
|
||||
invite, data.message
|
||||
)
|
||||
}
|
||||
Ok(invite_opt) => {
|
||||
ctx.send(|b| {
|
||||
match action {
|
||||
InviteActions::Exist => {
|
||||
b.content(match invite_opt {
|
||||
None => "Invite not found",
|
||||
Some(_) => "Invite found",
|
||||
});
|
||||
}
|
||||
InviteActions::Valid => {
|
||||
b.content(match invite_opt {
|
||||
None => "Invite not found",
|
||||
Some(i) => {
|
||||
if !i.used {
|
||||
"✅ Invite is valid and not used yet"
|
||||
} else {
|
||||
"❌ Invite is valid but is already used"
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
InviteActions::Info => {
|
||||
b.content(match invite_opt {
|
||||
None => "Invite not found".to_owned(),
|
||||
Some(i) => {
|
||||
if !i.used {
|
||||
"Ooh this invite is still up for grabs, go quickly claim it!"
|
||||
.to_owned()
|
||||
} else {
|
||||
match i.used_by {
|
||||
None => {
|
||||
"Invite is already used but can not find the user..."
|
||||
.to_owned()
|
||||
}
|
||||
Some(user) => format!(
|
||||
"This invite has already been used by {}",
|
||||
user.name
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
InviteActions::User => {
|
||||
match invite_opt {
|
||||
None => b.content("Invite not found"),
|
||||
Some(i) => {
|
||||
b.embed(|e| {
|
||||
e.title("Invite information");
|
||||
e.description(format!("Invite: {}", i.token));
|
||||
if i.used {
|
||||
e.field("Used", "Yes", true);
|
||||
if let Some(user) = i.used_by {
|
||||
e.field("Used by", user.name, true);
|
||||
}
|
||||
} else {
|
||||
e.field("Used", "No", true);
|
||||
}
|
||||
e.field(
|
||||
"High tier",
|
||||
if i.high_tier { "Yes" } else { "No" },
|
||||
true,
|
||||
);
|
||||
e.field("Custom", if i.custom { "Yes" } else { "No" }, true);
|
||||
e.field("Given out by", i.from_user.name, true);
|
||||
e
|
||||
});
|
||||
b.content("Embed:")
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
b
|
||||
})
|
||||
.await?;
|
||||
200 => {
|
||||
let data: InviteData = req.json().await?;
|
||||
if data.isHighTier {
|
||||
format!(
|
||||
"Invite `{}` is still available and grants High Tier status",
|
||||
invite
|
||||
)
|
||||
} else {
|
||||
format!("Invite `{}` is still available", invite)
|
||||
}
|
||||
}
|
||||
_ => format!(
|
||||
"Something unexpected happened with the API, can't check `{}` now",
|
||||
invite,
|
||||
),
|
||||
};
|
||||
ctx.say(result).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue