From 5c632556b7de2ee0f80d3ffcfb2ace4cea23fda3 Mon Sep 17 00:00:00 2001 From: aki Date: Wed, 23 Apr 2025 21:39:06 +0800 Subject: [PATCH] chore(rust): remove Rust source files and Cargo configuration --- .gitignore | 31 +++++++++----------------- Cargo.toml | 16 -------------- src/commands/join.rs | 16 -------------- src/commands/leave.rs | 16 -------------- src/commands/mod.rs | 26 ---------------------- src/commands/ping.rs | 16 -------------- src/commands/play.rs | 26 ---------------------- src/handler.rs | 49 ----------------------------------------- src/lavalink_handler.rs | 37 ------------------------------- src/main.rs | 38 -------------------------------- src/state.rs | 8 ------- src/utils.rs | 7 ------ 12 files changed, 10 insertions(+), 276 deletions(-) delete mode 100644 Cargo.toml delete mode 100644 src/commands/join.rs delete mode 100644 src/commands/leave.rs delete mode 100644 src/commands/mod.rs delete mode 100644 src/commands/ping.rs delete mode 100644 src/commands/play.rs delete mode 100644 src/handler.rs delete mode 100644 src/lavalink_handler.rs delete mode 100644 src/main.rs delete mode 100644 src/state.rs delete mode 100644 src/utils.rs diff --git a/.gitignore b/.gitignore index 637f657..fd5cd29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,6 @@ -# ---> Rust -# Generated by Cargo -# will have compiled files and executables -debug/ -/target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock +# Node modules +node_modules/ +dist/ # dotenv environment variables .env @@ -14,18 +8,13 @@ Cargo.lock # VSCode settings .vscode/ -# macOS +# Mac system files .DS_Store -# These are backup files generated by rustfmt -**/*.rs.bk +# Lockfiles +pnpm-lock.yaml -# MSVC Windows builds of rustc generate these, which store debugging information -*.pdb - -# RustRover -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +# Logs +npm-debug.log* +logs/ +*.log diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index 725b448..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "discord-music-bot" -version = "1.0.0" -edition = "2021" - -[dependencies] -serenity = { version = "0.12", features = ["client", "gateway", "cache", "model", "http", "builder", "voice", "rustls_backend", "application"] } -lavalink-rs = { version = "0.14", features = ["serenity", "tungstenite-rustls-native-roots"] } -tokio = { version = "1.28", features = ["macros", "rt-multi-thread"] } -anyhow = "1.0" -tracing = "0.1" -tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] } -dotenv = "0.15" -futures = "0.3" -inventory = { version = "0.3", features = ["proc-macro"] } -url = "2.4" diff --git a/src/commands/join.rs b/src/commands/join.rs deleted file mode 100644 index 0d4c58f..0000000 --- a/src/commands/join.rs +++ /dev/null @@ -1,16 +0,0 @@ -use super::SlashCommand; -use serenity::builder::CreateCommand; -use serenity::prelude::Context; -use serenity::model::application::interaction::Interaction; -use anyhow::Result; - -inventory::submit! { - SlashCommand { - name: "join", - register: |c| c.name("join").description("Joins your voice channel"), - handler: |ctx: Context, interaction: Interaction| Box::pin(async move { - // TODO: Implement join logic (e.g., move bot to user voice channel) - Ok(()) - }), - } -} \ No newline at end of file diff --git a/src/commands/leave.rs b/src/commands/leave.rs deleted file mode 100644 index 2d1f519..0000000 --- a/src/commands/leave.rs +++ /dev/null @@ -1,16 +0,0 @@ -use super::SlashCommand; -use serenity::builder::CreateApplicationCommand; -use serenity::prelude::Context; -use serenity::model::interactions::Interaction; -use anyhow::Result; - -inventory::submit! { - SlashCommand { - name: "leave", - register: |c| c.name("leave").description("Leaves the voice channel"), - handler: |ctx: Context, interaction: Interaction| Box::pin(async move { - // TODO: Implement leave logic (e.g., disconnect from voice channel) - Ok(()) - }), - } -} \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs deleted file mode 100644 index 5d26961..0000000 --- a/src/commands/mod.rs +++ /dev/null @@ -1,26 +0,0 @@ -use serenity::builder::CreateCommand; -use serenity::prelude::Context; -use serenity::model::interactions::Interaction; -use anyhow::Result; - -/// Defines a slash command with its registration and handler. -pub struct SlashCommand { - pub name: &'static str, - pub register: fn(&mut CreateCommand) -> &mut CreateCommand, - pub handler: fn(Context, Interaction) -> std::pin::Pin> + Send>>, -} - -inventory::collect!(SlashCommand); - -/// Returns all registered slash commands. -pub fn get_slash_commands() -> Vec<&'static SlashCommand> { - inventory::iter:: - .into_iter() - .collect() -} - -// Register individual command modules -pub mod ping; -pub mod join; -pub mod play; -pub mod leave; \ No newline at end of file diff --git a/src/commands/ping.rs b/src/commands/ping.rs deleted file mode 100644 index 5830706..0000000 --- a/src/commands/ping.rs +++ /dev/null @@ -1,16 +0,0 @@ -use super::SlashCommand; -use serenity::builder::CreateCommand; -use serenity::prelude::Context; -use serenity::model::application::interaction::Interaction; -use anyhow::Result; - -inventory::submit! { - SlashCommand { - name: "ping", - register: |c: &mut CreateCommand| c.name("ping").description("Replies with Pong!"), - handler: |ctx: Context, interaction: Interaction| Box::pin(async move { - // TODO: Implement ping logic (e.g., respond with "Pong!") - Ok(()) - }), - } -} \ No newline at end of file diff --git a/src/commands/play.rs b/src/commands/play.rs deleted file mode 100644 index ac26f71..0000000 --- a/src/commands/play.rs +++ /dev/null @@ -1,26 +0,0 @@ -use super::SlashCommand; -use serenity::builder::CreateApplicationCommand; -use serenity::model::interactions::application_command::ApplicationCommandOptionType; -use serenity::prelude::Context; -use serenity::model::interactions::Interaction; -use anyhow::Result; - -inventory::submit! { - SlashCommand { - name: "play", - register: |c| { - c.name("play") - .description("Plays audio from a given URL") - .create_option(|o| { - o.name("url") - .description("Track URL to play") - .kind(ApplicationCommandOptionType::String) - .required(true) - }) - }, - handler: |ctx: Context, interaction: Interaction| Box::pin(async move { - // TODO: Implement play logic (e.g., fetch URL argument and instruct Lavalink to play) - Ok(()) - }), - } -} \ No newline at end of file diff --git a/src/handler.rs b/src/handler.rs deleted file mode 100644 index 2f5f33e..0000000 --- a/src/handler.rs +++ /dev/null @@ -1,49 +0,0 @@ -use serenity::async_trait; -use serenity::model::gateway::Ready; -use serenity::model::application::interaction::{Interaction, InteractionResponseType}; -use serenity::model::channel::VoiceState; -use serenity::prelude::*; -use anyhow::Result; -use tracing::info; -use crate::commands::get_slash_commands; -use crate::state::LavalinkClientKey; - -pub struct Handler; - -#[async_trait] -impl EventHandler for Handler { - async fn ready(&self, ctx: Context, ready: Ready) { - info!("{} is connected!", ready.user.name); - let commands = get_slash_commands(); - for cmd in commands { - match ctx.http.create_global_application_command(|c| (cmd.register)(c)).await { - Ok(_) => info!("Registered slash command: {}", cmd.name), - Err(err) => info!("Failed to register {}: {:?}", cmd.name, err), - } - } - } - - async fn interaction_create(&self, ctx: Context, interaction: Interaction) { - if let Interaction::ApplicationCommand(ref command) = interaction { - if let Err(err) = command.defer(&ctx.http).await { - info!("Failed to defer response: {:?}", err); - } - for cmd in get_slash_commands() { - if cmd.name == command.data.name { - if let Err(err) = (cmd.handler)(ctx.clone(), interaction.clone()).await { - info!("Command error {}: {:?}", cmd.name, err); - } - } - } - } - } - - async fn voice_state_update(&self, ctx: Context, old: Option, new: VoiceState) { - if let Some(guild) = new.guild_id { - let data_read = ctx.data.read().await; - if let Some(lavalink) = data_read.get::() { - lavalink.on_voice_state_update(&new).await; - } - } - } -} \ No newline at end of file diff --git a/src/lavalink_handler.rs b/src/lavalink_handler.rs deleted file mode 100644 index 095d059..0000000 --- a/src/lavalink_handler.rs +++ /dev/null @@ -1,37 +0,0 @@ -// src/lavalink_handler.rs -use anyhow::Result; -use tracing::info; -use async_trait::async_trait; -use lavalink_rs::prelude::{LavalinkClient, LavalinkClientBuilder, EventHandler, TrackStartEvent, TrackEndEvent}; -use crate::utils::env_var; - -/// Handles Lavalink events such as track start and end. -pub struct LavalinkHandler; - -#[async_trait] -impl EventHandler for LavalinkHandler { - async fn track_start(&self, _client: &LavalinkClient, event: &TrackStartEvent) { - info!("Track started: {}", event.track); - } - - async fn track_end(&self, _client: &LavalinkClient, event: &TrackEndEvent) { - info!("Track ended: {}", event.track); - } - - // TODO: Add more event handlers as needed -} - -/// Initializes and returns a Lavalink client. -pub async fn create_lavalink_client() -> Result { - let host = env_var("LAVALINK_HOST"); - let port = env_var("LAVALINK_PORT").parse::()?; - let password = env_var("LAVALINK_PASSWORD"); - let url = format!("ws://{}:{}", host, port); - - let builder = LavalinkClientBuilder::new(&url) - .password(&password) - .event_handler(LavalinkHandler); - - let client = builder.build().await?; - Ok(client) -} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 6da5d11..0000000 --- a/src/main.rs +++ /dev/null @@ -1,38 +0,0 @@ -// src/main.rs -mod handler; -mod lavalink_handler; -mod state; -mod utils; -mod commands; - -use anyhow::Result; -use dotenv::dotenv; -use std::env; -use tracing_subscriber; -use serenity::prelude::TypeMapKey; -use serenity::Client; -use crate::lavalink_handler::create_lavalink_client; - -#[tokio::main] -async fn main() -> Result<()> { - dotenv().ok(); - tracing_subscriber::fmt().with_env_filter("info").init(); - - let token = env::var("DISCORD_TOKEN")?; - - // Initialize Lavalink client - let lavalink_client = create_lavalink_client().await?; - - let mut client = Client::builder(&token) - .event_handler(handler::Handler) - .await?; - - { - let mut data = client.data.write().await; - data.insert::(lavalink_client); - } - - client.start().await?; - - Ok(()) -} \ No newline at end of file diff --git a/src/state.rs b/src/state.rs deleted file mode 100644 index 05b53e6..0000000 --- a/src/state.rs +++ /dev/null @@ -1,8 +0,0 @@ -// src/state.rs -use serenity::prelude::TypeMapKey; -use lavalink_rs::prelude::LavalinkClient; - -pub struct LavalinkClientKey; -impl TypeMapKey for LavalinkClientKey { - type Value = LavalinkClient; -} diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index 67d2f45..0000000 --- a/src/utils.rs +++ /dev/null @@ -1,7 +0,0 @@ -// src/utils.rs -use std::env; - -/// Retrieves an environment variable or panics if it's not set. -pub fn env_var(key: &str) -> String { - env::var(key).expect(&format!("Environment variable {} not set", key)) -}