Adding play command

This commit is contained in:
2023-07-05 09:32:33 -04:00
parent 1d9cd4adcf
commit d54b8d3823
7 changed files with 218 additions and 42 deletions

View File

@@ -1,15 +1,16 @@
use std::collections::HashSet;
use std::env;
use commands::audio::create_response;
use dotenv::dotenv;
use log::{error, warn, info};
use serenity::async_trait;
use serenity::framework::StandardFramework;
use serenity::model::application::interaction::{Interaction, InteractionResponseType};
use serenity::model::application::interaction::Interaction;
use serenity::model::gateway::Ready;
use serenity::http::Http;
use serenity::model::prelude::GuildId;
use serenity::prelude::*;
use songbird::SerenityInit;
mod commands;
struct Handler;
@@ -18,43 +19,43 @@ struct Handler;
impl EventHandler for Handler {
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
if let Interaction::ApplicationCommand(command) = interaction {
let content: String = match command.data.name.as_str() {
"ping" => commands::ping::run(&command.data.options),
"play" => commands::audio::play::run(&command).await,
_ => "Unknown command".to_string()
};
if let Err(why) = command.create_interaction_response(&ctx.http, |response: &mut serenity::builder::CreateInteractionResponse<'_>| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|message: &mut serenity::builder::CreateInteractionResponseData<'_>| message.content(content))
}).await {
warn!("Cannot respond to slash command: {}", why);
match command.data.name.as_str() {
"play" => commands::audio::play::run(&ctx, &command).await,
_ => {
let content: String = match command.data.name.as_str() {
"ping" => commands::ping::run(&command.data.options),
_ => "Unknown command".to_string()
};
if let Err(why) = create_response(&ctx, &command, content).await {
warn!("Cannot respond to slash command: {}", why);
}
}
}
}
}
async fn ready(&self, ctx: Context, ready: Ready) {
if ready.guilds.is_empty() {
warn!("No ready guilds found");
}
for guild in ready.guilds {
if let Some(guild) = guild.id.to_guild_cached(&ctx.cache) {
info!("{} is connected to {}", ready.user.name, guild.name);
let commands: Result<Vec<serenity::model::prelude::command::Command>, SerenityError> = GuildId::set_application_commands(&guild.id, &ctx.http, |commands| {
commands.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::ping::register(command) })
.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::audio::play::register(command) })
}).await;
match commands {
Ok(commands) => info!("Registered {} commands", commands.len()),
Err(why) => error!("Could not register commands: {:?}", why)
}
}
let commands = guild.id.set_application_commands(&ctx.http, |commands| {
commands.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::ping::register(command) })
.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { commands::audio::play::register(command) })
}).await;
match commands {
Ok(c) => info!("Registered {} commands for guild {}", c.len(), guild.id.0),
Err(why) => error!("Could not register commands for guild {}: {:?}", guild.id.0, why)
};
}
}
}
#[tokio::main]
async fn main() {
env_logger::init();
dotenv().ok();
env_logger::init();
let token: String = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let intents: GatewayIntents = GatewayIntents::all();
@@ -85,6 +86,7 @@ async fn main() {
let mut client = Client::builder(token, intents)
.event_handler(Handler)
.framework(framework)
.register_songbird()
.await
.expect("Error creating client");