Followed Serenity's example code

This commit is contained in:
Michał Gdula 2023-02-25 12:56:24 +00:00
parent 59e293beff
commit c2e3982cc5
7 changed files with 184 additions and 20 deletions

View file

@ -0,0 +1,35 @@
use serenity::{
builder::CreateApplicationCommand,
model::prelude::command::CommandOptionType,
model::prelude::interaction::application_command::{
CommandDataOption,
CommandDataOptionValue,
}
};
pub fn run(options: &[CommandDataOption]) -> String {
let option = options
.get(0)
.expect("Expected attachment option")
.resolved
.as_ref()
.expect("Expected attachment object");
if let CommandDataOptionValue::Attachment(attachment) = option {
format!("Attachment name: {}, attachment size: {}", attachment.filename, attachment.size)
} else {
"Please provice a valid attachment".to_string()
}
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("attachmentinput").description("Test command for attachment input").create_option(
|option| {
option
.name("attachment")
.description("A file")
.kind(CommandOptionType::Attachment)
.required(true)
}
)
}

33
src/commands/id.rs Normal file
View file

@ -0,0 +1,33 @@
use serenity::{
builder::CreateApplicationCommand,
model::prelude::command::CommandOptionType,
model::prelude::interaction::application_command::{
CommandDataOption,
CommandDataOptionValue,
}
};
pub fn run(options: &[CommandDataOption]) -> String {
let option = options
.get(0)
.expect("Expected user option")
.resolved
.as_ref()
.expect("Expected user object");
if let CommandDataOptionValue::User(user, _member) = option {
format!("{}'s id is {}", user.tag(), user.id)
} else {
"Please provide a valid user".to_string()
}
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("id").description("Get a user id").create_option(|option| {
option
.name("id")
.description("The user to lookup")
.kind(CommandOptionType::User)
.required(true)
})
}

5
src/commands/mod.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod attachmentinput;
pub mod id;
pub mod numberinput;
pub mod ping;
pub mod wonderful_command;

View file

@ -0,0 +1,30 @@
use serenity::{
builder,
model::prelude::command::CommandOptionType,
};
pub fn register(
command: &mut builder::CreateApplicationCommand,
) -> &mut builder::CreateApplicationCommand {
command
.name("numberinput")
.description("Test command for number input")
.create_option(|option| {
option
.name("int")
.description("An integer from 1 to 69")
.kind(CommandOptionType::Integer)
.min_int_value(1)
.max_int_value(69)
.required(true)
})
.create_option(|option| {
option
.name("float")
.description("A float from -2.1 to 621.69")
.kind(CommandOptionType::Number)
.min_number_value(-2.1)
.max_number_value(621.69)
.required(true)
})
}

12
src/commands/ping.rs Normal file
View file

@ -0,0 +1,12 @@
use serenity::{
builder::CreateApplicationCommand,
model::prelude::interaction::application_command::CommandDataOption,
};
pub fn run(_options: &[CommandDataOption]) -> String {
"Pong! :3".to_string()
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("ping").description("Ping the bot")
}

View file

@ -0,0 +1,5 @@
use serenity::builder::CreateApplicationCommand;
pub fn _register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("wonderful_command").description("A wonderful command")
}

View file

@ -1,9 +1,17 @@
mod commands;
use dotenv::dotenv;
use std::env;
use serenity::{
async_trait,
model::{channel::Message, gateway::Ready},
// model::application::command::Command,
model::application::interaction::{
Interaction,
InteractionResponseType,
},
model::gateway::Ready,
model::id::GuildId,
prelude::*,
};
@ -12,33 +20,69 @@ struct Handler;
#[async_trait]
impl EventHandler for Handler {
// Create a handler for a new message event
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "ping" {
// Set Reply to "pong"
let reply: &str = "pong";
// Create a handler for slash commands
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
if let Interaction::ApplicationCommand(command) = interaction {
println!("Heard command: {:?}", command);
if let Err(why) = msg.channel_id.say(&ctx.http, reply).await {
// If an error occurs, display it
println!("Error sending message: {:?}", why);
}
}
// Create a string to hold the response
let content = match command.data.name.as_str() {
"ping" => commands::ping::run(&command.data.options),
"id" => commands::id::run(&command.data.options),
"attachmentinput" => commands::attachmentinput::run(&command.data.options),
_ => "Not implemented :c".to_string(),
};
if msg.content == "pale" {
// Set Reply to "We should ban this guy"
let reply: &str = "We should ban this guy";
if let Err(why) = msg.channel_id.say(&ctx.http, reply).await {
// If an error occurs, display it
println!("Error sending message: {:?}", why);
// Respond to the command
if let Err(why) = command
.create_interaction_response(&ctx.http, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|message| message.content(content))
})
.await
{
// Oups, something went wrong D:
println!("Error responding to command: {:?}", why);
}
}
}
// Create a handler for when the bot is ready
async fn ready(&self, _: Context, ready: Ready) {
async fn ready(&self, ctx: Context, ready: Ready) {
// Display that the bot is ready along with the username
println!("{} is ready Bitch!", ready.user.name);
// Set guild to register commands in
// Much faster than waiting for commands to be avaiable globally
let guild_id = GuildId(
env::var("GUILD_ID")
.expect("No GUILD_ID variable found!!!!!")
.parse()
.expect("GUILD_ID must be an intiger :<")
);
let _commands = GuildId::set_application_commands(&guild_id, &ctx.http, |commands| {
commands
.create_application_command(|command| commands::ping::register(command))
.create_application_command(|command| commands::id::register(command))
.create_application_command(|command| commands::numberinput::register(command))
.create_application_command(|command| commands::attachmentinput::register(command))
})
.await;
println!("Guild {} has been registered!", guild_id); // commands
// Set global commands
// This is slow and should be commented out when testing
/*
let guild_command = Command::create_global_application_command(&ctx.http, |command| {
commands::wonderful_command::register(command)
})
.await;
println!("Following comamnds available globally!: {:#?}", guild_command);
*/
}
}
@ -46,7 +90,7 @@ impl EventHandler for Handler {
async fn main() {
// Load dotenv file and yoink the token
dotenv().ok();
let token = env::var("DISCORD_TOKEN").expect("No Env enviroment variable found!");
let token = env::var("DISCORD_TOKEN").expect("No DISCORD_TOKEN variable found!!!!!");
// Set the bots intents
let intents = GatewayIntents::GUILD_MESSAGES