Rust Migration
This commit is contained in:
6
src/commands/audio/mod.rs
Normal file
6
src/commands/audio/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub mod pause;
|
||||
pub mod play;
|
||||
pub mod resume;
|
||||
pub mod skip;
|
||||
pub mod stop;
|
||||
pub mod volume;
|
||||
0
src/commands/audio/pause.rs
Normal file
0
src/commands/audio/pause.rs
Normal file
18
src/commands/audio/play.rs
Normal file
18
src/commands/audio/play.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use log::debug;
|
||||
use serenity::{model::prelude::{interaction::application_command::CommandDataOption, application_command::CommandDataOptionValue}, builder::CreateApplicationCommand};
|
||||
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
||||
|
||||
pub async fn run(command: &ApplicationCommandInteraction) -> String {
|
||||
let track_option: &CommandDataOptionValue = command.data.options.get(0).expect("Expected track option").resolved.as_ref().expect("Expected track option to be resolved");
|
||||
debug!("Play command executed with track: {:?}", track_option);
|
||||
"Playing xyz".to_string()
|
||||
}
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
command.name("play").description("Plays the given track").create_option(|option| { option
|
||||
.name("track")
|
||||
.description("The track to be played")
|
||||
.kind(serenity::model::prelude::command::CommandOptionType::String)
|
||||
.required(true)
|
||||
})
|
||||
}
|
||||
0
src/commands/audio/resume.rs
Normal file
0
src/commands/audio/resume.rs
Normal file
0
src/commands/audio/skip.rs
Normal file
0
src/commands/audio/skip.rs
Normal file
0
src/commands/audio/stop.rs
Normal file
0
src/commands/audio/stop.rs
Normal file
0
src/commands/audio/volume.rs
Normal file
0
src/commands/audio/volume.rs
Normal file
0
src/commands/help.rs
Normal file
0
src/commands/help.rs
Normal file
2
src/commands/mod.rs
Normal file
2
src/commands/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod ping;
|
||||
pub mod audio;
|
||||
11
src/commands/ping.rs
Normal file
11
src/commands/ping.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use log::debug;
|
||||
use serenity::{model::prelude::interaction::application_command::CommandDataOption, builder::CreateApplicationCommand};
|
||||
|
||||
pub fn run(_options: &[CommandDataOption]) -> String {
|
||||
debug!("Ping command executed");
|
||||
"pong".to_string()
|
||||
}
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
command.name("ping").description("Replies with pong")
|
||||
}
|
||||
94
src/main.rs
Normal file
94
src/main.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
use std::collections::HashSet;
|
||||
use std::env;
|
||||
|
||||
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::gateway::Ready;
|
||||
use serenity::http::Http;
|
||||
use serenity::model::prelude::GuildId;
|
||||
use serenity::prelude::*;
|
||||
|
||||
mod commands;
|
||||
struct Handler;
|
||||
|
||||
#[async_trait]
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
dotenv().ok();
|
||||
|
||||
let token: String = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
||||
let intents: GatewayIntents = GatewayIntents::all();
|
||||
|
||||
let http: Http = Http::new(&token);
|
||||
let (owners, _bot_id) = match http.get_current_application_info().await {
|
||||
Ok(info) => {
|
||||
let mut owners: HashSet<serenity::model::id::UserId> = HashSet::new();
|
||||
if let Some(team) = info.team {
|
||||
owners.insert(team.owner_user_id);
|
||||
} else {
|
||||
owners.insert(info.owner.id);
|
||||
}
|
||||
match http.get_current_user().await {
|
||||
Ok(bot) => (owners, bot.id),
|
||||
Err(why) => panic!("Could not access the bot id: {:?}", why)
|
||||
}
|
||||
},
|
||||
Err(why) => panic!("Could not access application info: {:?}", why)
|
||||
};
|
||||
|
||||
let framework = StandardFramework::new()
|
||||
.configure(|c| c
|
||||
.owners(owners)
|
||||
.prefix("!")
|
||||
);
|
||||
|
||||
let mut client = Client::builder(token, intents)
|
||||
.event_handler(Handler)
|
||||
.framework(framework)
|
||||
.await
|
||||
.expect("Error creating client");
|
||||
|
||||
if let Err(why) = client.start_autosharded().await {
|
||||
error!("An error occurred while running the client: {:?}", why);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user