moover_rust/src/main.rs

130 lines
3.6 KiB
Rust
Raw Normal View History

use poise::samples::{register_globally, register_in_guild};
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, GuildId, Message};
use serenity::Client;
2024-02-14 12:22:29 +00:00
use tokio_cron_scheduler::{JobScheduler, Job};
2023-07-07 08:55:59 +00:00
use util::security::dotenv_var;
2024-02-14 12:22:29 +00:00
2023-07-15 09:18:08 +00:00
mod message_handler;
use message_handler::handle;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
2023-07-15 09:18:08 +00:00
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;
2023-07-05 09:06:04 +00:00
struct Handler;
use commands::say::say;
use types::Error;
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) {
2024-02-18 11:30:02 +00:00
println!("{} v0.3 is connected!", ready.user.name);
2024-02-14 12:22:29 +00:00
#[cfg(feature="RELEASE")] {
use util::debug::hello;
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;
// create poise framework for registering commands
let options = poise::FrameworkOptions {
commands: vec![say()],
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 debug_guild_id = dotenv_var("DEBUG_GUILD_ID")
.context("DEBUG_GUILD_ID not found in env")?
.parse::<u64>().unwrap();
let framework = poise::Framework::builder()
.setup(move |ctx, _ready, framework| {
Box::pin(async move {
register_in_guild(ctx, &framework.options().commands, GuildId::new(debug_guild_id)).await?;
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";
let token = dotenv_var(token_str).context("TOKEN not found in env")?;
2024-02-14 12:22:29 +00:00
let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
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)
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
}