From f0a95356d46b45b61ed542f2cb0e3455bfba49c0 Mon Sep 17 00:00:00 2001 From: Oupson Date: Mon, 30 Nov 2020 15:59:08 +0100 Subject: [PATCH] Working on the bot --- src/api.rs | 9 +++++---- src/commands/admin.rs | 6 +++--- src/commands/general.rs | 21 +++----------------- src/commands/music.rs | 1 + src/commands/roulette.rs | 41 +++++++++------------------------------- 5 files changed, 21 insertions(+), 57 deletions(-) diff --git a/src/api.rs b/src/api.rs index 3145c17..4748b52 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,20 +1,21 @@ use crate::commands::Result; -use serenity::{model::channel::Message, prelude::Context}; +use serenity::{framework::standard::CommandResult, model::channel::Message, prelude::Context}; /// Send a reply to the channel the message was received on. pub(crate) async fn send_reply<'m, S: std::string::ToString>( ctx: &Context, msg: &Message, message: S, -) -> Result { - Ok(msg.channel_id.say(ctx, message.to_string()).await?) +) -> CommandResult { + msg.channel_id.say(ctx, message.to_string()).await?; + Ok(()) } pub(crate) async fn send_splitted_by_lines_in_card( ctx: &Context, msg: &Message, message: S, -) -> Result<()> { +) -> CommandResult { let mut buffer = String::from("\x60\x60\x60\n"); for line in message.to_string().lines() { if buffer.len() + 4 + line.len() >= 2000 { diff --git a/src/commands/admin.rs b/src/commands/admin.rs index dd4610b..4ae3429 100644 --- a/src/commands/admin.rs +++ b/src/commands/admin.rs @@ -1,4 +1,3 @@ -use crate::debugln; use serenity::{ framework::standard::{ macros::{command, group}, @@ -7,6 +6,7 @@ use serenity::{ model::prelude::*, prelude::*, }; +use log::debug; #[group] #[commands(ban, kick)] @@ -21,7 +21,7 @@ async fn kick(ctx: &Context, msg: &Message) -> CommandResult { if let Ok(sender_member) = msg.member(ctx).await { for user in &msg.mentions { if let Some(member) = ctx.cache.member(msg.guild_id.unwrap(), user.id).await { - debugln!("Kicking {:?}", user); + debug!("Kicking {:?}", user); let kick = if let Some(role_member) = sender_member.highest_role_info(ctx).await { if let Some(role) = member.highest_role_info(ctx).await { role_member.1 > role.1 @@ -63,7 +63,7 @@ async fn ban(ctx: &Context, msg: &Message) -> CommandResult { if let Ok(sender_member) = msg.member(ctx).await { for user in &msg.mentions { if let Some(member) = ctx.cache.member(msg.guild_id.unwrap(), user.id).await { - debugln!("Kicking {:?}", user); + debug!("Kicking {:?}", user); let kick = if let Some(role_member) = sender_member.highest_role_info(ctx).await { if let Some(role) = member.highest_role_info(ctx).await { role_member.1 > role.1 diff --git a/src/commands/general.rs b/src/commands/general.rs index a8a2f68..7e19f6a 100644 --- a/src/commands/general.rs +++ b/src/commands/general.rs @@ -1,4 +1,4 @@ -use crate::{api, data::ShardManagerContainer, debugln}; +use crate::{api, data::ShardManagerContainer}; use futures::StreamExt; use log::error; use serenity::{ @@ -63,15 +63,7 @@ async fn latency(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[description = "Split a huge code"] #[bucket = "longcode"] -pub async fn longcode(ctx: &Context, msg: &Message, args: Args) -> CommandResult { - if let Err(e) = _longcode(ctx, msg, args).await { - error!("Error in longcode : {:?}", e); - } - Ok(()) -} - -async fn _longcode(ctx: &Context, msg: &Message, mut args: Args) -> crate::commands::Result<()> { - debugln!("_longcode : {:?}", args); +pub async fn longcode(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { if let Ok(language) = args.single::() { if !msg.attachments.is_empty() { let att = msg.attachments[0].clone(); @@ -177,14 +169,7 @@ async fn invite(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[description = "Find who is the older"] -pub async fn older(ctx: &Context, msg: &Message, args: Args) -> CommandResult { - if let Err(e) = _older(ctx, msg, args).await { - error!("Error in older : {:?}", e); - } - Ok(()) -} - -async fn _older(ctx: &Context, msg: &Message, mut args: Args) -> crate::commands::Result<()> { +pub async fn older(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { let mut number = 10; if !args.is_empty() { number = args.single::()?; diff --git a/src/commands/music.rs b/src/commands/music.rs index e20e498..843ed0c 100644 --- a/src/commands/music.rs +++ b/src/commands/music.rs @@ -291,6 +291,7 @@ async fn is_mute( member: &PartialMember, guild_id: GuildId, ) -> tokio::io::Result { + let mut data = ctx.data.write().await; let data = data .get_mut::() diff --git a/src/commands/roulette.rs b/src/commands/roulette.rs index e7351fd..b91b473 100644 --- a/src/commands/roulette.rs +++ b/src/commands/roulette.rs @@ -1,9 +1,8 @@ use crate::{ api, commands, data::{BulletsContainer, GuildOptions, GuildOptionsKey}, - debugln, }; -use log::error; +use log::{debug, error, trace}; use rand::Rng; use serenity::{ framework::standard::{ @@ -26,8 +25,10 @@ struct Roulette; async fn shot(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let _message = args.message().trim_end(); if _message == "shot" || _message == "" { - if let Err(e) = _shot(ctx, msg).await { - error!("Error in shot : {:?}", e); + if rand::thread_rng().gen_range(0, 6) == 0 { + api::send_reply(ctx, &msg, "💥").await?; + } else { + api::send_reply(&ctx, &msg, format!("Click ! Reloading")).await?; } } else if let Err(e) = api::send_reply( ctx, @@ -41,30 +42,6 @@ async fn shot(ctx: &Context, msg: &Message, args: Args) -> CommandResult { Ok(()) } -async fn _shot(ctx: &Context, msg: &Message) -> commands::Result<()> { - let mut data = ctx.data.write().await; - let bullets_map = data - .get_mut::() - .expect("Expected CommandCounter in TypeMap."); - let bullets = bullets_map - .entry(msg.author.id.0) - .or_insert((5, rand::thread_rng().gen_range(0, 6))); - if bullets.0 == bullets.1 { - api::send_reply(ctx, &msg, "💥").await?; - *bullets = (5, rand::thread_rng().gen_range(0, 6)); - } else { - *bullets = (bullets.0 - 1, bullets.1); - api::send_reply( - &ctx, - &msg, - format!("Click ! bullets remaining : {}", bullets.0 + 1), - ) - .await?; - } - log::trace!("Bullets Map : {:?}", bullets_map); - Ok(()) -} - #[command] #[description = "Reload"] #[bucket = "roulette"] @@ -97,7 +74,7 @@ async fn check(ctx: &Context, msg: &Message) -> CommandResult { .entry(msg.author.id.0) .or_insert((5, rand::thread_rng().gen_range(0, 6))); msg.channel_id.say(ctx, format!("Because you are a little shit, you open your barrel and you see that the bullet was at the {} position", bullet_to_str(bullets.1 + 1))).await?; - debugln!("Bullets Map : {:?}", bullets_map); + debug!("Bullets Map : {:?}", bullets_map); Ok(()) } @@ -129,7 +106,7 @@ async fn kick(ctx: &Context, msg: &Message) -> CommandResult { Ok(()) } -async fn _kick(ctx: &Context, msg: &Message) -> commands::Result<()> { +async fn _kick(ctx: &Context, msg: &Message) -> CommandResult { if let Some(guild_id) = &msg.guild_id { let mut data = ctx.data.write().await; let guilds_options = data @@ -169,7 +146,7 @@ async fn _kick(ctx: &Context, msg: &Message) -> commands::Result<()> { ) .await?; } - debugln!("Bullets Map : {:?}", bullets_map); + debug!("Bullets Map : {:?}", bullets_map); } } Ok(()) @@ -203,6 +180,6 @@ async fn disable_kick(ctx: &Context, msg: &Message, mut args: Args) -> CommandRe entry.save_async(guild_id.0).await?; } - log::debug!("{:?}", guilds_options); + debug!("{:?}", guilds_options); Ok(()) }