impl auto guild_id parser, auto command registration && some fixes
This commit is contained in:
parent
f09631b391
commit
be7797bce1
4 changed files with 27 additions and 39 deletions
|
|
@ -1,3 +1 @@
|
||||||
BOT_ID=123
|
BOT_TOKEN=YOUR-BOT-TOKEN
|
||||||
BOT_TOKEN=YOUR-BOT-TOKEN
|
|
||||||
GUILD_ID=321
|
|
||||||
|
|
@ -6,7 +6,7 @@ pub fn run(options: &[ResolvedOption]) -> String {
|
||||||
value: ResolvedValue::User(user, _), ..
|
value: ResolvedValue::User(user, _), ..
|
||||||
}) = options.first()
|
}) = options.first()
|
||||||
{
|
{
|
||||||
format!("{}'s id is {}", user.tag(), user.id)
|
format!("**{}**'s id is `{}`", user.tag(), user.id)
|
||||||
} else {
|
} else {
|
||||||
"Please provide a valid user".to_string()
|
"Please provide a valid user".to_string()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,3 @@
|
||||||
use serenity::model::id::UserId;
|
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub bot_id: UserId,
|
|
||||||
pub bot_token: String,
|
pub bot_token: String,
|
||||||
pub guild_id: u64,
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
56
src/main.rs
56
src/main.rs
|
|
@ -9,8 +9,6 @@ use serenity::model::application::Interaction;
|
||||||
use serenity::model::gateway::Ready;
|
use serenity::model::gateway::Ready;
|
||||||
use serenity::model::channel::Message;
|
use serenity::model::channel::Message;
|
||||||
use serenity::model::user::OnlineStatus;
|
use serenity::model::user::OnlineStatus;
|
||||||
use serenity::model::id::GuildId;
|
|
||||||
use serenity::model::id::UserId;
|
|
||||||
use serenity::gateway::ActivityData;
|
use serenity::gateway::ActivityData;
|
||||||
use serenity::prelude::*;
|
use serenity::prelude::*;
|
||||||
use tracing::{warn, error, info};
|
use tracing::{warn, error, info};
|
||||||
|
|
@ -18,20 +16,26 @@ use tracing::{warn, error, info};
|
||||||
mod config;
|
mod config;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
|
||||||
struct Handler {
|
struct Handler;
|
||||||
config: Config,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventHandler for Handler {
|
impl EventHandler for Handler {
|
||||||
|
|
||||||
async fn message(&self, ctx: Context, msg: Message) {
|
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 {
|
if let Err(why) = msg.channel_id.say(&ctx.http, ":3").await {
|
||||||
error!("Error sending message: {why:?}");
|
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) {
|
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||||
if let Interaction::Command(command) = interaction {
|
if let Interaction::Command(command) = interaction {
|
||||||
|
|
@ -53,17 +57,21 @@ impl EventHandler for Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
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(
|
for guild_id in guilds {
|
||||||
&ctx.http,
|
guild_id.id.set_commands(
|
||||||
vec![
|
&ctx.http,
|
||||||
commands::ping::register(),
|
vec![
|
||||||
commands::id::register(),
|
commands::ping::register(),
|
||||||
]
|
commands::id::register(),
|
||||||
).await;
|
]
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("failed to create application command");
|
||||||
|
}
|
||||||
|
|
||||||
ctx.set_presence(
|
ctx.set_presence(
|
||||||
Some(ActivityData::playing("/help")),
|
Some(ActivityData::playing("/help")),
|
||||||
|
|
@ -79,18 +87,6 @@ async fn main() {
|
||||||
|
|
||||||
dotenv().ok();
|
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")
|
let bot_token = env::var("BOT_TOKEN")
|
||||||
.expect("BOT_TOKEN не найден")
|
.expect("BOT_TOKEN не найден")
|
||||||
.parse()
|
.parse()
|
||||||
|
|
@ -98,9 +94,7 @@ async fn main() {
|
||||||
|
|
||||||
|
|
||||||
let config = Config {
|
let config = Config {
|
||||||
bot_id: bot_id,
|
|
||||||
bot_token: bot_token,
|
bot_token: bot_token,
|
||||||
guild_id: guild_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let intents = GatewayIntents::GUILD_MESSAGES
|
let intents = GatewayIntents::GUILD_MESSAGES
|
||||||
|
|
@ -109,7 +103,7 @@ async fn main() {
|
||||||
|
|
||||||
let token = &config.bot_token;
|
let token = &config.bot_token;
|
||||||
let mut client = Client::builder(token, intents)
|
let mut client = Client::builder(token, intents)
|
||||||
.event_handler(Handler { config })
|
.event_handler(Handler)
|
||||||
.await
|
.await
|
||||||
.expect("Error creating client");
|
.expect("Error creating client");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue