From 7f24e86abf5fb88d155c9fcf11300c48f2c7c006 Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 5 Sep 2022 15:41:59 +0200 Subject: [PATCH] prepare docker --- src/general.rs | 43 ++++++++++++++++++++++++++++++------------- src/main.rs | 24 ++++++++++++------------ 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/general.rs b/src/general.rs index 0e2e528..37ff596 100644 --- a/src/general.rs +++ b/src/general.rs @@ -4,7 +4,7 @@ use std::fs; use fancy_regex::Regex; use lazy_static::lazy_static; use serenity::client::Context; -use serenity::framework::standard::{macros::hook}; +use serenity::framework::standard::macros::hook; use serenity::model::channel::{Message, ReactionType}; use serenity::model::id::EmojiId; use toml::Value; @@ -15,10 +15,12 @@ pub static CONFIG_ERR: &'static str = "Invalid config file"; lazy_static! { pub static ref CONFIG: Value = { - let config = fs::read_to_string("config.toml").expect("Config file not found. Add 'config.toml' to this directory"); + let config_path = + std::env::var("CONFIG_PATH").unwrap_or_else(|_| "config.toml".to_string()); + let config = fs::read_to_string(config_path) + .expect("Config file not found. Add 'config.toml' to this directory"); config.parse::().expect(CONFIG_ERR) }; - pub static ref REACTION_EMOTES: HashMap = { let mut m = HashMap::new(); let emotes = CONFIG.get("emotes").expect(CONFIG_ERR); @@ -30,7 +32,6 @@ lazy_static! { } m }; - static ref RESPONSES: HashMap = { let mut m = HashMap::new(); @@ -48,18 +49,28 @@ lazy_static! { #[hook] pub async fn normal_message(ctx: &Context, msg: &Message) { let mut data = ctx.data.write().await; - let map = data.get_mut::().expect("LastMessageInChannel not found"); + let map = data + .get_mut::() + .expect("LastMessageInChannel not found"); map.insert(msg.channel_id.clone(), msg.content.clone()); lazy_static! { static ref TOM_REGEX: Regex = Regex::new(r"(?<=^|\D)(\d{6})(?=\D|$)").unwrap(); } - let is_nsfw = msg.channel_id.to_channel(&ctx.http).await.expect("may be nsfw lol").is_nsfw(); + let is_nsfw = msg + .channel_id + .to_channel(&ctx.http) + .await + .expect("may be nsfw lol") + .is_nsfw(); if let Some(m) = TOM_REGEX.find(&msg.content).unwrap() { if is_nsfw { - let number = m.as_str().parse::().expect("matched regex, so it is valid"); + let number = m + .as_str() + .parse::() + .expect("matched regex, so it is valid"); reply(&*format!("", number), &msg, &ctx).await; } } @@ -72,11 +83,17 @@ pub async fn normal_message(ctx: &Context, msg: &Message) { for (name, id) in REACTION_EMOTES.iter() { if msg.content.to_lowercase().contains(name) { - if let Err(why) = msg.react(&ctx.http, ReactionType::Custom { - animated: false, - id: *id, - name: Some(name.to_string()), - }).await { + if let Err(why) = msg + .react( + &ctx.http, + ReactionType::Custom { + animated: false, + id: *id, + name: Some(name.to_string()), + }, + ) + .await + { println!("Error reacting: {}", why); } } @@ -87,4 +104,4 @@ pub async fn reply(txt: &str, msg: &Message, ctx: &Context) { if let Err(why) = msg.channel_id.say(&ctx.http, txt).await { println!("Error sending message: {:?}", why); } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 0f22337..dba7f2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,17 @@ use std::collections::{HashMap, HashSet}; use std::fs; -use serenity::{async_trait, model::gateway::Ready, prelude::*}; use serenity::client::Context; use serenity::framework::StandardFramework; use serenity::http::Http; use serenity::model::id::{ChannelId, UserId}; +use serenity::{async_trait, model::gateway::Ready, prelude::*}; use crate::commands::{ADMIN_GROUP, GENERAL_GROUP, MEME_GROUP, MY_HELP}; use crate::general::normal_message; -mod general; mod commands; +mod general; pub struct LastMessageInChannel; @@ -28,11 +28,11 @@ impl EventHandler for Handler { } } - #[tokio::main] async fn main() { - let token = fs::read_to_string("bot_token") - .expect("Expected bot token in file 'bot_token'"); + let token_path = std::env::var("BOT_TOKEN_PATH").unwrap_or_else(|_| "bot_token".to_string()); + let token = fs::read_to_string(token_path).expect("Expected bot token in file 'bot_token'"); + let token = token.trim(); let http = Http::new_with_token(&token); @@ -50,13 +50,13 @@ async fn main() { }; let framework = StandardFramework::new() - .configure(|c| c - .with_whitespace(false) - .on_mention(Some(bot_id)) - .prefix("<:tom:811324632082415626> ") - .delimiter(" ") - .owners(owners) - ) + .configure(|c| { + c.with_whitespace(false) + .on_mention(Some(bot_id)) + .prefix("<:tom:811324632082415626> ") + .delimiter(" ") + .owners(owners) + }) .normal_message(normal_message) .help(&MY_HELP) .group(&GENERAL_GROUP)