init
This commit is contained in:
parent
4d4474af42
commit
f09631b391
8 changed files with 175 additions and 0 deletions
3
.env.example
Normal file
3
.env.example
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
BOT_ID=123
|
||||
BOT_TOKEN=YOUR-BOT-TOKEN
|
||||
GUILD_ID=321
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1,3 +1,6 @@
|
|||
# custom
|
||||
.env
|
||||
|
||||
# ---> Rust
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
|
|
|
|||
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "discordbot-rs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
serenity = "0.12"
|
||||
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
|
||||
tracing = "0.1.23"
|
||||
tracing-subscriber = "0.3"
|
||||
dotenv = "0.15"
|
||||
20
src/commands/id.rs
Normal file
20
src/commands/id.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use serenity::builder::{CreateCommand, CreateCommandOption};
|
||||
use serenity::model::application::{CommandOptionType, ResolvedOption, ResolvedValue};
|
||||
|
||||
pub fn run(options: &[ResolvedOption]) -> String {
|
||||
if let Some(ResolvedOption {
|
||||
value: ResolvedValue::User(user, _), ..
|
||||
}) = options.first()
|
||||
{
|
||||
format!("{}'s id is {}", user.tag(), user.id)
|
||||
} else {
|
||||
"Please provide a valid user".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register() -> CreateCommand {
|
||||
CreateCommand::new("id").description("Get a user id").add_option(
|
||||
CreateCommandOption::new(CommandOptionType::User, "id", "The user to lookup")
|
||||
.required(true),
|
||||
)
|
||||
}
|
||||
2
src/commands/mod.rs
Normal file
2
src/commands/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod ping;
|
||||
pub mod id;
|
||||
10
src/commands/ping.rs
Normal file
10
src/commands/ping.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use serenity::builder::CreateCommand;
|
||||
use serenity::model::application::ResolvedOption;
|
||||
|
||||
pub fn run(_options: &[ResolvedOption]) -> String {
|
||||
"Pong!".to_string()
|
||||
}
|
||||
|
||||
pub fn register() -> CreateCommand {
|
||||
CreateCommand::new("ping").description("A ping command")
|
||||
}
|
||||
7
src/config.rs
Normal file
7
src/config.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
use serenity::model::id::UserId;
|
||||
|
||||
pub struct Config {
|
||||
pub bot_id: UserId,
|
||||
pub bot_token: String,
|
||||
pub guild_id: u64,
|
||||
}
|
||||
119
src/main.rs
Normal file
119
src/main.rs
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
mod commands;
|
||||
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
|
||||
use serenity::async_trait;
|
||||
use serenity::builder::{CreateInteractionResponse, CreateInteractionResponseMessage};
|
||||
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};
|
||||
|
||||
mod config;
|
||||
use config::Config;
|
||||
|
||||
struct Handler {
|
||||
config: Config,
|
||||
}
|
||||
|
||||
#[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 let Err(why) = msg.channel_id.say(&ctx.http, ":3").await {
|
||||
error!("Error sending message: {why:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
if let Interaction::Command(command) = interaction {
|
||||
|
||||
let content = match command.data.name.as_str() {
|
||||
"ping" => Some(commands::ping::run(&command.data.options())),
|
||||
"id" => Some(commands::id::run(&command.data.options())),
|
||||
_ => Some(":no_entry:".to_string()),
|
||||
};
|
||||
|
||||
if let Some(content) = content {
|
||||
let data = CreateInteractionResponseMessage::new().content(content);
|
||||
let builder = CreateInteractionResponse::Message(data);
|
||||
if let Err(why) = command.create_response(&ctx.http, builder).await {
|
||||
warn!("Cannot respond to slash command: {why}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
info!("{} is connected!", ready.user.name);
|
||||
|
||||
let guild_id = GuildId::new(self.config.guild_id);
|
||||
|
||||
let _commands = guild_id.set_commands(
|
||||
&ctx.http,
|
||||
vec![
|
||||
commands::ping::register(),
|
||||
commands::id::register(),
|
||||
]
|
||||
).await;
|
||||
|
||||
ctx.set_presence(
|
||||
Some(ActivityData::playing("/help")),
|
||||
OnlineStatus::DoNotDisturb,
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
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()
|
||||
.expect("Неверный формат BOT_TOKEN");
|
||||
|
||||
|
||||
let config = Config {
|
||||
bot_id: bot_id,
|
||||
bot_token: bot_token,
|
||||
guild_id: guild_id,
|
||||
};
|
||||
|
||||
let intents = GatewayIntents::GUILD_MESSAGES
|
||||
| GatewayIntents::DIRECT_MESSAGES
|
||||
| GatewayIntents::MESSAGE_CONTENT;
|
||||
|
||||
let token = &config.bot_token;
|
||||
let mut client = Client::builder(token, intents)
|
||||
.event_handler(Handler { config })
|
||||
.await
|
||||
.expect("Error creating client");
|
||||
|
||||
if let Err(why) = client.start().await {
|
||||
error!("Client error: {why:?}");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue