Implemented roll request, updated API requests
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
use std::env;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use serenity::all::{Interaction, ResumedEvent};
|
||||
use serenity::all::{CreateEmbed, CreateInteractionResponse, CreateInteractionResponseMessage, EditInteractionResponse, Interaction, ResumedEvent, UserId};
|
||||
use serenity::async_trait;
|
||||
use serenity::model::gateway::Ready;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::prelude::*;
|
||||
use songbird::Songbird;
|
||||
use crate::bot::commands::chat::generate_response;
|
||||
use crate::bot::commands::fun::roll::{format_roll, roll_dice, send_roll_message};
|
||||
use crate::bot::oai::OAI;
|
||||
use crate::data::guilds::GuildCache;
|
||||
use crate::HttpKey;
|
||||
use crate::utils::{a_or_an, number_to_words};
|
||||
use super::{commands};
|
||||
use super::chat::{create_modal_response};
|
||||
use super::chat::{create_modal_response, user_dm};
|
||||
|
||||
pub struct BotHandler {
|
||||
// Open AI Config
|
||||
@@ -82,18 +84,22 @@ impl EventHandler for BotHandler {
|
||||
log::warn!("No ready guilds found");
|
||||
}
|
||||
|
||||
let songbird = songbird::get(&ctx).await.unwrap();
|
||||
SONGBIRD
|
||||
.set(songbird.clone())
|
||||
.expect("Songbird value could not be set");
|
||||
let http_client = {
|
||||
let data = ctx.data.read().await;
|
||||
data
|
||||
.get::<HttpKey>()
|
||||
.cloned()
|
||||
.expect("Guaranteed to exist in the typemap.")
|
||||
};
|
||||
CLIENT.set(http_client).ok();
|
||||
if SONGBIRD.get().is_none() {
|
||||
let songbird = songbird::get(&ctx).await.unwrap();
|
||||
SONGBIRD
|
||||
.set(songbird.clone())
|
||||
.expect("Songbird value could not be set");
|
||||
}
|
||||
if CLIENT.get().is_none() {
|
||||
let http_client = {
|
||||
let data = ctx.data.read().await;
|
||||
data
|
||||
.get::<HttpKey>()
|
||||
.cloned()
|
||||
.expect("Guaranteed to exist in the typemap.")
|
||||
};
|
||||
CLIENT.set(http_client).ok();
|
||||
}
|
||||
|
||||
log::trace!("Handling {} guilds", ready.guilds.len());
|
||||
for guild in ready.guilds {
|
||||
@@ -122,6 +128,7 @@ impl EventHandler for BotHandler {
|
||||
commands::audio::volume::register(),
|
||||
commands::event::schedule::register(),
|
||||
commands::fun::roll::register(),
|
||||
commands::fun::request_roll::register(),
|
||||
commands::utility::ping::register(),
|
||||
],
|
||||
)
|
||||
@@ -146,10 +153,8 @@ impl EventHandler for BotHandler {
|
||||
}
|
||||
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
if let Interaction::Ping(ping) = interaction {
|
||||
log::trace!("Received interaction ping: {:?}", ping);
|
||||
} else if let Interaction::Command(command) = interaction {
|
||||
log::trace!("Received command interaction: {command:#?}");
|
||||
if let Interaction::Command(command) = interaction {
|
||||
log::trace!("Received COMMAND");
|
||||
match command.data.name.as_str() {
|
||||
// Match commands without returns
|
||||
"play" => commands::audio::play::run(&ctx, &command).await,
|
||||
@@ -161,11 +166,47 @@ impl EventHandler for BotHandler {
|
||||
"volume" => commands::audio::volume::run(&ctx, &command).await,
|
||||
"schedule" => commands::event::schedule::run(&ctx, &command).await,
|
||||
"roll" => commands::fun::roll::run(&ctx, &command).await,
|
||||
"requestroll" => commands::fun::request_roll::run(&ctx, &command).await,
|
||||
"ping" => commands::utility::ping::run(&ctx, &command).await,
|
||||
_ => {}
|
||||
}
|
||||
} else if let Interaction::Component(component) = interaction {
|
||||
log::trace!("Received COMPONENT");
|
||||
let custom_id = &component.data.custom_id;
|
||||
if custom_id.starts_with("request_dice_roll") {
|
||||
// Acknowledge the interaction
|
||||
if let Err(err) = component.create_response(ctx.http.clone(), CreateInteractionResponse::Acknowledge).await {
|
||||
log::error!("Could not create dice response: {}", err);
|
||||
};
|
||||
let parts = custom_id.split('|').collect::<Vec<&str>>();
|
||||
if parts.len() == 6 {
|
||||
let count = parts[1].parse().unwrap();
|
||||
let sides = parts[2].parse().unwrap();
|
||||
let modifier = parts[3].parse().unwrap();
|
||||
let result = roll_dice(count, sides, modifier);
|
||||
let response = format!("(Rolled {})", format_roll(count, sides, modifier));
|
||||
let user_id = UserId::from(parts[4].parse::<u64>().unwrap());
|
||||
let roller_id = component.user.id;
|
||||
let hidden: bool = parts[5].parse().unwrap();
|
||||
send_roll_message(&ctx, result, user_id, roller_id, &response).await;
|
||||
component.delete_response(ctx.http.clone()).await.ok();
|
||||
let message;
|
||||
if hidden {
|
||||
message = format!("Results sent to {}", user_id.mention());
|
||||
} else {
|
||||
message = format!("🎲 You rolled {} {}\n-# {}", a_or_an(&number_to_words(result)), result, response);
|
||||
}
|
||||
user_dm(&ctx, &component.user.id, message).await;
|
||||
} else {
|
||||
log::error!("Could not handle dice click: {}", custom_id);
|
||||
}
|
||||
}
|
||||
} else if let Interaction::Ping(_ping) = interaction {
|
||||
log::trace!("Received PING");
|
||||
} else if let Interaction::Autocomplete(_autocomplete) = interaction {
|
||||
log::trace!("Received AUTOCOMPLETE");
|
||||
} else if let Interaction::Modal(modal) = interaction {
|
||||
log::trace!("Received interaction modal: {:?}", modal);
|
||||
log::trace!("Received MODAL");
|
||||
create_modal_response(&ctx, &modal).await;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user