prepare docker

This commit is contained in:
nora 2022-09-05 15:41:59 +02:00
parent 6fac0013cb
commit 7f24e86abf
2 changed files with 42 additions and 25 deletions

View file

@ -4,7 +4,7 @@ use std::fs;
use fancy_regex::Regex; use fancy_regex::Regex;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serenity::client::Context; 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::channel::{Message, ReactionType};
use serenity::model::id::EmojiId; use serenity::model::id::EmojiId;
use toml::Value; use toml::Value;
@ -15,10 +15,12 @@ pub static CONFIG_ERR: &'static str = "Invalid config file";
lazy_static! { lazy_static! {
pub static ref CONFIG: Value = { 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::<Value>().expect(CONFIG_ERR) config.parse::<Value>().expect(CONFIG_ERR)
}; };
pub static ref REACTION_EMOTES: HashMap<String, EmojiId> = { pub static ref REACTION_EMOTES: HashMap<String, EmojiId> = {
let mut m = HashMap::new(); let mut m = HashMap::new();
let emotes = CONFIG.get("emotes").expect(CONFIG_ERR); let emotes = CONFIG.get("emotes").expect(CONFIG_ERR);
@ -30,7 +32,6 @@ lazy_static! {
} }
m m
}; };
static ref RESPONSES: HashMap<String, String> = { static ref RESPONSES: HashMap<String, String> = {
let mut m = HashMap::new(); let mut m = HashMap::new();
@ -48,18 +49,28 @@ lazy_static! {
#[hook] #[hook]
pub async fn normal_message(ctx: &Context, msg: &Message) { pub async fn normal_message(ctx: &Context, msg: &Message) {
let mut data = ctx.data.write().await; let mut data = ctx.data.write().await;
let map = data.get_mut::<LastMessageInChannel>().expect("LastMessageInChannel not found"); let map = data
.get_mut::<LastMessageInChannel>()
.expect("LastMessageInChannel not found");
map.insert(msg.channel_id.clone(), msg.content.clone()); map.insert(msg.channel_id.clone(), msg.content.clone());
lazy_static! { lazy_static! {
static ref TOM_REGEX: Regex = Regex::new(r"(?<=^|\D)(\d{6})(?=\D|$)").unwrap(); 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 let Some(m) = TOM_REGEX.find(&msg.content).unwrap() {
if is_nsfw { if is_nsfw {
let number = m.as_str().parse::<u32>().expect("matched regex, so it is valid"); let number = m
.as_str()
.parse::<u32>()
.expect("matched regex, so it is valid");
reply(&*format!("<https://nhentai.net/g/{}/>", number), &msg, &ctx).await; reply(&*format!("<https://nhentai.net/g/{}/>", number), &msg, &ctx).await;
} }
} }
@ -72,11 +83,17 @@ pub async fn normal_message(ctx: &Context, msg: &Message) {
for (name, id) in REACTION_EMOTES.iter() { for (name, id) in REACTION_EMOTES.iter() {
if msg.content.to_lowercase().contains(name) { if msg.content.to_lowercase().contains(name) {
if let Err(why) = msg.react(&ctx.http, ReactionType::Custom { if let Err(why) = msg
animated: false, .react(
id: *id, &ctx.http,
name: Some(name.to_string()), ReactionType::Custom {
}).await { animated: false,
id: *id,
name: Some(name.to_string()),
},
)
.await
{
println!("Error reacting: {}", why); 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 { if let Err(why) = msg.channel_id.say(&ctx.http, txt).await {
println!("Error sending message: {:?}", why); println!("Error sending message: {:?}", why);
} }
} }

View file

@ -1,17 +1,17 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fs; use std::fs;
use serenity::{async_trait, model::gateway::Ready, prelude::*};
use serenity::client::Context; use serenity::client::Context;
use serenity::framework::StandardFramework; use serenity::framework::StandardFramework;
use serenity::http::Http; use serenity::http::Http;
use serenity::model::id::{ChannelId, UserId}; 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::commands::{ADMIN_GROUP, GENERAL_GROUP, MEME_GROUP, MY_HELP};
use crate::general::normal_message; use crate::general::normal_message;
mod general;
mod commands; mod commands;
mod general;
pub struct LastMessageInChannel; pub struct LastMessageInChannel;
@ -28,11 +28,11 @@ impl EventHandler for Handler {
} }
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let token = fs::read_to_string("bot_token") let token_path = std::env::var("BOT_TOKEN_PATH").unwrap_or_else(|_| "bot_token".to_string());
.expect("Expected bot token in file 'bot_token'"); 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); let http = Http::new_with_token(&token);
@ -50,13 +50,13 @@ async fn main() {
}; };
let framework = StandardFramework::new() let framework = StandardFramework::new()
.configure(|c| c .configure(|c| {
.with_whitespace(false) c.with_whitespace(false)
.on_mention(Some(bot_id)) .on_mention(Some(bot_id))
.prefix("<:tom:811324632082415626> ") .prefix("<:tom:811324632082415626> ")
.delimiter(" ") .delimiter(" ")
.owners(owners) .owners(owners)
) })
.normal_message(normal_message) .normal_message(normal_message)
.help(&MY_HELP) .help(&MY_HELP)
.group(&GENERAL_GROUP) .group(&GENERAL_GROUP)