95 lines
2.4 KiB
Rust
95 lines
2.4 KiB
Rust
#![warn(clippy::all)]
|
|
|
|
mod commands;
|
|
mod personality;
|
|
mod queue;
|
|
|
|
use commands::*;
|
|
use openai::set_key;
|
|
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
|
|
|
use std::{env, sync::Arc};
|
|
|
|
use anyhow::Result;
|
|
use parking_lot::Mutex;
|
|
use poise::serenity_prelude as serenity;
|
|
|
|
pub struct Data {
|
|
queue: Arc<Mutex<queue::Queue>>,
|
|
}
|
|
pub type Error = Box<dyn std::error::Error + Send + Sync>;
|
|
pub type CommandContext<'a> = poise::Context<'a, Data, Error>;
|
|
|
|
async fn event_event_handler(
|
|
_ctx: &serenity::Context,
|
|
event: &serenity::FullEvent,
|
|
_framework: poise::FrameworkContext<'_, Data, Error>,
|
|
_user_data: &Data,
|
|
) -> Result<(), Error> {
|
|
if let serenity::FullEvent::Ready { data_about_bot } = event {
|
|
println!("{} is connected!", data_about_bot.user.name)
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Registers slash commands in this guild or globally
|
|
#[poise::command(prefix_command, hide_in_help)]
|
|
async fn register(ctx: CommandContext<'_>) -> Result<(), Error> {
|
|
poise::builtins::register_application_commands_buttons(ctx).await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
tracing_subscriber::registry()
|
|
.with(fmt::layer())
|
|
.with(EnvFilter::from_default_env())
|
|
.init();
|
|
|
|
set_key(env::var("OPENAI_KEY").expect("Expected an OpenAI key in the environment: OPENAI_KEY"));
|
|
|
|
let token =
|
|
env::var("DISCORD_TOKEN").expect("Expected a bot token in the environment: DISCORD_TOKEN");
|
|
|
|
let options = poise::FrameworkOptions {
|
|
commands: vec![
|
|
register(),
|
|
join(),
|
|
leave(),
|
|
play(),
|
|
queue(),
|
|
stop(),
|
|
skip(),
|
|
pause(),
|
|
resume(),
|
|
clear(),
|
|
],
|
|
event_handler: |ctx, event, framework, user_data| {
|
|
Box::pin(event_event_handler(ctx, event, framework, user_data))
|
|
},
|
|
..Default::default()
|
|
};
|
|
|
|
let intents = serenity::GatewayIntents::non_privileged();
|
|
|
|
let framework = poise::Framework::builder()
|
|
.options(options)
|
|
.setup(|_ctx, _data, _framework| {
|
|
Box::pin(async move {
|
|
Ok(Data {
|
|
queue: Arc::new(Mutex::new(queue::Queue::new())),
|
|
})
|
|
})
|
|
})
|
|
.build();
|
|
|
|
let client = serenity::ClientBuilder::new(token, intents)
|
|
.framework(framework)
|
|
.await?
|
|
.start()
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|