Compare commits
2 commits
5becb595db
...
a8bb9bc8fd
| Author | SHA1 | Date | |
|---|---|---|---|
| a8bb9bc8fd | |||
| ae8d56e3f8 |
4 changed files with 32 additions and 16 deletions
|
|
@ -1 +1,3 @@
|
||||||
BOT_TOKEN=YOUR-BOT-TOKEN
|
BOT_TOKEN=YOUR-BOT-TOKEN
|
||||||
|
MEMBER_ROLE_ID=123
|
||||||
|
WELCOME_CHANNEL_ID=321
|
||||||
|
|
@ -8,4 +8,6 @@ serenity = "0.12"
|
||||||
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
|
||||||
tracing = "0.1.23"
|
tracing = "0.1.23"
|
||||||
tracing-subscriber = "0.3"
|
tracing-subscriber = "0.3"
|
||||||
|
envy = "0.4"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
dotenv = "0.15"
|
dotenv = "0.15"
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,8 @@
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub bot_token: String,
|
pub bot_token: String,
|
||||||
|
pub member_role_id: u64,
|
||||||
|
pub welcome_channel_id: u64,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
37
src/main.rs
37
src/main.rs
|
|
@ -1,12 +1,12 @@
|
||||||
mod commands;
|
mod commands;
|
||||||
|
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use std::env;
|
|
||||||
|
|
||||||
use serenity::async_trait;
|
use serenity::async_trait;
|
||||||
use serenity::builder::{CreateInteractionResponse, CreateInteractionResponseMessage};
|
use serenity::builder::{CreateInteractionResponse, CreateInteractionResponseMessage};
|
||||||
use serenity::model::application::Interaction;
|
use serenity::model::application::Interaction;
|
||||||
use serenity::model::gateway::Ready;
|
use serenity::model::gateway::Ready;
|
||||||
|
use serenity::model::guild::Member;
|
||||||
|
use serenity::model::id::ChannelId;
|
||||||
use serenity::model::channel::Message;
|
use serenity::model::channel::Message;
|
||||||
use serenity::model::user::OnlineStatus;
|
use serenity::model::user::OnlineStatus;
|
||||||
use serenity::gateway::ActivityData;
|
use serenity::gateway::ActivityData;
|
||||||
|
|
@ -16,7 +16,9 @@ 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 {
|
||||||
|
|
@ -79,6 +81,16 @@ impl EventHandler for Handler {
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn guild_member_addition(&self, ctx: Context, new_member: Member) {
|
||||||
|
if let Err(why) = new_member.add_role(&ctx.http, self.config.member_role_id).await {
|
||||||
|
error!("Error adding role: {why:?}");
|
||||||
|
}
|
||||||
|
let channel = ChannelId::new(self.config.welcome_channel_id);
|
||||||
|
if let Err(why) = channel.say(ctx.http, format!("Hello, {}!", new_member.user.name)).await {
|
||||||
|
error!("Error sending message: {why:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
|
@ -87,23 +99,18 @@ async fn main() {
|
||||||
|
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
let bot_token = env::var("BOT_TOKEN")
|
let config = match envy::from_env::<Config>() {
|
||||||
.expect("BOT_TOKEN не найден")
|
Ok(config) => config,
|
||||||
.parse()
|
Err(error) => panic!("{:#?}", error)
|
||||||
.expect("Неверный формат BOT_TOKEN");
|
|
||||||
|
|
||||||
|
|
||||||
let config = Config {
|
|
||||||
bot_token: bot_token,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let intents = GatewayIntents::GUILD_MESSAGES
|
let intents = GatewayIntents::GUILD_MESSAGES
|
||||||
| GatewayIntents::DIRECT_MESSAGES
|
| GatewayIntents::DIRECT_MESSAGES
|
||||||
| GatewayIntents::MESSAGE_CONTENT;
|
| GatewayIntents::MESSAGE_CONTENT
|
||||||
|
| GatewayIntents::GUILD_MEMBERS;
|
||||||
|
|
||||||
let token = &config.bot_token;
|
let mut client = Client::builder(&config.bot_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