mirror of
https://github.com/Fluffy-Bean/Lynxie.git
synced 2025-05-29 23:03:15 +00:00
Followed Serenity's example code
This commit is contained in:
parent
59e293beff
commit
c2e3982cc5
7 changed files with 184 additions and 20 deletions
35
src/commands/attachmentinput.rs
Normal file
35
src/commands/attachmentinput.rs
Normal 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
33
src/commands/id.rs
Normal 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
5
src/commands/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
pub mod attachmentinput;
|
||||||
|
pub mod id;
|
||||||
|
pub mod numberinput;
|
||||||
|
pub mod ping;
|
||||||
|
pub mod wonderful_command;
|
30
src/commands/numberinput.rs
Normal file
30
src/commands/numberinput.rs
Normal 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
12
src/commands/ping.rs
Normal 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")
|
||||||
|
}
|
5
src/commands/wonderful_command.rs
Normal file
5
src/commands/wonderful_command.rs
Normal 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")
|
||||||
|
}
|
84
src/main.rs
84
src/main.rs
|
@ -1,9 +1,17 @@
|
||||||
|
mod commands;
|
||||||
|
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use serenity::{
|
use serenity::{
|
||||||
async_trait,
|
async_trait,
|
||||||
model::{channel::Message, gateway::Ready},
|
// model::application::command::Command,
|
||||||
|
model::application::interaction::{
|
||||||
|
Interaction,
|
||||||
|
InteractionResponseType,
|
||||||
|
},
|
||||||
|
model::gateway::Ready,
|
||||||
|
model::id::GuildId,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -12,33 +20,69 @@ struct Handler;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventHandler for Handler {
|
impl EventHandler for Handler {
|
||||||
// Create a handler for a new message event
|
// Create a handler for slash commands
|
||||||
async fn message(&self, ctx: Context, msg: Message) {
|
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||||
if msg.content == "ping" {
|
if let Interaction::ApplicationCommand(command) = interaction {
|
||||||
// Set Reply to "pong"
|
println!("Heard command: {:?}", command);
|
||||||
let reply: &str = "pong";
|
|
||||||
|
|
||||||
if let Err(why) = msg.channel_id.say(&ctx.http, reply).await {
|
// Create a string to hold the response
|
||||||
// If an error occurs, display it
|
let content = match command.data.name.as_str() {
|
||||||
println!("Error sending message: {:?}", why);
|
"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" {
|
// Respond to the command
|
||||||
// Set Reply to "We should ban this guy"
|
if let Err(why) = command
|
||||||
let reply: &str = "We should ban this guy";
|
.create_interaction_response(&ctx.http, |response| {
|
||||||
|
response
|
||||||
if let Err(why) = msg.channel_id.say(&ctx.http, reply).await {
|
.kind(InteractionResponseType::ChannelMessageWithSource)
|
||||||
// If an error occurs, display it
|
.interaction_response_data(|message| message.content(content))
|
||||||
println!("Error sending message: {:?}", why);
|
})
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
// Oups, something went wrong D:
|
||||||
|
println!("Error responding to command: {:?}", why);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a handler for when the bot is ready
|
// 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
|
// Display that the bot is ready along with the username
|
||||||
println!("{} is ready Bitch!", ready.user.name);
|
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() {
|
async fn main() {
|
||||||
// Load dotenv file and yoink the token
|
// Load dotenv file and yoink the token
|
||||||
dotenv().ok();
|
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
|
// Set the bots intents
|
||||||
let intents = GatewayIntents::GUILD_MESSAGES
|
let intents = GatewayIntents::GUILD_MESSAGES
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue