mirror of
https://github.com/eRgo35/lyra.git
synced 2026-02-04 12:26:10 +01:00
codebase migration to poise
This commit is contained in:
163
src/main.rs
163
src/main.rs
@@ -1,27 +1,13 @@
|
||||
use serenity::gateway::ActivityData;
|
||||
use serenity::model::prelude::Message;
|
||||
use songbird::SerenityInit;
|
||||
|
||||
use reqwest::Client as HttpClient;
|
||||
|
||||
use serenity::client::Context;
|
||||
|
||||
use serenity::{
|
||||
async_trait,
|
||||
client::{Client, EventHandler},
|
||||
framework::{
|
||||
standard::{macros::group, macros::hook, Configuration},
|
||||
StandardFramework,
|
||||
},
|
||||
model::gateway::Ready,
|
||||
prelude::GatewayIntents,
|
||||
};
|
||||
|
||||
use tracing::info;
|
||||
use poise::serenity_prelude::{self as serenity, ActivityData};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tracing::{info, warn, error};
|
||||
|
||||
mod commands;
|
||||
|
||||
// music management commands
|
||||
// commands: music
|
||||
use crate::commands::music::deafen::*;
|
||||
use crate::commands::music::join::*;
|
||||
use crate::commands::music::leave::*;
|
||||
@@ -34,57 +20,130 @@ use crate::commands::music::loopcurrent::*;
|
||||
use crate::commands::music::pause::*;
|
||||
use crate::commands::music::resume::*;
|
||||
|
||||
// tools
|
||||
// commands: tools
|
||||
use crate::commands::tools::ping::*;
|
||||
|
||||
// kashi
|
||||
// commands: kashi
|
||||
use crate::commands::kashi::kashi::*;
|
||||
|
||||
struct Handler;
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
info!("{} [{}] connected successfully!", ready.user.name, ready.user.id);
|
||||
let prefix = std::env::var("PREFIX").expect("Environment variable `PREFIX` not found!");
|
||||
ctx.set_activity(Some(ActivityData::listening(prefix + "help")));
|
||||
pub struct Data;
|
||||
|
||||
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
|
||||
match error {
|
||||
poise::FrameworkError::Setup { error, .. } => panic!("Failed to start bot {:?}", error),
|
||||
poise::FrameworkError::Command { error, ctx, .. } => {
|
||||
warn!("Error in command `{}`: {:?}", ctx.command().name, error);
|
||||
}
|
||||
error => {
|
||||
if let Err(e) = poise::builtins::on_error(error).await {
|
||||
error!("Error while handling error: {}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[hook]
|
||||
async fn before(_: &Context, msg: &Message, command_name: &str) -> bool {
|
||||
info!(
|
||||
"Received command [{}] from user [{}]",
|
||||
command_name, msg.author.name
|
||||
);
|
||||
true
|
||||
// this is for debug only
|
||||
#[poise::command(prefix_command)]
|
||||
pub async fn register(ctx: Context<'_>) -> Result<(), Error> {
|
||||
poise::builtins::register_application_commands_buttons(ctx).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[group]
|
||||
#[commands(
|
||||
join, deafen, leave, mute, play, ping, kashi, queue, stop, skip, loopcurrent, pause, resume
|
||||
)]
|
||||
struct General;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenv::dotenv().expect("Failed to load .env file.");
|
||||
// logger and dotenv initialization
|
||||
tracing_subscriber::fmt::init();
|
||||
dotenv::dotenv().expect("Failed to load .env file.");
|
||||
|
||||
let token =
|
||||
std::env::var("DISCORD_TOKEN").expect("Environment variable `DISCORD_TOKEN` not found!");
|
||||
let token = std::env::var("DISCORD_TOKEN").expect("Environment variable `DISCORD_TOKEN` not found!");
|
||||
let prefix = std::env::var("PREFIX").expect("Environment variable `PREFIX` not found!");
|
||||
|
||||
let framework = StandardFramework::new().before(before).group(&GENERAL_GROUP);
|
||||
framework.configure(Configuration::new().prefix(prefix));
|
||||
let commands = vec![
|
||||
// commands: music
|
||||
deafen(),
|
||||
join(),
|
||||
leave(),
|
||||
loopcurrent(),
|
||||
mute(),
|
||||
pause(),
|
||||
play(),
|
||||
queue(),
|
||||
resume(),
|
||||
skip(),
|
||||
stop(),
|
||||
// commands: tools
|
||||
ping(),
|
||||
// commands: kashi
|
||||
kashi(),
|
||||
// commands: debug
|
||||
register(),
|
||||
];
|
||||
|
||||
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
|
||||
let options = poise::FrameworkOptions {
|
||||
commands,
|
||||
prefix_options: poise::PrefixFrameworkOptions {
|
||||
prefix: Some(prefix.to_string().into()),
|
||||
edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
|
||||
Duration::from_secs(3600),
|
||||
))),
|
||||
additional_prefixes: vec![],
|
||||
..Default::default()
|
||||
},
|
||||
|
||||
let mut client = Client::builder(&token, intents)
|
||||
on_error: |error| Box::pin(on_error(error)),
|
||||
|
||||
pre_command: |ctx| {
|
||||
Box::pin(async move {
|
||||
info!("Executing command {}...", ctx.command().qualified_name);
|
||||
})
|
||||
},
|
||||
|
||||
post_command: |ctx| {
|
||||
Box::pin(async move {
|
||||
info!("Executed command {}!", ctx.command().qualified_name);
|
||||
})
|
||||
},
|
||||
|
||||
command_check: Some(|ctx| {
|
||||
Box::pin(async move {
|
||||
if ctx.author().id == 123456789 {
|
||||
return Ok(false);
|
||||
}
|
||||
Ok(true)
|
||||
})
|
||||
}),
|
||||
|
||||
skip_checks_for_owners: false,
|
||||
event_handler: |_ctx, event, _framework, _data| {
|
||||
Box::pin(async move {
|
||||
info!("Got an event in event handler: {:?}", event.snake_case_name());
|
||||
Ok(())
|
||||
})
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let framework = poise::Framework::builder()
|
||||
.setup(move |ctx, ready, _framework| {
|
||||
Box::pin(async move {
|
||||
info!("{} [{}] connected successfully!", ready.user.name, ready.user.id);
|
||||
ctx.set_activity(Some(ActivityData::listening(prefix + "help")));
|
||||
// poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||
|
||||
Ok(Data {})
|
||||
})
|
||||
})
|
||||
.options(options)
|
||||
.build();
|
||||
|
||||
let intents = serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
|
||||
|
||||
let mut client = serenity::ClientBuilder::new(token, intents)
|
||||
.framework(framework)
|
||||
.register_songbird()
|
||||
.event_handler(Handler)
|
||||
.type_map_insert::<HttpKey>(HttpClient::new())
|
||||
.await
|
||||
.expect("Error creating client");
|
||||
|
||||
@@ -92,9 +151,9 @@ async fn main() {
|
||||
let _ = client
|
||||
.start()
|
||||
.await
|
||||
.map_err(|why| println!("Client ended: {:?}", why));
|
||||
.map_err(|why| error!("Client ended: {:?}", why));
|
||||
});
|
||||
|
||||
let _signal_err = tokio::signal::ctrl_c().await;
|
||||
println!("Recieved Ctrl-C, shutting down.");
|
||||
warn!("Recieved Ctrl-C, shutting down.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user