From b03625443a8c00a78048549183ade6096297df08 Mon Sep 17 00:00:00 2001 From: Victor Mignot Date: Sun, 20 Nov 2022 18:18:29 -0500 Subject: [PATCH] Begin Discord client implementation --- src/discord.rs | 2 ++ src/discord/client.rs | 32 +++++++++++++++++ src/discord/event_handler.rs | 68 ++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/discord.rs create mode 100644 src/discord/client.rs create mode 100644 src/discord/event_handler.rs diff --git a/src/discord.rs b/src/discord.rs new file mode 100644 index 0000000..85bfb09 --- /dev/null +++ b/src/discord.rs @@ -0,0 +1,2 @@ +mod client; +mod event_handler; diff --git a/src/discord/client.rs b/src/discord/client.rs new file mode 100644 index 0000000..31f3d19 --- /dev/null +++ b/src/discord/client.rs @@ -0,0 +1,32 @@ +use serenity::{prelude::GatewayIntents, Client}; + +use crate::environment::get_env_variable; + +use super::event_handler::Handler; + +const INTENTS: GatewayIntents = GatewayIntents::empty(); + +struct DiscordClient { + serenity_client: Option, + + discord_token: String, +} + +impl DiscordClient { + pub async fn new() -> Self { + let discord_token = get_env_variable("DISCORD_TOKEN"); + + let serenity_client = match Client::builder(&discord_token, INTENTS) + .event_handler(Handler) + .await + { + Ok(c) => c, + Err(e) => panic!("Failed to instantiate Discord Client: {e}"), + }; + + DiscordClient { + discord_token, + serenity_client: Some(serenity_client), + } + } +} diff --git a/src/discord/event_handler.rs b/src/discord/event_handler.rs new file mode 100644 index 0000000..8423877 --- /dev/null +++ b/src/discord/event_handler.rs @@ -0,0 +1,68 @@ +use serenity::{ + async_trait, + model::gateway::Ready, + model::{ + application::command::{Command, CommandOptionType}, + prelude::ResumedEvent, + }, + prelude::{Context, EventHandler}, +}; + +const MAX_ARGS_NUMBER: u32 = 25; + +pub struct Handler; + +#[async_trait] +impl EventHandler for Handler { + async fn ready(&self, ctx: Context, ready: Ready) { + println!("Successfully connected as {}", ready.user.name); + + let _ = Command::create_global_application_command(&ctx.http, |command| { + command + .name("tag new") + .description("Add a new tag") + .create_option(|option| { + option + .name("new_tag") + .description("The new tag to add") + .kind(CommandOptionType::String) + .required(true) + }) + }) + .await; + + let _ = Command::create_global_application_command(&ctx.http, |command| { + let command = command + .name("tag bulk_new") + .description("Add multiples tags"); + + for i in 0..MAX_ARGS_NUMBER { + command.create_option(|option| { + option + .name("new_tag_{i+1}") + .kind(CommandOptionType::String) + .required(i == 0) + }); + } + + command + }) + .await; + + let _ = Command::create_global_application_command(&ctx.http, |command| { + command + .name("tag delete") + .description("Delete a tag") + .create_option(|option| { + option + .name("tag") + .kind(CommandOptionType::String) + .required(true) + }) + }); + } + + async fn resume(&self, _: Context, _: ResumedEvent) { + println!("Successfully reconnected.") + } +}