Merge pull request #5

Switch to global command registration
This commit is contained in:
Em (Ethan) Ruszanowski 2023-02-07 17:51:48 -05:00 committed by GitHub
commit e943135d2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 36 deletions

View file

@ -1,10 +1,10 @@
use chrono;
use md5::Digest;
use rand::seq::SliceRandom;
use reqwest::{Error, Response};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct Session {
ret_msg: String,
session_id: String,
@ -12,6 +12,7 @@ struct Session {
}
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
pub struct God {
#[serde(rename = "Name")]
name: String,
@ -20,7 +21,7 @@ pub struct God {
async fn get_utc_timestamp() -> Result<String, Error> {
let timestamp: String = chrono::Utc::now().format("%Y%m%d%H%M%S").to_string();
return Ok(timestamp);
Ok(timestamp)
}
async fn get_signature(
@ -31,16 +32,15 @@ async fn get_signature(
) -> Result<String, Error> {
let hash: Digest = md5::compute(format!("{}{}{}{}", dev_id, method, auth_key, timestamp));
let signature: String = format!("{:x}", hash);
return Ok(signature);
Ok(signature)
}
async fn create_session() -> Result<Session, Error> {
let dev_id: String = std::env::var("DEV_ID").expect("Missing DEV_ID");
let auth_key: String = std::env::var("AUTH_KEY").expect("Missing AUTH_KEY");
let method: &str = "createsession";
let timestamp: String = get_utc_timestamp().await?;
let signature: String = get_signature(&dev_id, &method, &auth_key, &timestamp).await?;
let signature: String = get_signature(&dev_id, "createsession", &auth_key, &timestamp).await?;
let request: String = format!(
"https://api.smitegame.com/smiteapi.svc/createsessionJson/{dev_id}/{signature}/{timestamp}",
@ -51,18 +51,17 @@ async fn create_session() -> Result<Session, Error> {
let response: Response = reqwest::get(&request).await?;
let session: Session = response.json().await?;
return Ok(session);
Ok(session)
}
async fn get_gods() -> Result<Vec<God>, Error> {
let dev_id: String = std::env::var("DEV_ID").expect("Missing DEV_ID");
let auth_key: String = std::env::var("AUTH_KEY").expect("Missing AUTH_KEY");
let method: &str = "getgods";
let session_id: String = create_session().await?.session_id;
let timestamp: String = get_utc_timestamp().await?;
let signature: String = get_signature(&dev_id, &method, &auth_key, &timestamp).await?;
let signature: String = get_signature(&dev_id, "getgods", &auth_key, &timestamp).await?;
let request: String = format!(
"https://api.smitegame.com/smiteapi.svc/getgodsJson/{id}/{signature}/{session}/{timestamp}/1",
@ -74,7 +73,7 @@ async fn get_gods() -> Result<Vec<God>, Error> {
let response: Response = reqwest::get(&request).await?;
let gods: Vec<God> = response.json().await?;
return Ok(gods);
Ok(gods)
}
pub async fn get_random_god() -> Result<String, Error> {
@ -83,5 +82,5 @@ pub async fn get_random_god() -> Result<String, Error> {
.choose(&mut rand::thread_rng())
.expect("Couldn't pick random god.");
let name: String = god.name.clone();
return Ok(name);
Ok(name)
}

View file

@ -1,5 +1,6 @@
// Group all commands for registration
mod api;
pub mod random;
pub mod register;
pub mod slur;
pub mod team;

View file

@ -11,18 +11,6 @@ pub async fn random(_ctx: Context<'_>) -> Result<(), Error> {
/// Picks a random god
#[poise::command(slash_command)]
pub async fn god(ctx: Context<'_>) -> Result<(), Error> {
if ctx.author().id == serenity::UserId(310437480216395777) {
ctx.send(|f| {
f.embed(|f| {
f.title("Random God")
.description(format!("You main **Aphrodite**, idiot."))
.color(serenity::Colour::from_rgb(227, 28, 121))
})
})
.await?;
return Ok(());
}
let god: String = get_random_god().await?;
ctx.send(|f| {
f.embed(|f| {

7
src/commands/register.rs Normal file
View file

@ -0,0 +1,7 @@
use crate::{Context, Error};
#[poise::command(prefix_command, owners_only)]
pub async fn register(ctx: Context<'_>) -> Result<(), Error> {
poise::builtins::register_application_commands_buttons(ctx).await?;
Ok(())
}

View file

@ -15,6 +15,7 @@ async fn main() {
commands::slur::slur(),
commands::team::team(),
commands::random::random(),
commands::register::register(),
], // IntelliJ doesn't like this, but it's fine.
..Default::default()
})
@ -22,23 +23,10 @@ async fn main() {
.intents(serenity::GatewayIntents::non_privileged()) // Set intents for Discord dev portal
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_in_guild(
ctx,
&framework.options().commands,
serenity::GuildId(
std::env::var("GUILD_ID")
.expect("Missing GUILD_ID") // Get GID from env and parse
.parse::<u64>()
.unwrap(),
),
)
.await?; // Update slash commands in GID
poise::builtins::register_globally(ctx, &framework.options().commands).await?; // Update slash commands
ctx.set_activity(serenity::Activity::playing("SMITE")).await;
Ok(Data {})
})
});
framework.run().await.unwrap();
}