Begin Discord client implementation

This commit is contained in:
Victor Mignot 2022-11-20 18:18:29 -05:00
parent 85a61bb463
commit b03625443a
No known key found for this signature in database
GPG key ID: FFE4EF056FB5E0D0
3 changed files with 102 additions and 0 deletions

2
src/discord.rs Normal file
View file

@ -0,0 +1,2 @@
mod client;
mod event_handler;

32
src/discord/client.rs Normal file
View file

@ -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<Client>,
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),
}
}
}

View file

@ -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.")
}
}