feat: include ping and help

This commit is contained in:
Maxime Leriche 2024-01-22 21:33:46 +00:00
parent a54d14783a
commit 6f2b15493d
13 changed files with 106 additions and 7 deletions

View File

@ -12,6 +12,7 @@
"helixquar.randomeverything",
"shardulm94.trailing-spaces",
"tamasfe.even-better-toml",
"edwinkofler.vscode-hyperupcall-pack-markdown"
"edwinkofler.vscode-hyperupcall-pack-markdown",
"proxzima.sweetdracula"
]
}

12
Cargo.lock generated
View File

@ -570,6 +570,7 @@ dependencies = [
"lock_api",
"once_cell",
"parking_lot_core",
"serde",
]
[[package]]
@ -846,6 +847,15 @@ dependencies = [
"slab",
]
[[package]]
name = "fxhash"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
dependencies = [
"byteorder",
]
[[package]]
name = "generic-array"
version = "0.14.7"
@ -1858,8 +1868,10 @@ dependencies = [
"bitflags 2.4.1",
"bytes",
"command_attr",
"dashmap",
"flate2",
"futures",
"fxhash",
"levenshtein",
"mime_guess",
"parking_lot",

View File

@ -37,6 +37,7 @@ serenity = { version = "0.12", default-features = false, features = [
"model",
"framework",
"standard_framework",
"cache",
] }
tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] }

View File

@ -12,3 +12,6 @@ password = "postgres"
database = "postgres"
tls = false
tls_insecure = false
[image]
path = "/projects/base-image"

View File

@ -2,7 +2,7 @@ bot_name = "AGAU_DEV"
env = "test"
port = 5437
token = ""
prefix= "&"
prefix = "&"
[persistence]
host = "localhost"
@ -12,3 +12,6 @@ password = "1OLlRZo1tnNluvx"
database = "1pNkVsX3FgFeiQdga"
tls = false
tls_insecure = false
[image]
path = "/projects/base-image"

27
src/bot/cmd/help.rs Normal file
View File

@ -0,0 +1,27 @@
use serenity::client::Context;
use serenity::framework::standard::macros::help;
use serenity::framework::standard::{
help_commands, Args, CommandGroup, CommandResult, HelpOptions,
};
use serenity::model::prelude::{Message, UserId};
use std::collections::HashSet;
#[help]
#[individual_command_tip = "Hello! Pour plus d'information sur une commande, passer la en argument."]
#[command_not_found_text = "La commande `{}` n'a pas été trouver."]
#[max_levenshtein_distance(3)]
#[wrong_channel = "Strike"]
#[lacking_role = "Hide"]
#[lacking_permissions = "Hide"]
#[strikethrough_commands_tip_in_guild("")]
#[embed_success_colour = "#5F021F"]
async fn help(
context: &Context,
msg: &Message,
args: Args,
help_options: &'static HelpOptions,
groups: &[&'static CommandGroup],
owners: HashSet<UserId>,
) -> CommandResult {
let _ = help_commands::with_embeds(context, msg, args, help_options, groups, owners).await;
Ok(())
}

2
src/bot/cmd/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod help;
pub mod ping;

14
src/bot/cmd/ping.rs Normal file
View File

@ -0,0 +1,14 @@
use serenity::{
all::Message,
client::Context,
framework::standard::{macros::command, CommandResult},
};
#[command]
#[description = "Pong!"]
pub async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong from framework!").await {
println!("Error sending message: {:?}", why)
}
Ok(())
}

View File

@ -12,7 +12,7 @@ impl EventHandler for Handler {
if msg.author.bot {
return;
}
if msg.content == "!ping" {
if msg.content == "&ping" {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
println!("Error sending message: {:?}", why);
}

View File

@ -1,9 +1,20 @@
use super::cmd::{help::HELP, ping::PING_COMMAND};
use super::handler::Handler;
use crate::config::Config;
use serenity::{all::GatewayIntents, http::Http, Client};
use serenity::framework::standard::Configuration;
use serenity::{
all::GatewayIntents,
framework::{standard::macros::group, StandardFramework},
http::Http,
Client,
};
use std::collections::HashSet;
use tokio::task::spawn_blocking;
#[group]
#[commands(ping)]
struct General;
pub fn start_bot(config: Config) {
if config.token != "" {
spawn_blocking(move || {
@ -17,9 +28,11 @@ pub fn start_bot(config: Config) {
| GatewayIntents::MESSAGE_CONTENT
| GatewayIntents::GUILD_VOICE_STATES
| GatewayIntents::GUILDS
| GatewayIntents::GUILD_MEMBERS;
| GatewayIntents::GUILD_MEMBERS
| GatewayIntents::GUILD_PRESENCES
| GatewayIntents::GUILD_MESSAGE_REACTIONS;
let http = Http::new(&config.token);
let (_owners, _bot_id) = match http.get_current_application_info().await {
let (owners, _bot_id) = match http.get_current_application_info().await {
Ok(info) => {
let mut owners = HashSet::new();
match info.owner {
@ -34,8 +47,19 @@ pub fn start_bot(config: Config) {
}
Err(why) => panic!("Could not access application info: {:?}", why),
};
let framework = StandardFramework::new().help(&HELP).group(&GENERAL_GROUP);
framework.configure(
Configuration::new()
.with_whitespace(true)
.prefix(config.prefix)
.owners(owners),
);
let mut client = Client::builder(&config.token, intents)
.event_handler(Handler)
.framework(framework)
.await
.expect("Err creating client");
if let Err(why) = client.start().await {

View File

@ -1,2 +1,3 @@
pub mod cmd;
pub mod handler;
pub mod init;

View File

@ -16,6 +16,8 @@ const BOT_NAME: &str = "BOT_NAME";
const BOT_TOKEN: &str = "BOT_TOKEN";
const BOT_PREFIX: &str = "BOT_PREFIX";
const IMAGE_PATH: &str = "IMAGE_PATH";
const RUST_ENV: &str = "RUST_ENV";
const PORT: &str = "PORT";
@ -28,6 +30,12 @@ pub struct Config {
pub port: u16,
pub token: String,
pub prefix: String,
pub image: ImageConfig,
}
#[derive(Deserialize, Clone)]
pub struct ImageConfig {
pub path: String,
}
#[derive(Deserialize, Clone)]
@ -108,6 +116,9 @@ fn override_config_with_env_vars(config: Config) -> Config {
.ok()
.or(pers.tls_insecure),
},
image: ImageConfig {
path: env::var(IMAGE_PATH).unwrap_or(config.image.path),
},
}
}

View File

@ -6,7 +6,7 @@ use actix_web::{App, HttpServer};
use bot::init::start_bot;
use config::parse_local_config;
#[actix_web::main] // or #[tokio::main]
#[tokio::main] // or #[actix_web::main]
async fn main() -> std::io::Result<()> {
let config = parse_local_config();
let port = config.port;