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

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