impl auto guild_id parser, auto command registration && some fixes

This commit is contained in:
AmokDev 2025-03-16 09:20:32 +03:00
parent f09631b391
commit be7797bce1
4 changed files with 27 additions and 39 deletions

View file

@ -1,3 +1 @@
BOT_ID=123
BOT_TOKEN=YOUR-BOT-TOKEN
GUILD_ID=321

View file

@ -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()
}

View file

@ -1,7 +1,3 @@
use serenity::model::id::UserId;
pub struct Config {
pub bot_id: UserId,
pub bot_token: String,
pub guild_id: u64,
}

View file

@ -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,18 +16,24 @@ 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:?}");
}
}
}
@ -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");