refactor + fix poise error

This commit is contained in:
djkato 2024-12-08 19:42:37 +01:00
parent 6bc87dc64b
commit 68579d6de9
3 changed files with 126 additions and 142 deletions

3
rust-toolchain.toml Normal file
View file

@ -0,0 +1,3 @@
[toolchain]
channel = "nightly-2024-11-13"
targets = ["x86_64-unknown-linux-gnu"]

View file

@ -4,32 +4,26 @@ use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler};
use songbird::TrackEvent; use songbird::TrackEvent;
use crate::util::debug::send_error; use crate::util::debug::send_error;
use crate::{types::{Context, Error}, util::utilities::get_channel_by_name}; use crate::{
types::{Context, Error},
util::utilities::get_channel_by_name,
};
use serenity::all::User; use serenity::all::User;
#[poise::command( #[poise::command(slash_command, description_localized("en-US", "Play song"))]
slash_command, pub async fn play(ctx: Context<'_>, #[description = "Song: "] _url: String) -> Result<(), Error> {
description_localized("en-US", "Play song")
)]
pub async fn play(ctx: Context<'_>,
#[description = "Song: "]
_url: String
) -> Result<(), Error> {
ctx.reply("Done!").await?; ctx.reply("Done!").await?;
Ok(()) Ok(())
} }
#[poise::command( #[poise::command(
slash_command, slash_command,
description_localized("en-US", "Headpat all your friends!") description_localized("en-US", "Headpat all your friends!")
)] )]
pub async fn headpat(ctx: Context<'_>, pub async fn headpat(
#[description = "Who is the lucky one?"] ctx: Context<'_>,
user: User #[description = "Who is the lucky one?"] user: User,
) -> Result<(), Error> { ) -> Result<(), Error> {
let _title = "HEADPATS!"; let _title = "HEADPATS!";
let _desc = format!("{} headpats {}", ctx.author(), user); let _desc = format!("{} headpats {}", ctx.author(), user);
@ -38,85 +32,70 @@ pub async fn headpat(ctx: Context<'_>,
Ok(()) Ok(())
} }
enum ConnectStatus<'a> {
Ok,
ErrorReplyMessage(&'a str),
}
#[poise::command( #[poise::command(slash_command, description_localized("en-US", "Connect to channel"))]
slash_command, pub async fn connect(
description_localized("en-US", "Connect to channel") ctx: Context<'_>,
)]
pub async fn connect(ctx: Context<'_>,
#[autocomplete = "autocomplete_channel"] #[autocomplete = "autocomplete_channel"]
#[description = "Voice channel name: "] #[description = "Voice channel name: "]
name: Option<String> name: Option<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
if ctx.guild().is_none() { if ctx.guild().is_none() {
ctx.reply("Can't use this outside of guild").await?; ctx.reply("Can't use this outside of guild").await?;
return Ok(())
}
let channel: ChannelId;
if name.is_none() || name.clone().unwrap() == "" {
// ERROR: <>
// !### This will work if you comment out one of the replies ###!
// let Some(guild) = ctx.guild() else {
// ctx.reply("You must be in a voice channel or specify explicit voice channel by name").await?;
// return Ok(())
// };
// let Some(voice_channel) = guild.voice_states.get(&ctx.author().id).and_then(|voice_state| voice_state.channel_id) else {
// ctx.reply("You must be in a voice channel or specify explicit voice channel by name").await?;
// return Ok(())
// };
// channel = voice_channel;
// !### ###!
// This one liner works only if you comment out the reply
// channel = match ctx.guild().unwrap().voice_states.get(&ctx.author().id).and_then(|voice_state| voice_state.channel_id) {
// Some(channel) => channel,
// None => {
// // ERROR: <>
// // Will work if you comment this out
// ctx.reply("You must be in a voice channel or specify explicit voice channel by name").await?;
// return Ok(())
// }
// }
// This one works
// Ugly one liner since I don't know how to do this another way yet
// TODO fix please
let Some(voice_channel) = ctx.guild().unwrap().voice_states.get(&ctx.author().id).and_then(|voice_state| voice_state.channel_id) else {
ctx.reply("You must be in a voice channel or specify explicit voice channel by name").await?;
return Ok(())
};
channel = voice_channel;
}
else {
channel = match get_channel_by_name(ctx.guild().unwrap(), name.unwrap()) {
Some(channel) => channel,
None => {
ctx.reply("Channel with this name does not exist").await?;
return Ok(()); return Ok(());
} }
let mut maybe_reply = ConnectStatus::Ok;
let channel_id: Option<ChannelId> =
match name.is_none() || name.as_ref().is_some_and(|n| n != "") {
true => {
//Radko: Ak nechceš vyvíjať na nightly (alebo ešte nevyšiel rust 2024 edition) tak
//chainuj .and_then() / .map()
if let Some(voice_channel) = ctx.guild()
&& let Some(voice_state) = voice_channel.voice_states.get(&ctx.author().id)
&& let Some(channel_id) = voice_state.channel_id
{
Some(channel_id)
} else {
maybe_reply = ConnectStatus::ErrorReplyMessage(
"You must be in a voice channel or specify explicit voice channel by name",
);
None
}
}
false => match get_channel_by_name(ctx.guild().unwrap(), name.unwrap()) {
Some(channel) => Some(channel),
None => {
maybe_reply =
ConnectStatus::ErrorReplyMessage("Channel with this name does not exist");
None
}
},
}; };
if let ConnectStatus::ErrorReplyMessage(m) = maybe_reply {
ctx.reply(m).await?;
} }
if let Some(channel_id) = channel_id {
let manager = songbird::get(ctx.serenity_context()) let manager = songbird::get(ctx.serenity_context())
.await .await
.expect("Songbird Voice client placed in at initialisation.") .expect("Songbird Voice client placed in at initialisation.")
.clone(); .clone();
if let Ok(handler_lock) = manager.join(ctx.guild_id().unwrap(), channel).await { if let Ok(handler_lock) = manager.join(ctx.guild_id().unwrap(), channel_id).await {
let mut handler = handler_lock.lock().await; let mut handler = handler_lock.lock().await;
handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier); handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier);
} }
}
ctx.reply("Done!").await?; ctx.reply("Done!").await?;
Ok(()) Ok(())
} }
struct TrackErrorNotifier; struct TrackErrorNotifier;
#[async_trait] #[async_trait]
@ -136,15 +115,11 @@ impl VoiceEventHandler for TrackErrorNotifier {
} }
} }
async fn autocomplete_channel( async fn autocomplete_channel(ctx: Context<'_>, _partial: &str) -> Vec<String> {
ctx: Context<'_>,
_partial: &str,
) -> Vec<String> {
use crate::util::utilities::get_vc_names; use crate::util::utilities::get_vc_names;
match ctx.guild() { match ctx.guild() {
Some(guild) => get_vc_names(guild), Some(guild) => get_vc_names(guild),
None => [].to_vec() None => [].to_vec(),
} }
} }
@ -152,10 +127,7 @@ async fn autocomplete_channel(
slash_command, slash_command,
description_localized("en-US", "Disconnect from voice channel") description_localized("en-US", "Disconnect from voice channel")
)] )]
async fn disconnect( async fn disconnect(ctx: Context<'_>) -> Result<(), Error> {
ctx: Context<'_>
) -> Result<(), Error> {
let Some(guild_id) = ctx.guild_id() else { let Some(guild_id) = ctx.guild_id() else {
ctx.reply("Can't use this outside of guild").await?; ctx.reply("Can't use this outside of guild").await?;
return Ok(()); return Ok(());
@ -168,19 +140,19 @@ async fn disconnect(
let has_handler = manager.get(guild_id).is_some(); let has_handler = manager.get(guild_id).is_some();
if ! has_handler { if !has_handler {
ctx.reply("I am not connected to a channel!").await?; ctx.reply("I am not connected to a channel!").await?;
return Ok(()) return Ok(());
} }
match manager.remove(guild_id).await { match manager.remove(guild_id).await {
Ok(()) => { Ok(()) => {
ctx.reply("Disconnected").await?; ctx.reply("Disconnected").await?;
return Ok(()) return Ok(());
} }
Err(e) => { Err(e) => {
let _ = send_error(ctx.serenity_context().http.clone(), e.to_string()).await; let _ = send_error(ctx.serenity_context().http.clone(), e.to_string()).await;
return Ok(()) return Ok(());
} }
} }
} }

View file

@ -1,21 +1,22 @@
#![feature(let_chains)]
use std::env;
use std::error;
use std::future::Future; use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use std::error;
use std::env;
use serenity::all::{EventHandler, Message};
use serenity::async_trait; use serenity::async_trait;
use serenity::prelude::GatewayIntents;
use serenity::client::Context; use serenity::client::Context;
use serenity::model::gateway::Ready; use serenity::model::gateway::Ready;
use serenity::all::{EventHandler, Message}; use serenity::prelude::GatewayIntents;
use serenity::Client; use serenity::Client;
use dotenv::dotenv; use dotenv::dotenv;
use songbird::SerenityInit; use songbird::SerenityInit;
use tokio_cron_scheduler::{JobScheduler, Job}; use tokio_cron_scheduler::{Job, JobScheduler};
mod message_handler; mod message_handler;
use message_handler::handle; use message_handler::handle;
@ -54,7 +55,8 @@ impl EventHandler for Handler {
async fn ready(&self, ctx: Context, ready: Ready) { async fn ready(&self, ctx: Context, ready: Ready) {
println!("{} v0.3 is connected!", ready.user.name); println!("{} v0.3 is connected!", ready.user.name);
#[cfg(feature="RELEASE")] { #[cfg(feature = "RELEASE")]
{
use util::debug::hello; use util::debug::hello;
hello(ctx.http.clone()).await; hello(ctx.http.clone()).await;
} }
@ -63,12 +65,17 @@ impl EventHandler for Handler {
let job_closure = move |_, _| -> Pin<Box<dyn Future<Output = ()> + Send>> { let job_closure = move |_, _| -> Pin<Box<dyn Future<Output = ()> + Send>> {
let ctx_clone = ctx.clone(); let ctx_clone = ctx.clone();
Box::pin( async move { Box::pin(async move {
notice::notice_wrapper(ctx_clone).await; notice::notice_wrapper(ctx_clone).await;
}) })
}; };
sched.add(Job::new_async("0 0 13 * * *", job_closure).expect("Cron job not set up correctly")).await.unwrap(); sched
.add(
Job::new_async("0 0 13 * * *", job_closure).expect("Cron job not set up correctly"),
)
.await
.unwrap();
sched.start().await.unwrap(); sched.start().await.unwrap();
} }
} }
@ -80,7 +87,8 @@ async fn main() -> anyhow::Result<()> {
dotenv().ok(); dotenv().ok();
// create poise framework for registering commands // create poise framework for registering commands
let options: poise::FrameworkOptions<(), Box<dyn error::Error + Send + Sync>> = poise::FrameworkOptions { let options: poise::FrameworkOptions<(), Box<dyn error::Error + Send + Sync>> =
poise::FrameworkOptions {
commands: vec![ commands: vec![
commands::say(), commands::say(),
commands::hug(), commands::hug(),
@ -95,31 +103,32 @@ async fn main() -> anyhow::Result<()> {
..Default::default() ..Default::default()
}, },
on_error: |err| Box::pin(on_error(err)), on_error: |err| Box::pin(on_error(err)),
command_check: Some(|ctx| { command_check: Some(|ctx| Box::pin(async move { return Ok(!ctx.author().bot) })),
Box::pin(async move {
return Ok(!ctx.author().bot)
})
}),
..Default::default() ..Default::default()
}; };
let framework = poise::Framework::builder() let framework = poise::Framework::builder()
.setup(move |ctx, _ready, framework| { .setup(move |ctx, _ready, framework| {
Box::pin(async move { Box::pin(async move {
#[cfg(feature = "GUILD_COMMAND")]
{
#[cfg(feature="GUILD_COMMAND")] {
use poise::samples::register_in_guild; use poise::samples::register_in_guild;
use serenity::all::GuildId; use serenity::all::GuildId;
let debug_guild_id = env::var("DEBUG_GUILD_ID") let debug_guild_id = env::var("DEBUG_GUILD_ID")
.context("DEBUG_GUILD_ID not found in env")? .context("DEBUG_GUILD_ID not found in env")?
.parse::<u64>().unwrap(); .parse::<u64>()
.unwrap();
register_in_guild(ctx, &framework.options().commands, GuildId::new(debug_guild_id)).await?; register_in_guild(
ctx,
&framework.options().commands,
GuildId::new(debug_guild_id),
)
.await?;
} }
#[cfg(not(feature="GUILD_COMMAND"))] { #[cfg(not(feature = "GUILD_COMMAND"))]
{
use poise::samples::register_globally; use poise::samples::register_globally;
register_globally(ctx, &framework.options().commands).await?; register_globally(ctx, &framework.options().commands).await?;
} }
@ -131,7 +140,7 @@ async fn main() -> anyhow::Result<()> {
let token_str = "TOKEN"; let token_str = "TOKEN";
#[cfg(feature="DEBUG")] #[cfg(feature = "DEBUG")]
let token_str = "DEBUGTOKEN"; let token_str = "DEBUGTOKEN";
let token = env::var(token_str).context("TOKEN not found in env")?; let token = env::var(token_str).context("TOKEN not found in env")?;