From be7797bce132113aef8b52576d8916bc3e87f31d Mon Sep 17 00:00:00 2001 From: AmokDev Date: Sun, 16 Mar 2025 09:20:32 +0300 Subject: [PATCH] impl auto guild_id parser, auto command registration && some fixes --- .env.example | 4 +--- src/commands/id.rs | 2 +- src/config.rs | 4 ---- src/main.rs | 56 +++++++++++++++++++++------------------------- 4 files changed, 27 insertions(+), 39 deletions(-) diff --git a/.env.example b/.env.example index af8caf4..043e271 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1 @@ -BOT_ID=123 -BOT_TOKEN=YOUR-BOT-TOKEN -GUILD_ID=321 \ No newline at end of file +BOT_TOKEN=YOUR-BOT-TOKEN \ No newline at end of file diff --git a/src/commands/id.rs b/src/commands/id.rs index db71f4f..a41f1f4 100644 --- a/src/commands/id.rs +++ b/src/commands/id.rs @@ -6,7 +6,7 @@ pub fn run(options: &[ResolvedOption]) -> String { value: ResolvedValue::User(user, _), .. }) = options.first() { - format!("{}'s id is {}", user.tag(), user.id) + format!("**{}**'s id is `{}`", user.tag(), user.id) } else { "Please provide a valid user".to_string() } diff --git a/src/config.rs b/src/config.rs index 1280156..9a0e567 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,3 @@ -use serenity::model::id::UserId; - pub struct Config { - pub bot_id: UserId, pub bot_token: String, - pub guild_id: u64, } diff --git a/src/main.rs b/src/main.rs index dd7eab2..dbf7f28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,6 @@ use serenity::model::application::Interaction; use serenity::model::gateway::Ready; use serenity::model::channel::Message; use serenity::model::user::OnlineStatus; -use serenity::model::id::GuildId; -use serenity::model::id::UserId; use serenity::gateway::ActivityData; use serenity::prelude::*; use tracing::{warn, error, info}; @@ -18,20 +16,26 @@ use tracing::{warn, error, info}; mod config; use config::Config; -struct Handler { - config: Config, -} +struct Handler; #[async_trait] impl EventHandler for Handler { async fn message(&self, ctx: Context, msg: Message) { - if msg.content == ":3" && msg.author.id != self.config.bot_id { + if msg.author.id == ctx.http.get_current_user().await.unwrap().id { + return; + } + + if msg.content.contains(":3") { if let Err(why) = msg.channel_id.say(&ctx.http, ":3").await { error!("Error sending message: {why:?}"); } + } else if msg.content.contains("ахуел") { + if let Err(why) = msg.channel_id.say(&ctx.http, "а я х*й не ел").await { + error!("Error sending message: {why:?}"); + } } - } + } async fn interaction_create(&self, ctx: Context, interaction: Interaction) { if let Interaction::Command(command) = interaction { @@ -53,17 +57,21 @@ impl EventHandler for Handler { } async fn ready(&self, ctx: Context, ready: Ready) { - info!("{} is connected!", ready.user.name); + info!("{} is started!", ready.user.name); - let guild_id = GuildId::new(self.config.guild_id); + let guilds = ready.guilds; - let _commands = guild_id.set_commands( - &ctx.http, - vec![ - commands::ping::register(), - commands::id::register(), - ] - ).await; + for guild_id in guilds { + guild_id.id.set_commands( + &ctx.http, + vec![ + commands::ping::register(), + commands::id::register(), + ] + ) + .await + .expect("failed to create application command"); + } ctx.set_presence( Some(ActivityData::playing("/help")), @@ -79,18 +87,6 @@ async fn main() { dotenv().ok(); - let bot_id = UserId::new( - env::var("BOT_ID") - .expect("BOT_ID не найден") - .parse() - .expect("Неверный формат BOT_ID") - ); - - let guild_id = env::var("GUILD_ID") - .expect("GUILD_ID не найден") - .parse() - .expect("Неверный формат GUILD_ID"); - let bot_token = env::var("BOT_TOKEN") .expect("BOT_TOKEN не найден") .parse() @@ -98,9 +94,7 @@ async fn main() { let config = Config { - bot_id: bot_id, bot_token: bot_token, - guild_id: guild_id, }; let intents = GatewayIntents::GUILD_MESSAGES @@ -109,7 +103,7 @@ async fn main() { let token = &config.bot_token; let mut client = Client::builder(token, intents) - .event_handler(Handler { config }) + .event_handler(Handler) .await .expect("Error creating client");