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 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::<Value>().expect(CONFIG_ERR)
};
pub static ref REACTION_EMOTES: HashMap<String, EmojiId> = {
let mut m = HashMap::new();
let emotes = CONFIG.get("emotes").expect(CONFIG_ERR);
@ -30,7 +32,6 @@ lazy_static! {
}
m
};
static ref RESPONSES: HashMap<String, String> = {
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::<LastMessageInChannel>().expect("LastMessageInChannel not found");
let map = data
.get_mut::<LastMessageInChannel>()
.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::<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;
}
}
@ -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 {
if let Err(why) = msg
.react(
&ctx.http,
ReactionType::Custom {
animated: false,
id: *id,
name: Some(name.to_string()),
}).await {
},
)
.await
{
println!("Error reacting: {}", why);
}
}

View file

@ -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)
.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)