2020-08-26 17:26:28 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
|
2020-08-09 22:59:31 +00:00
|
|
|
mod commands;
|
2020-09-28 12:42:20 +00:00
|
|
|
mod consts;
|
2020-10-12 20:01:27 +00:00
|
|
|
mod framework;
|
|
|
|
mod models;
|
|
|
|
mod time_parser;
|
2020-08-06 14:22:13 +00:00
|
|
|
|
|
|
|
use serenity::{
|
2020-10-03 16:31:23 +00:00
|
|
|
cache::Cache,
|
2020-10-12 20:01:27 +00:00
|
|
|
client::{bridge::gateway::GatewayIntents, Client},
|
|
|
|
framework::Framework,
|
|
|
|
http::{client::Http, CacheHttp},
|
2020-10-03 16:31:23 +00:00
|
|
|
model::{
|
|
|
|
channel::Message,
|
2020-10-12 20:01:27 +00:00
|
|
|
id::{GuildId, UserId},
|
2020-09-15 13:43:49 +00:00
|
|
|
},
|
2020-08-06 14:22:13 +00:00
|
|
|
prelude::TypeMapKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
use sqlx::{
|
2020-10-12 20:01:27 +00:00
|
|
|
mysql::{MySqlConnection, MySqlPool},
|
2020-08-06 14:22:13 +00:00
|
|
|
Pool,
|
|
|
|
};
|
|
|
|
|
|
|
|
use dotenv::dotenv;
|
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
use std::{env, sync::Arc};
|
2020-08-06 14:22:13 +00:00
|
|
|
|
2020-09-28 12:42:20 +00:00
|
|
|
use crate::{
|
2020-10-12 20:01:27 +00:00
|
|
|
commands::{info_cmds, moderation_cmds, reminder_cmds, todo_cmds},
|
2020-10-13 13:35:13 +00:00
|
|
|
consts::{CNC_GUILD, DEFAULT_PREFIX, SUBSCRIPTION_ROLES, THEME_COLOR},
|
2020-09-28 12:42:20 +00:00
|
|
|
framework::RegexFramework,
|
2020-08-09 22:59:31 +00:00
|
|
|
};
|
2020-10-11 16:41:26 +00:00
|
|
|
|
|
|
|
use serenity::futures::TryFutureExt;
|
2020-08-06 14:22:13 +00:00
|
|
|
|
2020-10-17 22:56:19 +00:00
|
|
|
use log::info;
|
|
|
|
|
2020-08-06 14:22:13 +00:00
|
|
|
struct SQLPool;
|
|
|
|
|
|
|
|
impl TypeMapKey for SQLPool {
|
|
|
|
type Value = Pool<MySqlConnection>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ReqwestClient;
|
|
|
|
|
|
|
|
impl TypeMapKey for ReqwestClient {
|
|
|
|
type Value = Arc<reqwest::Client>;
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:29:19 +00:00
|
|
|
struct FrameworkCtx;
|
|
|
|
|
|
|
|
impl TypeMapKey for FrameworkCtx {
|
|
|
|
type Value = Arc<Box<dyn Framework + Send + Sync>>;
|
|
|
|
}
|
|
|
|
|
2020-08-06 14:22:13 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
2020-10-17 22:56:19 +00:00
|
|
|
env_logger::init();
|
|
|
|
|
2020-08-06 14:22:13 +00:00
|
|
|
dotenv()?;
|
|
|
|
|
2020-10-11 16:41:26 +00:00
|
|
|
let token = env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment");
|
|
|
|
|
|
|
|
let http = Http::new_with_token(&token);
|
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
let logged_in_id = http
|
|
|
|
.get_current_user()
|
|
|
|
.map_ok(|user| user.id.as_u64().to_owned())
|
|
|
|
.await?;
|
2020-10-12 17:37:14 +00:00
|
|
|
|
|
|
|
let framework = RegexFramework::new(logged_in_id)
|
2020-10-13 13:35:13 +00:00
|
|
|
.default_prefix(DEFAULT_PREFIX.clone())
|
2020-10-12 18:12:33 +00:00
|
|
|
.ignore_bots(env::var("IGNORE_BOTS").map_or(true, |var| var == "1"))
|
2020-10-17 14:21:00 +00:00
|
|
|
// info commands
|
2020-09-01 17:48:40 +00:00
|
|
|
.add_command("ping", &info_cmds::PING_COMMAND)
|
2020-08-09 22:59:31 +00:00
|
|
|
.add_command("help", &info_cmds::HELP_COMMAND)
|
|
|
|
.add_command("info", &info_cmds::INFO_COMMAND)
|
2020-10-11 00:42:19 +00:00
|
|
|
.add_command("invite", &info_cmds::INFO_COMMAND)
|
2020-08-09 22:59:31 +00:00
|
|
|
.add_command("donate", &info_cmds::DONATE_COMMAND)
|
2020-09-01 16:07:51 +00:00
|
|
|
.add_command("dashboard", &info_cmds::DASHBOARD_COMMAND)
|
2020-08-27 20:37:44 +00:00
|
|
|
.add_command("clock", &info_cmds::CLOCK_COMMAND)
|
2020-10-17 14:21:00 +00:00
|
|
|
// reminder commands
|
2020-09-08 22:08:02 +00:00
|
|
|
.add_command("timer", &reminder_cmds::TIMER_COMMAND)
|
2020-09-11 16:41:15 +00:00
|
|
|
.add_command("remind", &reminder_cmds::REMIND_COMMAND)
|
|
|
|
.add_command("r", &reminder_cmds::REMIND_COMMAND)
|
|
|
|
.add_command("interval", &reminder_cmds::INTERVAL_COMMAND)
|
|
|
|
.add_command("i", &reminder_cmds::INTERVAL_COMMAND)
|
2020-09-19 14:20:43 +00:00
|
|
|
.add_command("natural", &reminder_cmds::NATURAL_COMMAND)
|
|
|
|
.add_command("n", &reminder_cmds::NATURAL_COMMAND)
|
|
|
|
.add_command("", &reminder_cmds::NATURAL_COMMAND)
|
2020-10-17 14:21:00 +00:00
|
|
|
// management commands
|
2020-09-05 20:17:45 +00:00
|
|
|
.add_command("look", &reminder_cmds::LOOK_COMMAND)
|
2020-09-08 22:08:02 +00:00
|
|
|
.add_command("del", &reminder_cmds::DELETE_COMMAND)
|
2020-10-17 14:21:00 +00:00
|
|
|
// to-do commands
|
|
|
|
.add_command("todo", &todo_cmds::TODO_USER_COMMAND)
|
|
|
|
.add_command("todo user", &todo_cmds::TODO_USER_COMMAND)
|
|
|
|
.add_command("todoc", &todo_cmds::TODO_CHANNEL_COMMAND)
|
|
|
|
.add_command("todo channel", &todo_cmds::TODO_CHANNEL_COMMAND)
|
|
|
|
.add_command("todos", &todo_cmds::TODO_GUILD_COMMAND)
|
|
|
|
.add_command("todo server", &todo_cmds::TODO_GUILD_COMMAND)
|
|
|
|
.add_command("todo guild", &todo_cmds::TODO_GUILD_COMMAND)
|
|
|
|
// moderation commands
|
2020-08-18 19:09:21 +00:00
|
|
|
.add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND)
|
2020-09-02 16:13:17 +00:00
|
|
|
.add_command("restrict", &moderation_cmds::RESTRICT_COMMAND)
|
2020-08-27 20:37:44 +00:00
|
|
|
.add_command("timezone", &moderation_cmds::TIMEZONE_COMMAND)
|
2020-09-01 16:07:51 +00:00
|
|
|
.add_command("prefix", &moderation_cmds::PREFIX_COMMAND)
|
2020-09-01 14:34:50 +00:00
|
|
|
.add_command("lang", &moderation_cmds::LANGUAGE_COMMAND)
|
|
|
|
.add_command("pause", &reminder_cmds::PAUSE_COMMAND)
|
2020-09-01 17:18:45 +00:00
|
|
|
.add_command("offset", &reminder_cmds::OFFSET_COMMAND)
|
2020-09-01 17:37:43 +00:00
|
|
|
.add_command("nudge", &reminder_cmds::NUDGE_COMMAND)
|
2020-09-04 20:21:47 +00:00
|
|
|
.add_command("alias", &moderation_cmds::ALIAS_COMMAND)
|
2020-09-05 20:17:45 +00:00
|
|
|
.add_command("a", &moderation_cmds::ALIAS_COMMAND)
|
2020-08-06 14:22:13 +00:00
|
|
|
.build();
|
|
|
|
|
2020-09-03 23:29:19 +00:00
|
|
|
let framework_arc = Arc::new(Box::new(framework) as Box<dyn Framework + Send + Sync>);
|
|
|
|
|
2020-10-11 16:41:26 +00:00
|
|
|
let mut client = Client::new(&token)
|
2020-10-12 20:01:27 +00:00
|
|
|
.intents(
|
|
|
|
GatewayIntents::GUILD_MESSAGES
|
|
|
|
| GatewayIntents::GUILDS
|
|
|
|
| GatewayIntents::DIRECT_MESSAGES,
|
|
|
|
)
|
2020-09-03 23:29:19 +00:00
|
|
|
.framework_arc(framework_arc.clone())
|
2020-10-12 20:01:27 +00:00
|
|
|
.await
|
|
|
|
.expect("Error occurred creating client");
|
2020-08-06 14:22:13 +00:00
|
|
|
|
2020-08-07 15:45:19 +00:00
|
|
|
{
|
2020-10-12 20:01:27 +00:00
|
|
|
let pool = MySqlPool::new(
|
|
|
|
&env::var("DATABASE_URL").expect("Missing DATABASE_URL from environment"),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-08-07 15:45:19 +00:00
|
|
|
|
|
|
|
let mut data = client.data.write().await;
|
|
|
|
|
|
|
|
data.insert::<SQLPool>(pool);
|
|
|
|
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));
|
2020-09-03 23:29:19 +00:00
|
|
|
data.insert::<FrameworkCtx>(framework_arc);
|
2020-08-07 15:45:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 22:56:19 +00:00
|
|
|
if let Ok((Some(lower), Some(upper))) = env::var("SHARD_RANGE").map(|sr| {
|
|
|
|
let mut split = sr
|
|
|
|
.split(',')
|
|
|
|
.map(|val| val.parse::<u64>().expect("SHARD_RANGE not an integer"));
|
|
|
|
|
|
|
|
(split.next(), split.next())
|
|
|
|
}) {
|
|
|
|
let total_shards = env::var("SHARD_COUNT")
|
|
|
|
.map(|shard_count| shard_count.parse::<u64>().ok())
|
|
|
|
.ok()
|
|
|
|
.flatten()
|
|
|
|
.expect("No SHARD_COUNT provided, but SHARD_RANGE was provided");
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
lower < upper,
|
|
|
|
"SHARD_RANGE lower limit is not less than the upper limit"
|
|
|
|
);
|
|
|
|
|
|
|
|
info!(
|
|
|
|
"Starting client fragment with shards {}-{}/{}",
|
|
|
|
lower, upper, total_shards
|
|
|
|
);
|
|
|
|
|
|
|
|
client
|
|
|
|
.start_shard_range([lower, upper], total_shards)
|
|
|
|
.await?;
|
|
|
|
} else if let Ok(total_shards) = env::var("SHARD_COUNT").map(|shard_count| {
|
|
|
|
shard_count
|
|
|
|
.parse::<u64>()
|
|
|
|
.expect("SHARD_COUNT not an integer")
|
|
|
|
}) {
|
|
|
|
info!("Starting client with {} shards", total_shards);
|
|
|
|
|
|
|
|
client.start_shards(total_shards).await?;
|
|
|
|
} else {
|
|
|
|
info!("Starting client as autosharded");
|
|
|
|
|
|
|
|
client.start_autosharded().await?;
|
|
|
|
}
|
2020-08-06 14:22:13 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-15 13:43:49 +00:00
|
|
|
|
|
|
|
pub async fn check_subscription(cache_http: impl CacheHttp, user_id: impl Into<UserId>) -> bool {
|
2020-10-11 16:41:26 +00:00
|
|
|
if let Some(subscription_guild) = *CNC_GUILD {
|
2020-10-12 20:01:27 +00:00
|
|
|
let guild_member = GuildId(subscription_guild)
|
|
|
|
.member(cache_http, user_id)
|
|
|
|
.await;
|
2020-10-11 16:41:26 +00:00
|
|
|
|
|
|
|
if let Ok(member) = guild_member {
|
|
|
|
for role in member.roles {
|
|
|
|
if SUBSCRIPTION_ROLES.contains(role.as_u64()) {
|
2020-10-12 20:01:27 +00:00
|
|
|
return true;
|
2020-09-15 13:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2020-09-15 13:43:49 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 16:31:23 +00:00
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
pub async fn check_subscription_on_message(
|
|
|
|
cache_http: impl CacheHttp + AsRef<Cache>,
|
|
|
|
msg: &Message,
|
|
|
|
) -> bool {
|
|
|
|
check_subscription(&cache_http, &msg.author).await
|
|
|
|
|| if let Some(guild) = msg.guild(&cache_http).await {
|
|
|
|
check_subscription(&cache_http, guild.owner_id).await
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2020-10-03 16:31:23 +00:00
|
|
|
}
|