chore(rust): remove Rust source files and Cargo configuration

This commit is contained in:
Jose Daniel G. Percy 2025-04-23 21:39:06 +08:00
parent 05fec6747d
commit 5c632556b7
12 changed files with 10 additions and 276 deletions

31
.gitignore vendored
View File

@ -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

View File

@ -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"

View File

@ -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(())
}),
}
}

View File

@ -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(())
}),
}
}

View File

@ -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<Box<dyn std::future::Future<Output = Result<()>> + Send>>,
}
inventory::collect!(SlashCommand);
/// Returns all registered slash commands.
pub fn get_slash_commands() -> Vec<&'static SlashCommand> {
inventory::iter::<SlashCommand>
.into_iter()
.collect()
}
// Register individual command modules
pub mod ping;
pub mod join;
pub mod play;
pub mod leave;

View File

@ -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(())
}),
}
}

View File

@ -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(())
}),
}
}

View File

@ -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<VoiceState>, new: VoiceState) {
if let Some(guild) = new.guild_id {
let data_read = ctx.data.read().await;
if let Some(lavalink) = data_read.get::<LavalinkClientKey>() {
lavalink.on_voice_state_update(&new).await;
}
}
}
}

View File

@ -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<LavalinkClient> {
let host = env_var("LAVALINK_HOST");
let port = env_var("LAVALINK_PORT").parse::<u16>()?;
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)
}

View File

@ -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::<state::LavalinkClientKey>(lavalink_client);
}
client.start().await?;
Ok(())
}

View File

@ -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;
}

View File

@ -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))
}