moover_rust/src/main.rs

158 lines
4.5 KiB
Rust
Raw Permalink Normal View History

2024-10-06 12:02:24 +00:00
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use std::error;
use std::env;
2023-07-05 09:06:04 +00:00
use serenity::async_trait;
use serenity::prelude::GatewayIntents;
2024-02-14 12:22:29 +00:00
use serenity::client::Context;
use serenity::model::gateway::Ready;
use serenity::all::{EventHandler, Message};
use serenity::Client;
2024-02-14 12:22:29 +00:00
2024-10-06 12:02:24 +00:00
use dotenv::dotenv;
2024-12-09 14:40:46 +00:00
use songbird::SerenityInit;
use tokio_cron_scheduler::{JobScheduler, Job};
2024-02-14 12:22:29 +00:00
2023-07-15 09:18:08 +00:00
mod message_handler;
use message_handler::handle;
mod commands;
2023-07-07 08:55:59 +00:00
mod util;
2024-02-14 12:22:29 +00:00
2023-09-23 19:15:19 +00:00
mod other;
2024-02-14 12:22:29 +00:00
use other::notice;
2023-07-05 09:06:04 +00:00
mod types;
2024-10-06 12:02:24 +00:00
use types::Error;
2023-07-05 09:06:04 +00:00
struct Handler;
async fn on_error(error: poise::FrameworkError<'_, (), Error>) {
match error {
poise::FrameworkError::Setup { error, .. } => panic!("Failed to start bot: {:?}", error),
poise::FrameworkError::Command { error, ctx, .. } => {
println!("Error in command `{}`: {:?}", ctx.command().name, error,);
}
error => {
if let Err(e) = poise::builtins::on_error(error).await {
println!("Error while handling error: {}", e)
}
}
}
}
2023-07-05 09:06:04 +00:00
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
2023-07-15 09:18:08 +00:00
handle(ctx, msg).await;
2023-07-05 09:06:04 +00:00
}
2023-07-07 08:55:59 +00:00
async fn ready(&self, ctx: Context, ready: Ready) {
2025-01-06 13:14:52 +00:00
println!("{} v3.2.0 is connected!", ready.user.name);
2024-02-14 12:22:29 +00:00
#[cfg(feature="RELEASE")] {
use util::debug::hello;
2024-12-17 14:28:52 +00:00
let _ = hello(ctx.http.clone()).await;
}
2024-02-18 11:14:02 +00:00
let sched = JobScheduler::new().await.unwrap();
let job_closure = move |_, _| -> Pin<Box<dyn Future<Output = ()> + Send>> {
let ctx_clone = ctx.clone();
Box::pin( async move {
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.start().await.unwrap();
2023-07-05 09:06:04 +00:00
}
}
2023-07-07 11:44:07 +00:00
2023-07-05 09:06:04 +00:00
#[tokio::main]
2023-07-07 10:16:25 +00:00
async fn main() -> anyhow::Result<()> {
use anyhow::Context;
2024-10-06 12:02:24 +00:00
dotenv().ok();
// create poise framework for registering commands
2024-10-06 12:02:24 +00:00
let options: poise::FrameworkOptions<(), Box<dyn error::Error + Send + Sync>> = poise::FrameworkOptions {
commands: vec![
commands::say(),
2024-12-09 14:40:46 +00:00
commands::hug(),
2024-12-17 14:28:52 +00:00
commands::player::play_local(),
commands::player::disconnect(),
2025-01-06 11:51:57 +00:00
commands::radio::radio()
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("/".into()),
edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
Duration::from_secs(3600),
))),
..Default::default()
},
on_error: |err| Box::pin(on_error(err)),
command_check: Some(|ctx| {
Box::pin(async move {
return Ok(!ctx.author().bot)
})
}),
..Default::default()
};
let framework = poise::Framework::builder()
.setup(move |ctx, _ready, framework| {
Box::pin(async move {
2024-12-09 14:40:46 +00:00
#[cfg(feature="GUILD_COMMAND")] {
2024-12-09 14:40:46 +00:00
use poise::samples::register_in_guild;
use serenity::all::GuildId;
let debug_guild_id = env::var("DEBUG_GUILD_ID")
.context("DEBUG_GUILD_ID not found in env")?
.parse::<u64>().unwrap();
2024-12-09 14:40:46 +00:00
register_in_guild(ctx, &framework.options().commands, GuildId::new(debug_guild_id)).await?;
}
2024-12-17 14:28:52 +00:00
#[cfg(not(feature="GUILD_COMMAND"))] {
2024-12-09 14:40:46 +00:00
use poise::samples::register_globally;
register_globally(ctx, &framework.options().commands).await?;
}
2024-12-17 14:28:52 +00:00
Ok(())
})
})
.options(options)
.build();
2024-02-14 12:22:29 +00:00
let token_str = "TOKEN";
2024-02-14 12:22:29 +00:00
#[cfg(feature="DEBUG")]
let token_str = "DEBUGTOKEN";
2024-10-06 12:02:24 +00:00
let token = env::var(token_str).context("TOKEN not found in env")?;
2024-02-14 12:22:29 +00:00
2024-12-17 14:28:52 +00:00
let intents = GatewayIntents::GUILDS
| GatewayIntents::GUILD_MESSAGES
2024-12-09 14:40:46 +00:00
| GatewayIntents::MESSAGE_CONTENT
| GatewayIntents::GUILD_VOICE_STATES
| GatewayIntents::GUILD_MESSAGE_REACTIONS
| GatewayIntents::GUILD_MESSAGE_TYPING;
2023-07-14 14:41:16 +00:00
2023-07-07 08:55:59 +00:00
let mut client = Client::builder(&token, intents)
2023-07-07 10:16:25 +00:00
.event_handler(Handler)
.framework(framework)
2024-12-09 14:40:46 +00:00
.register_songbird()
2023-07-07 10:16:25 +00:00
.await
.context("Failed to build client")?;
2023-07-14 14:41:16 +00:00
2023-07-07 10:16:25 +00:00
client.start().await?;
Ok(())
2023-07-05 09:06:04 +00:00
}