26 Commits

Author SHA1 Message Date
1007b75069 downgrade some stuff 2022-05-13 14:38:36 +01:00
0e7a4d02de ? 2022-05-13 14:21:34 +01:00
37e5c50800 added message content intent 2022-05-13 14:11:39 +01:00
88255032de update for new database models 2022-05-13 10:03:45 +01:00
b88d046846 block mentions in todo command 2021-11-15 07:55:37 +00:00
f9c110ffb7 removed unreleased code. fixed offset command 2021-11-07 23:36:07 +00:00
85a8ae625d interactions to delete a newly created reminder 2021-07-17 22:53:00 +01:00
43bbcb3fe0 cleared up clippy lints 2021-07-17 17:00:47 +01:00
1556318d07 turned models into module 2021-07-16 21:28:51 +01:00
ea2b0f4b0a removed useless thing 2021-07-16 18:23:43 +01:00
f02c04b313 use discord timestamp formatting 2021-07-16 18:22:39 +01:00
320060b1bd functions for signing stuff 2021-07-16 18:18:35 +01:00
bef33c6dac show user who created reminder 2021-07-10 18:24:43 +01:00
7bcb3c4a70 fixed oversight where users could set DM reminders across servers 2021-07-10 13:25:12 +01:00
2e153cffab removed some stuff from perm checks that is no longer needed 2021-06-27 17:15:50 +01:00
540f120d7d lang command uses interaction buttons 2021-06-27 16:55:59 +01:00
59ffb505dc timezone command uses interaction buttons 2021-06-27 16:31:54 +01:00
2bec2b9e12 added an on delete clause 2021-06-23 14:04:46 +01:00
507075d9d4 fixed the migration script since mysql is stupid 2021-06-22 16:46:42 +01:00
85659f05aa migrated reminder commands to new database code 2021-06-03 17:43:28 +01:00
eb07ece779 added migration file 2021-06-03 16:42:22 +01:00
1a09f026c9 made ratelimiting only apply when a command is actually done 2021-05-25 22:43:35 +01:00
b31843c478 check time to kill users that get stuck in executing state 2021-05-23 10:59:06 +01:00
9109250fe8 fix for alias 2021-05-13 20:20:53 +01:00
2346c2e978 v bump 2021-05-13 18:50:42 +01:00
a0da4dcf00 ratelimit commands 2021-05-13 18:50:22 +01:00
20 changed files with 2048 additions and 2371 deletions

1286
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "reminder_rs"
version = "1.5.0"
version = "1.5.2"
authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018"
@ -22,8 +22,7 @@ serde_json = "1.0"
rand = "0.7"
Inflector = "0.11"
levenshtein = "1.0"
# serenity = { version = "0.10", features = ["collector"] }
serenity = { git = "https://github.com/serenity-rs/serenity", branch = "next", features = ["collector", "unstable_discord_api"] }
serenity = { git = "https://github.com/jellywx/serenity", branch = "jellywx-attachment_option", features = ["collector", "unstable_discord_api"] }
sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono"]}
[dependencies.regex_command_attr]

View File

@ -1,3 +1,5 @@
CREATE DATABASE IF NOT EXISTS reminders;
SET FOREIGN_KEY_CHECKS=0;
USE reminders;

View File

@ -48,15 +48,16 @@ CREATE TABLE reminders_new (
PRIMARY KEY (id),
FOREIGN KEY (`channel_id`) REFERENCES channels (`id`) ON DELETE CASCADE,
FOREIGN KEY (`set_by`) REFERENCES users (`id`) ON DELETE SET NULL,
FOREIGN KEY (`set_by`) REFERENCES users (`id`) ON DELETE SET NULL
# disallow having a reminder as restartable if it has no interval
CONSTRAINT restartable_interval_mutex CHECK (`restartable` = 0 OR `interval` IS NULL),
-- , CONSTRAINT restartable_interval_mutex CHECK (`restartable` = 0 OR `interval` IS NULL)
# disallow disabling if interval is unspecified
CONSTRAINT interval_enabled_mutin CHECK (`enabled` = 1 OR `interval` IS NULL),
-- , CONSTRAINT interval_enabled_mutin CHECK (`enabled` = 1 OR `interval` IS NULL)
# disallow an expiry time if interval is unspecified
CONSTRAINT interval_expires_mutin CHECK (`expires` IS NULL OR `interval` IS NOT NULL)
);
-- , CONSTRAINT interval_expires_mutin CHECK (`expires` IS NULL OR `interval` IS NOT NULL)
)
COLLATE utf8mb4_unicode_ci;
# import data from other tables
INSERT INTO reminders_new (
@ -86,7 +87,7 @@ INSERT INTO reminders_new (
reminders.uid,
reminders.name,
reminders.channel_id,
FROM_UNIXTIME(reminders.time),
DATE_ADD(FROM_UNIXTIME(0), INTERVAL reminders.`time` SECOND),
reminders.`interval`,
reminders.enabled,
reminders.expires,
@ -120,7 +121,7 @@ CREATE TABLE embed_fields_new (
PRIMARY KEY (id),
FOREIGN KEY (reminder_id) REFERENCES reminders_new (id)
FOREIGN KEY (reminder_id) REFERENCES reminders_new (id) ON DELETE CASCADE
);
INSERT INTO embed_fields_new (

View File

@ -1,24 +1,22 @@
use regex_command_attr::command;
use serenity::{
builder::CreateEmbedFooter,
client::Context,
model::{
channel::Message,
interactions::{Interaction, InteractionResponseType},
},
};
use serenity::{builder::CreateEmbedFooter, client::Context, model::channel::Message};
use chrono::offset::Utc;
use crate::{
command_help, consts::DEFAULT_PREFIX, get_ctx_data, language_manager::LanguageManager,
models::CtxGuildData, models::UserData, FrameworkCtx, THEME_COLOR,
command_help,
consts::DEFAULT_PREFIX,
get_ctx_data,
language_manager::LanguageManager,
models::{user_data::UserData, CtxGuildData},
FrameworkCtx, THEME_COLOR,
};
use inflector::Inflector;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};
#[command]
#[can_blacklist(false)]
@ -37,7 +35,7 @@ async fn ping(ctx: &Context, msg: &Message, _args: String) {
}
async fn footer(ctx: &Context) -> impl FnOnce(&mut CreateEmbedFooter) -> &mut CreateEmbedFooter {
let shard_count = ctx.cache.shard_count().await;
let shard_count = ctx.cache.shard_count();
let shard = ctx.shard_id;
move |f| {
@ -136,137 +134,6 @@ async fn help(ctx: &Context, msg: &Message, args: String) {
}
}
pub async fn help_interaction(ctx: &Context, interaction: Interaction) {
async fn default_help(
ctx: &Context,
interaction: Interaction,
lm: Arc<LanguageManager>,
language: &str,
) {
let desc = lm.get(language, "help/desc").replace("{prefix}", "/");
let footer = footer(ctx).await;
interaction
.create_interaction_response(ctx, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| {
data.embed(move |e| {
e.title("Help Menu")
.description(desc)
.field(
lm.get(language, "help/setup_title"),
"`lang` `timezone` `meridian`",
true,
)
.field(
lm.get(language, "help/mod_title"),
"`prefix` `blacklist` `restrict` `alias`",
true,
)
.field(
lm.get(language, "help/reminder_title"),
"`remind` `interval` `natural` `look` `countdown`",
true,
)
.field(
lm.get(language, "help/reminder_mod_title"),
"`del` `offset` `pause` `nudge`",
true,
)
.field(
lm.get(language, "help/info_title"),
"`help` `info` `donate` `clock`",
true,
)
.field(
lm.get(language, "help/todo_title"),
"`todo` `todos` `todoc`",
true,
)
.field(lm.get(language, "help/other_title"), "`timer`", true)
.footer(footer)
.color(*THEME_COLOR)
})
})
})
.await
.unwrap();
}
async fn command_help(
ctx: &Context,
interaction: Interaction,
lm: Arc<LanguageManager>,
language: &str,
command_name: &str,
) {
interaction
.create_interaction_response(ctx, |r| {
r.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| {
data.embed(move |e| {
e.title(format!("{} Help", command_name.to_title_case()))
.description(
lm.get(&language, &format!("help/{}", command_name))
.replace("{prefix}", "/"),
)
.footer(|f| {
f.text(concat!(
env!("CARGO_PKG_NAME"),
" ver ",
env!("CARGO_PKG_VERSION")
))
})
.color(*THEME_COLOR)
})
})
})
.await
.unwrap();
}
let (pool, lm) = get_ctx_data(&ctx).await;
let language = UserData::language_of(interaction.member.user.id, &pool);
if let Some(data) = &interaction.data {
if let Some(command_name) = data
.options
.first()
.map(|opt| {
opt.value
.clone()
.map(|inner| inner.as_str().unwrap().to_string())
})
.flatten()
{
let framework = ctx
.data
.read()
.await
.get::<FrameworkCtx>()
.cloned()
.expect("Could not get FrameworkCtx from data");
let matched = framework
.commands
.get(&command_name)
.map(|inner| inner.name);
if let Some(command_name) = matched {
command_help(ctx, interaction, lm, &language.await, command_name).await
} else {
default_help(ctx, interaction, lm, &language.await).await;
}
} else {
default_help(ctx, interaction, lm, &language.await).await;
}
} else {
default_help(ctx, interaction, lm, &language.await).await;
}
}
#[command]
async fn info(ctx: &Context, msg: &Message, _args: String) {
let (pool, lm) = get_ctx_data(&ctx).await;
@ -278,7 +145,7 @@ async fn info(ctx: &Context, msg: &Message, _args: String) {
let desc = lm
.get(&language.await, "info")
.replacen("{user}", &current_user.await.name, 1)
.replacen("{user}", &current_user.name, 1)
.replace("{default_prefix}", &*DEFAULT_PREFIX)
.replace("{prefix}", &prefix.await);
@ -295,36 +162,6 @@ async fn info(ctx: &Context, msg: &Message, _args: String) {
.await;
}
pub async fn info_interaction(ctx: &Context, interaction: Interaction) {
let (pool, lm) = get_ctx_data(&ctx).await;
let language = UserData::language_of(&interaction.member, &pool);
let current_user = ctx.cache.current_user();
let footer = footer(ctx).await;
let desc = lm
.get(&language.await, "info")
.replacen("{user}", &current_user.await.name, 1)
.replace("{default_prefix}", &*DEFAULT_PREFIX)
.replace("{prefix}", "/");
interaction
.create_interaction_response(ctx, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| {
data.embed(move |e| {
e.title("Info")
.description(desc)
.footer(footer)
.color(*THEME_COLOR)
})
})
})
.await
.unwrap();
}
#[command]
async fn donate(ctx: &Context, msg: &Message, _args: String) {
let (pool, lm) = get_ctx_data(&ctx).await;
@ -346,30 +183,6 @@ async fn donate(ctx: &Context, msg: &Message, _args: String) {
.await;
}
pub async fn donate_interaction(ctx: &Context, interaction: Interaction) {
let (pool, lm) = get_ctx_data(&ctx).await;
let language = UserData::language_of(&interaction.member, &pool).await;
let desc = lm.get(&language, "donate");
let footer = footer(ctx).await;
interaction
.create_interaction_response(ctx, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| {
data.embed(move |e| {
e.title("Donate")
.description(desc)
.footer(footer)
.color(*THEME_COLOR)
})
})
})
.await
.unwrap();
}
#[command]
async fn dashboard(ctx: &Context, msg: &Message, _args: String) {
let footer = footer(ctx).await;
@ -393,7 +206,6 @@ async fn clock(ctx: &Context, msg: &Message, _args: String) {
let language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_of(&msg.author, &pool).await;
let meridian = UserData::meridian_of(&msg.author, &pool).await;
let now = Utc::now().with_timezone(&timezone);
@ -403,34 +215,7 @@ async fn clock(ctx: &Context, msg: &Message, _args: String) {
.channel_id
.say(
&ctx,
clock_display.replacen("{}", &now.format(meridian.fmt_str()).to_string(), 1),
clock_display.replacen("{}", &now.format("%H:%M").to_string(), 1),
)
.await;
}
pub async fn clock_interaction(ctx: &Context, interaction: Interaction) {
let (pool, lm) = get_ctx_data(&ctx).await;
let language = UserData::language_of(&interaction.member, &pool).await;
let timezone = UserData::timezone_of(&interaction.member, &pool).await;
let meridian = UserData::meridian_of(&interaction.member, &pool).await;
let now = Utc::now().with_timezone(&timezone);
let clock_display = lm.get(&language, "clock/time");
interaction
.create_interaction_response(ctx, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| {
data.content(clock_display.replacen(
"{}",
&now.format(meridian.fmt_str()).to_string(),
1,
))
})
})
.await
.unwrap();
}

View File

@ -1,13 +1,13 @@
use regex_command_attr::command;
use serenity::{
builder::CreateActionRow,
client::Context,
framework::Framework,
model::{
channel::ReactionType,
channel::{Channel, Message},
id::{ChannelId, RoleId},
interactions::{Interaction, InteractionResponseType},
channel::Message,
id::{ChannelId, MessageId, RoleId},
interactions::message_component::ButtonStyle,
},
};
@ -24,12 +24,11 @@ use crate::{
consts::{REGEX_ALIAS, REGEX_CHANNEL, REGEX_COMMANDS, REGEX_ROLE, THEME_COLOR},
framework::SendIterator,
get_ctx_data,
models::{ChannelData, GuildData, UserData},
models::{channel_data::ChannelData, guild_data::GuildData, user_data::UserData, CtxGuildData},
FrameworkCtx, PopularTimezones,
};
use crate::models::CtxGuildData;
use std::{collections::HashMap, iter, time::Duration};
use std::{collections::HashMap, iter};
#[command]
#[supports_dm(false)]
@ -47,13 +46,11 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) {
let (channel, local) = match capture_opt {
Some(capture) => (
ChannelId(capture.as_str().parse::<u64>().unwrap())
.to_channel_cached(&ctx)
.await,
ChannelId(capture.as_str().parse::<u64>().unwrap()).to_channel_cached(&ctx),
false,
),
None => (msg.channel(&ctx).await, true),
None => (msg.channel(&ctx).await.ok(), true),
};
let mut channel_data = ChannelData::from_channel(channel.unwrap(), &pool)
@ -75,18 +72,16 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) {
.say(&ctx, lm.get(&language, "blacklist/added_from"))
.await;
}
} else if local {
let _ = msg
.channel_id
.say(&ctx, lm.get(&language, "blacklist/removed"))
.await;
} else {
if local {
let _ = msg
.channel_id
.say(&ctx, lm.get(&language, "blacklist/removed"))
.await;
} else {
let _ = msg
.channel_id
.say(&ctx, lm.get(&language, "blacklist/removed_from"))
.await;
}
let _ = msg
.channel_id
.say(&ctx, lm.get(&language, "blacklist/removed_from"))
.await;
}
}
@ -113,11 +108,7 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
let content = lm
.get(&user_data.language, "timezone/set_p")
.replacen("{timezone}", &user_data.timezone, 1)
.replacen(
"{time}",
&now.format(user_data.meridian().fmt_str_short()).to_string(),
1,
);
.replacen("{time}", &now.format("%H:%M").to_string(), 1);
let _ =
msg.channel_id
@ -140,22 +131,25 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
Err(_) => {
let filtered_tz = TZ_VARIANTS
.iter()
.map(|tz| (tz, tz.to_string(), levenshtein(&tz.to_string(), &args)))
.filter(|(_, tz, dist)| args.contains(tz) || tz.contains(&args) || dist < &4)
.filter(|tz| {
args.contains(&tz.to_string())
|| tz.to_string().contains(&args)
|| levenshtein(&tz.to_string(), &args) < 4
})
.take(25)
.map(|(tz, tz_s, _)| {
(
tz_s,
format!(
"🕗 `{}`",
Utc::now()
.with_timezone(tz)
.format(user_data.meridian().fmt_str_short())
.to_string()
),
true,
)
});
.map(|t| t.to_owned())
.collect::<Vec<Tz>>();
let fields = filtered_tz.iter().map(|tz| {
(
tz.to_string(),
format!(
"🕗 `{}`",
Utc::now().with_timezone(tz).format("%H:%M").to_string()
),
true,
)
});
let _ = msg
.channel_id
@ -164,9 +158,24 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
e.title(lm.get(&user_data.language, "timezone/no_timezone_title"))
.description(lm.get(&user_data.language, "timezone/no_timezone"))
.color(*THEME_COLOR)
.fields(filtered_tz)
.fields(fields)
.footer(|f| f.text(footer_text))
.url("https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee")
}).components(|c| {
for row in filtered_tz.as_slice().chunks(5) {
let mut action_row = CreateActionRow::default();
for timezone in row {
action_row.create_button(|b| {
b.style(ButtonStyle::Secondary)
.label(timezone.to_string())
.custom_id(format!("timezone:{}", timezone.to_string()))
});
}
c.add_action_row(action_row);
}
c
})
})
.await;
@ -190,10 +199,7 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
t.to_string(),
format!(
"🕗 `{}`",
Utc::now()
.with_timezone(t)
.format(user_data.meridian().fmt_str_short())
.to_string()
Utc::now().with_timezone(t).format("%H:%M").to_string()
),
true,
)
@ -210,161 +216,24 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
.footer(|f| f.text(footer_text))
.url("https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee")
})
})
.await;
}
}
.components(|c| {
for row in popular_timezones.as_slice().chunks(5) {
let mut action_row = CreateActionRow::default();
for timezone in row {
action_row.create_button(|b| {
b.style(ButtonStyle::Secondary)
.label(timezone.to_string())
.custom_id(format!("timezone:{}", timezone.to_string()))
});
}
pub async fn timezone_interaction(ctx: &Context, interaction: Interaction) {
let (pool, lm) = get_ctx_data(&&ctx).await;
c.add_action_row(action_row);
}
let mut user_data = UserData::from_user(&interaction.member.user, &ctx, &pool)
.await
.unwrap();
let footer_text = lm.get(&user_data.language, "timezone/footer").replacen(
"{timezone}",
&user_data.timezone,
1,
);
if let Some(data) = &interaction.data {
if let Some(timezone) = data
.options
.first()
.map(|inner| {
inner
.value
.clone()
.map(|v| v.as_str().map(|s| s.to_string()))
.flatten()
})
.flatten()
.map(|tz| tz.parse::<Tz>().ok())
.flatten()
{
user_data.timezone = timezone.to_string();
user_data.commit_changes(&pool).await;
let now = Utc::now().with_timezone(&user_data.timezone());
let content = lm
.get(&user_data.language, "timezone/set_p")
.replacen("{timezone}", &user_data.timezone, 1)
.replacen(
"{time}",
&now.format(user_data.meridian().fmt_str_short()).to_string(),
1,
);
interaction
.create_interaction_response(&ctx, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| {
data.embed(|e| {
e.title(lm.get(&user_data.language, "timezone/set_p_title"))
.description(content)
.color(*THEME_COLOR)
.footer(|f| {
f.text(
lm.get(&user_data.language, "timezone/footer")
.replacen("{timezone}", &user_data.timezone, 1),
)
})
})
})
})
.await
.unwrap();
} else {
let content = lm
.get(&user_data.language, "timezone/no_argument")
.replace("{prefix}", "/");
let popular_timezones = ctx
.data
.read()
.await
.get::<PopularTimezones>()
.cloned()
.unwrap();
let popular_timezones_iter = popular_timezones.iter().map(|t| {
(
t.to_string(),
format!(
"🕗 `{}`",
Utc::now()
.with_timezone(t)
.format(user_data.meridian().fmt_str_short())
.to_string()
),
true,
)
});
interaction
.create_interaction_response(&ctx, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| {
data.embed(|e| {
e.title(lm.get(&user_data.language, "timezone/no_argument_title"))
.description(content)
.color(*THEME_COLOR)
.fields(popular_timezones_iter)
.footer(|f| f.text(footer_text))
.url("https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee")
})
})
})
.await
.unwrap();
}
}
}
#[command("meridian")]
async fn change_meridian(ctx: &Context, msg: &Message, args: String) {
let (pool, lm) = get_ctx_data(&ctx).await;
let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
if &args == "12" {
user_data.meridian_time = true;
user_data.commit_changes(&pool).await;
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.embed(|e| {
e.title(lm.get(&user_data.language, "meridian/title"))
.color(*THEME_COLOR)
.description(lm.get(&user_data.language, "meridian/12"))
c
})
})
.await;
} else if &args == "24" {
user_data.meridian_time = false;
user_data.commit_changes(&pool).await;
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.embed(|e| {
e.title(lm.get(&user_data.language, "meridian/title"))
.color(*THEME_COLOR)
.description(lm.get(&user_data.language, "meridian/24"))
})
})
.await;
} else {
let prefix = ctx.prefix(msg.guild_id).await;
command_help(ctx, msg, lm, &prefix, &user_data.language, "meridian").await;
}
}
@ -411,6 +280,28 @@ async fn language(ctx: &Context, msg: &Message, args: String) {
.description(lm.get(&user_data.language, "lang/invalid"))
.fields(language_codes)
})
.components(|c| {
for row in lm
.all_languages()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect::<Vec<(String, String)>>()
.as_slice()
.chunks(5)
{
let mut action_row = CreateActionRow::default();
for (code, name) in row {
action_row.create_button(|b| {
b.style(ButtonStyle::Primary)
.label(name.to_title_case())
.custom_id(format!("lang:{}", code.to_uppercase()))
});
}
c.add_action_row(action_row);
}
c
})
})
.await;
}
@ -424,21 +315,7 @@ async fn language(ctx: &Context, msg: &Message, args: String) {
)
});
let flags = lm
.all_languages()
.map(|(k, _)| ReactionType::Unicode(lm.get(k, "flag").to_string()));
let can_react = if let Some(Channel::Guild(channel)) = msg.channel(&ctx).await {
channel
.permissions_for_user(&ctx, ctx.cache.current_user().await)
.await
.map(|p| p.add_reactions())
.unwrap_or(false)
} else {
true
};
let reactor = msg
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.embed(|e| {
@ -446,87 +323,31 @@ async fn language(ctx: &Context, msg: &Message, args: String) {
.color(*THEME_COLOR)
.description(lm.get(&user_data.language, "lang/select"))
.fields(language_codes)
});
})
.components(|c| {
for row in lm
.all_languages()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect::<Vec<(String, String)>>()
.as_slice()
.chunks(5)
{
let mut action_row = CreateActionRow::default();
for (code, name) in row {
action_row.create_button(|b| {
b.style(ButtonStyle::Primary)
.label(name.to_title_case())
.custom_id(format!("lang:{}", code.to_uppercase()))
});
}
if can_react {
m.reactions(flags);
}
c.add_action_row(action_row);
}
m
c
})
})
.await;
if let Ok(sent_msg) = reactor {
let reaction_reply = sent_msg
.await_reaction(&ctx)
.timeout(Duration::from_secs(45))
.await;
if let Some(reaction_action) = reaction_reply {
if reaction_action.is_added() {
if let ReactionType::Unicode(emoji) = &reaction_action.as_inner_ref().emoji {
if let Some(lang) = lm.get_language_by_flag(emoji) {
user_data.language = lang.to_string();
user_data.commit_changes(&pool).await;
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.embed(|e| {
e.title(lm.get(&user_data.language, "lang/set_p_title"))
.color(*THEME_COLOR)
.description(lm.get(&user_data.language, "lang/set_p"))
})
})
.await;
}
}
}
}
if let Some(Channel::Guild(channel)) = msg.channel(&ctx).await {
let has_perms = channel
.permissions_for_user(&ctx, ctx.cache.current_user().await)
.await
.map(|p| p.manage_messages())
.unwrap_or(false);
if has_perms {
let _ = sent_msg.delete_reactions(&ctx).await;
}
}
}
}
}
pub async fn language_interaction(ctx: &Context, interaction: Interaction) {
let (pool, lm) = get_ctx_data(&ctx).await;
let mut user_data = UserData::from_user(&interaction.member.user, &ctx, &pool)
.await
.unwrap();
if let Some(data) = &interaction.data {
let option = &data.options[0];
user_data.language = option.value.clone().unwrap().as_str().unwrap().to_string();
user_data.commit_changes(&pool).await;
interaction
.create_interaction_response(ctx, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| {
data.embed(|e| {
e.title(lm.get(&user_data.language, "lang/set_p_title"))
.color(*THEME_COLOR)
.description(lm.get(&user_data.language, "lang/set_p"))
})
})
})
.await
.unwrap();
}
}
@ -537,7 +358,6 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) {
let (pool, lm) = get_ctx_data(&ctx).await;
let guild_data = ctx.guild_data(msg.guild_id.unwrap()).await.unwrap();
let language = UserData::language_of(&msg.author, &pool).await;
if args.len() > 5 {
@ -552,6 +372,7 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) {
.await;
} else {
guild_data.write().await.prefix = args;
guild_data.read().await.commit_changes(&pool).await;
let content = lm.get(&language, "prefix/success").replacen(
@ -564,49 +385,6 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) {
}
}
pub async fn prefix_interaction(ctx: &Context, interaction: Interaction) {
let (pool, lm) = get_ctx_data(&ctx).await;
let guild_data = ctx.guild_data(interaction.guild_id).await.unwrap();
let language = UserData::language_of(&interaction.member, &pool).await;
if let Some(data) = &interaction.data {
let option = &data.options[0];
let new_prefix = option.value.clone().unwrap().as_str().unwrap().to_string();
if new_prefix.len() > 5 {
interaction
.create_interaction_response(ctx, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| {
data.content(lm.get(&language, "prefix/too_long"))
})
})
.await
.unwrap();
} else {
guild_data.write().await.prefix = new_prefix.clone();
guild_data.read().await.commit_changes(&pool).await;
let content = lm
.get(&language, "prefix/success")
.replacen("{prefix}", &new_prefix, 1);
interaction
.create_interaction_response(ctx, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|data| data.content(content))
})
.await
.unwrap();
}
}
}
#[command]
#[supports_dm(false)]
#[permission_level(Restricted)]
@ -614,7 +392,7 @@ async fn restrict(ctx: &Context, msg: &Message, args: String) {
let (pool, lm) = get_ctx_data(&ctx).await;
let language = UserData::language_of(&msg.author, &pool).await;
let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool)
let guild_data = GuildData::from_guild(msg.guild(&ctx).unwrap(), &pool)
.await
.unwrap();
@ -631,7 +409,7 @@ async fn restrict(ctx: &Context, msg: &Message, args: String) {
.unwrap(),
);
let role_opt = role_id.to_role_cached(&ctx).await;
let role_opt = role_id.to_role_cached(&ctx);
if let Some(role) = role_opt {
let _ = sqlx::query!(
@ -844,7 +622,8 @@ SELECT command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHER
.get::<FrameworkCtx>().cloned().expect("Could not get FrameworkCtx from data");
let mut new_msg = msg.clone();
new_msg.content = format!("<@{}> {}", &ctx.cache.current_user_id().await, row.command);
new_msg.content = format!("<@{}> {}", &ctx.cache.current_user_id(), row.command);
new_msg.id = MessageId(0);
framework.dispatch(ctx.clone(), new_msg).await;
},

View File

@ -1,36 +1,39 @@
use regex_command_attr::command;
use chrono_tz::Tz;
use serenity::{
cache::Cache,
client::Context,
http::CacheHttp,
model::{
channel::GuildChannel,
channel::Message,
channel::{Channel, GuildChannel},
guild::Guild,
id::{ChannelId, GuildId, UserId},
misc::Mentionable,
webhook::Webhook,
},
prelude::Mentionable,
Result as SerenityResult,
};
use crate::{
check_subscription_on_message, command_help,
consts::{
CHARACTERS, DAY, HOUR, MAX_TIME, MINUTE, MIN_INTERVAL, REGEX_CHANNEL, REGEX_CHANNEL_USER,
REGEX_CONTENT_SUBSTITUTION, REGEX_NATURAL_COMMAND_1, REGEX_NATURAL_COMMAND_2,
REGEX_REMIND_COMMAND, THEME_COLOR,
CHARACTERS, MAX_TIME, MIN_INTERVAL, REGEX_CHANNEL_USER, REGEX_CONTENT_SUBSTITUTION,
REGEX_NATURAL_COMMAND_1, REGEX_NATURAL_COMMAND_2, REGEX_REMIND_COMMAND, THEME_COLOR,
},
framework::SendIterator,
get_ctx_data,
models::{ChannelData, GuildData, Timer, UserData},
models::{
channel_data::ChannelData,
guild_data::GuildData,
reminder::{LookFlags, Reminder},
timer::Timer,
user_data::UserData,
CtxGuildData,
},
time_parser::{natural_parser, TimeParser},
};
use chrono::{offset::TimeZone, NaiveDateTime};
use chrono::NaiveDateTime;
use rand::{rngs::OsRng, seq::IteratorRandom};
@ -42,44 +45,13 @@ use std::{
collections::HashSet,
convert::TryInto,
default::Default,
env,
fmt::Display,
string::ToString,
time::{SystemTime, UNIX_EPOCH},
};
use crate::models::{CtxGuildData, MeridianType};
use regex::Captures;
use serenity::model::channel::Channel;
use serenity::model::interactions::Interaction;
fn shorthand_displacement(seconds: u64) -> String {
let (days, seconds) = seconds.div_rem(&DAY);
let (hours, seconds) = seconds.div_rem(&HOUR);
let (minutes, seconds) = seconds.div_rem(&MINUTE);
let time_repr = format!("{:02}:{:02}:{:02}", hours, minutes, seconds);
format!("{} days, {}", days, time_repr)
}
fn longhand_displacement(seconds: u64) -> String {
let (days, seconds) = seconds.div_rem(&DAY);
let (hours, seconds) = seconds.div_rem(&HOUR);
let (minutes, seconds) = seconds.div_rem(&MINUTE);
let mut sections = vec![];
for (var, name) in [days, hours, minutes, seconds]
.iter()
.zip(["days", "hours", "minutes", "seconds"].iter())
{
if *var > 0 {
sections.push(format!("{} {}", var, name));
}
}
sections.join(", ")
}
async fn create_webhook(
ctx: impl CacheHttp,
@ -113,7 +85,6 @@ async fn pause(ctx: &Context, msg: &Message, args: String) {
let language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_of(&msg.author, &pool).await;
let meridian = UserData::meridian_of(&msg.author, &pool).await;
let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool)
.await
@ -149,13 +120,9 @@ async fn pause(ctx: &Context, msg: &Message, args: String) {
channel.commit_changes(&pool).await;
let content = lm.get(&language, "pause/paused_until").replace(
"{}",
&timezone
.timestamp(timestamp, 0)
.format(meridian.fmt_str())
.to_string(),
);
let content = lm
.get(&language, "pause/paused_until")
.replace("{}", &format!("<t:{}:D>", timestamp));
let _ = msg.channel_id.say(&ctx, content).await;
}
@ -185,7 +152,7 @@ async fn offset(ctx: &Context, msg: &Message, args: String) {
let parser = TimeParser::new(&args, user_data.timezone());
if let Ok(displacement) = parser.displacement() {
if let Some(guild) = msg.guild(&ctx).await {
if let Some(guild) = msg.guild(&ctx) {
let guild_data = GuildData::from_guild(guild, &pool).await.unwrap();
sqlx::query!(
@ -194,7 +161,7 @@ UPDATE reminders
INNER JOIN `channels`
ON `channels`.id = reminders.channel_id
SET
reminders.`utc_time` = reminders.`utc_time` + ?
reminders.`utc_time` = DATE_ADD(reminders.`utc_time`, INTERVAL ? SECOND)
WHERE channels.guild_id = ?
",
displacement,
@ -206,7 +173,7 @@ UPDATE reminders
} else {
sqlx::query!(
"
UPDATE reminders SET `utc_time` = `utc_time` + ? WHERE reminders.channel_id = ?
UPDATE reminders SET `utc_time` = DATE_ADD(`utc_time`, INTERVAL ? SECOND) WHERE reminders.channel_id = ?
",
displacement,
user_data.dm_channel
@ -286,177 +253,28 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) {
}
}
enum TimeDisplayType {
Absolute,
Relative,
}
struct LookFlags {
pub limit: u16,
pub show_disabled: bool,
pub channel_id: Option<u64>,
time_display: TimeDisplayType,
}
impl Default for LookFlags {
fn default() -> Self {
Self {
limit: u16::MAX,
show_disabled: true,
channel_id: None,
time_display: TimeDisplayType::Relative,
}
}
}
impl LookFlags {
fn from_string(args: &str) -> Self {
let mut new_flags: Self = Default::default();
for arg in args.split(' ') {
match arg {
"enabled" => {
new_flags.show_disabled = false;
}
"time" => {
new_flags.time_display = TimeDisplayType::Absolute;
}
param => {
if let Ok(val) = param.parse::<u16>() {
new_flags.limit = val;
} else {
if let Some(channel) = REGEX_CHANNEL
.captures(&arg)
.map(|cap| cap.get(1))
.flatten()
.map(|c| c.as_str().parse::<u64>().unwrap())
{
new_flags.channel_id = Some(channel);
}
}
}
}
}
new_flags
}
}
struct LookReminder {
id: u32,
time: NaiveDateTime,
interval: Option<u32>,
channel: u64,
content: String,
description: String,
}
impl LookReminder {
fn display_content(&self) -> String {
if self.content.len() > 0 {
self.content.clone()
} else {
self.description.clone()
}
}
fn display(
&self,
flags: &LookFlags,
meridian: &MeridianType,
timezone: &Tz,
inter: &str,
) -> String {
let time_display = match flags.time_display {
TimeDisplayType::Absolute => timezone
.from_utc_datetime(&self.time)
.format(meridian.fmt_str())
.to_string(),
TimeDisplayType::Relative => {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
longhand_displacement((self.time.timestamp() as u64).checked_sub(now).unwrap_or(1))
}
};
if let Some(interval) = self.interval {
format!(
"'{}' *{}* **{}**, repeating every **{}**",
self.display_content(),
&inter,
time_display,
longhand_displacement(interval as u64)
)
} else {
format!(
"'{}' *{}* **{}**",
self.display_content(),
&inter,
time_display
)
}
}
}
#[command("look")]
#[permission_level(Managed)]
async fn look(ctx: &Context, msg: &Message, args: String) {
let (pool, lm) = get_ctx_data(&ctx).await;
let language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_of(&msg.author, &pool).await;
let meridian = UserData::meridian_of(&msg.author, &pool).await;
let flags = LookFlags::from_string(&args);
let enabled = if flags.show_disabled { "0,1" } else { "1" };
let channel_opt = msg.channel_id.to_channel_cached(&ctx).await;
let channel_opt = msg.channel_id.to_channel_cached(&ctx);
let channel_id = if let Some(Channel::Guild(channel)) = channel_opt {
if Some(channel.guild_id) == msg.guild_id {
flags
.channel_id
.unwrap_or_else(|| msg.channel_id.as_u64().to_owned())
flags.channel_id.unwrap_or(msg.channel_id)
} else {
msg.channel_id.as_u64().to_owned()
msg.channel_id
}
} else {
msg.channel_id.as_u64().to_owned()
msg.channel_id
};
let reminders = sqlx::query_as!(
LookReminder,
"
SELECT
reminders.id, reminders.utc_time AS time, reminders.interval, channels.channel, reminders.content, reminders.embed_description AS description
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
WHERE
channels.channel = ? AND
FIND_IN_SET(reminders.enabled, ?)
ORDER BY
reminders.utc_time
LIMIT
?
",
channel_id,
enabled,
flags.limit
)
.fetch_all(&pool)
.await
.unwrap();
let reminders = Reminder::from_channel(ctx, channel_id, &flags).await;
if reminders.is_empty() {
let _ = msg
@ -468,7 +286,7 @@ LIMIT
let display = reminders
.iter()
.map(|reminder| reminder.display(&flags, &meridian, &timezone, &inter));
.map(|reminder| reminder.display(&flags, &inter));
let _ = msg.channel_id.say_lines(&ctx, display).await;
}
@ -486,90 +304,19 @@ async fn delete(ctx: &Context, msg: &Message, _args: String) {
.say(&ctx, lm.get(&user_data.language, "del/listing"))
.await;
let reminders = if let Some(guild_id) = msg.guild_id {
let guild_opt = guild_id.to_guild_cached(&ctx).await;
if let Some(guild) = guild_opt {
let channels = guild
.channels
.keys()
.into_iter()
.map(|k| k.as_u64().to_string())
.collect::<Vec<String>>()
.join(",");
sqlx::query_as_unchecked!(
LookReminder,
"
SELECT
reminders.id, reminders.utc_time AS time, reminders.interval, channels.channel, reminders.content, reminders.embed_description AS description
FROM
reminders
INNER JOIN
channels
ON
channels.id = reminders.channel_id
WHERE
FIND_IN_SET(channels.channel, ?)
",
channels
)
.fetch_all(&pool)
.await
} else {
sqlx::query_as_unchecked!(
LookReminder,
"
SELECT
reminders.id, reminders.utc_time AS time, reminders.interval, channels.channel, reminders.content, reminders.embed_description AS description
FROM
reminders
INNER JOIN
channels
ON
channels.id = reminders.channel_id
WHERE
channels.guild_id = (SELECT id FROM guilds WHERE guild = ?)
",
guild_id.as_u64()
)
.fetch_all(&pool)
.await
}
} else {
sqlx::query_as!(
LookReminder,
"
SELECT
reminders.id, reminders.utc_time AS time, reminders.interval, channels.channel, reminders.content, reminders.embed_description AS description
FROM
reminders
INNER JOIN
channels
ON
channels.id = reminders.channel_id
WHERE
channels.channel = ?
",
msg.channel_id.as_u64()
)
.fetch_all(&pool)
.await
}
.unwrap();
let mut reminder_ids: Vec<u32> = vec![];
let reminders = Reminder::from_guild(ctx, msg.guild_id, msg.author.id).await;
let enumerated_reminders = reminders.iter().enumerate().map(|(count, reminder)| {
reminder_ids.push(reminder.id);
let time = user_data.timezone().timestamp(reminder.time.timestamp(), 0);
format!(
"**{}**: '{}' *<#{}>* at {}",
"**{}**: '{}' *<#{}>* at <t:{}>",
count + 1,
reminder.display_content(),
reminder.channel,
time.format(user_data.meridian().fmt_str())
reminder.utc_time.timestamp()
)
});
@ -813,7 +560,6 @@ impl ReminderScope {
#[derive(PartialEq, Eq, Hash, Debug)]
enum ReminderError {
LongTime,
LongInterval,
PastTime,
ShortInterval,
@ -840,7 +586,6 @@ trait ToResponse {
impl ToResponse for ReminderError {
fn to_response(&self) -> &'static str {
match self {
Self::LongTime => "remind/long_time",
Self::LongInterval => "interval/long_interval",
Self::PastTime => "remind/past_time",
Self::ShortInterval => "interval/short_interval",
@ -853,7 +598,6 @@ impl ToResponse for ReminderError {
fn to_response_natural(&self) -> &'static str {
match self {
Self::LongTime => "natural/long_time",
Self::InvalidTime => "natural/invalid_time",
_ => self.to_response(),
}
@ -937,7 +681,7 @@ impl Content {
Ok(Self {
content: content.to_string(),
tts: false,
attachment: Some(attachment_bytes.clone()),
attachment: Some(attachment_bytes),
attachment_name: Some(attachment.filename.clone()),
})
} else {
@ -1021,36 +765,36 @@ async fn countdown(ctx: &Context, msg: &Message, args: String) {
INSERT INTO reminders (
`uid`,
`name`,
`channel_id`,
`utc_time`,
`interval`,
`expires`,
`embed_title`,
`embed_description`,
`embed_color`,
`set_by`
`channel_id`,
`utc_time`,
`interval_seconds`,
`set_by`,
`expires`
) VALUES (
?,
'Countdown',
(SELECT id FROM channels WHERE channel = ?),
?,
?,
FROM_UNIXTIME(?),
?,
?,
?,
(SELECT id FROM users WHERE user = ?)
?,
?,
?,
(SELECT id FROM users WHERE user = ?),
FROM_UNIXTIME(?)
)
",
generate_uid(),
msg.channel_id.as_u64(),
first_time,
interval,
target_ts,
event_name,
description,
*THEME_COLOR,
msg.channel_id.as_u64(),
first_time,
interval,
msg.author.id.as_u64(),
target_ts
)
.execute(&pool)
.await
@ -1140,7 +884,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
Some(captures) => {
let parsed = parse_mention_list(captures.name("mentions").unwrap().as_str());
let scopes = if parsed.len() == 0 {
let scopes = if parsed.is_empty() {
vec![ReminderScope::Channel(msg.channel_id.into())]
} else {
parsed
@ -1179,6 +923,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
match content_res {
Ok(mut content) => {
let mut ok_locations = vec![];
let mut ok_reminders = vec![];
let mut err_locations = vec![];
let mut err_types = HashSet::new();
@ -1190,18 +935,22 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
msg.guild_id,
&scope,
&time_parser,
timezone.to_string(),
expires_parser.as_ref().clone(),
expires_parser.as_ref(),
interval,
&mut content,
)
.await;
if let Err(e) = res {
err_locations.push(scope);
err_types.insert(e);
} else {
ok_locations.push(scope);
match res {
Err(e) => {
err_locations.push(scope);
err_types.insert(e);
}
Ok(id) => {
ok_locations.push(scope);
ok_reminders.push(id);
}
}
}
@ -1212,9 +961,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
.replace("{location}", &ok_locations[0].mention())
.replace(
"{offset}",
&shorthand_displacement(
time_parser.displacement().unwrap() as u64,
),
&format!("<t:{}:R>", time_parser.timestamp().unwrap()),
),
n => lm
.get(&language, "remind/success_bulk")
@ -1229,9 +976,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
)
.replace(
"{offset}",
&shorthand_displacement(
time_parser.displacement().unwrap() as u64,
),
&format!("<t:{}:R>", time_parser.timestamp().unwrap()),
),
};
@ -1338,11 +1083,6 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
async fn natural(ctx: &Context, msg: &Message, args: String) {
let (pool, lm) = get_ctx_data(&ctx).await;
let now = SystemTime::now();
let since_epoch = now
.duration_since(UNIX_EPOCH)
.expect("Time calculated as going backwards. Very bad");
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
match REGEX_NATURAL_COMMAND_1.captures(&args) {
@ -1406,8 +1146,6 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
match content_res {
Ok(mut content) => {
let offset = timestamp as u64 - since_epoch.as_secs();
let mut ok_locations = vec![];
let mut err_locations = vec![];
let mut err_types = HashSet::new();
@ -1420,9 +1158,8 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
msg.guild_id,
&scope,
timestamp,
user_data.timezone.clone(),
expires,
interval.clone(),
interval,
&mut content,
)
.await;
@ -1440,7 +1177,7 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
1 => lm
.get(&user_data.language, "remind/success")
.replace("{location}", &ok_locations[0].mention())
.replace("{offset}", &shorthand_displacement(offset)),
.replace("{offset}", &format!("<t:{}:R>", timestamp)),
n => lm
.get(&user_data.language, "remind/success_bulk")
.replace("{number}", &n.to_string())
@ -1452,7 +1189,7 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
.collect::<Vec<String>>()
.join(", "),
)
.replace("{offset}", &shorthand_displacement(offset)),
.replace("{offset}", &format!("<t:{}:R>", timestamp)),
};
let error_part = format!(
@ -1553,35 +1290,21 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
}
}
pub async fn set_reminder(ctx: &Context, interaction: Interaction) {
let (pool, lm) = get_ctx_data(&ctx).await;
let now = SystemTime::now();
let since_epoch = now
.duration_since(UNIX_EPOCH)
.expect("Time calculated as going backwards. Very bad");
let user_data = UserData::from_user(&interaction.member.user, &ctx, &pool)
.await
.unwrap();
}
async fn create_reminder<'a, U: Into<u64>, T: TryInto<i64>>(
ctx: impl CacheHttp + AsRef<Cache>,
ctx: &Context,
pool: &MySqlPool,
user_id: U,
guild_id: Option<GuildId>,
scope_id: &ReminderScope,
time_parser: T,
timezone: String,
expires_parser: Option<T>,
interval: Option<i64>,
content: &mut Content,
) -> Result<(), ReminderError> {
) -> Result<Reminder, ReminderError> {
let user_id = user_id.into();
if let Some(g_id) = guild_id {
if let Some(guild) = g_id.to_guild_cached(&ctx).await {
if let Some(guild) = g_id.to_guild_cached(&ctx) {
content.substitute(guild);
}
}
@ -1590,11 +1313,19 @@ async fn create_reminder<'a, U: Into<u64>, T: TryInto<i64>>(
let db_channel_id = match scope_id {
ReminderScope::User(user_id) => {
let user = UserId(*user_id).to_user(&ctx).await.unwrap();
if let Ok(user) = UserId(*user_id).to_user(&ctx).await {
let user_data = UserData::from_user(&user, &ctx, &pool).await.unwrap();
let user_data = UserData::from_user(&user, &ctx, &pool).await.unwrap();
if let Some(guild_id) = guild_id {
if guild_id.member(&ctx, user).await.is_err() {
return Err(ReminderError::InvalidTag);
}
}
user_data.dm_channel
user_data.dm_channel
} else {
return Err(ReminderError::InvalidTag);
}
}
ReminderScope::Channel(channel_id) => {
@ -1649,32 +1380,52 @@ async fn create_reminder<'a, U: Into<u64>, T: TryInto<i64>>(
.as_secs() as i64;
if time >= unix_time - 10 {
if time > unix_time + *MAX_TIME {
Err(ReminderError::LongTime)
} else {
sqlx::query!(
"
INSERT INTO reminders (uid, content, tts, attachment, attachment_name, channel_id, `utc_time`, timezone, expires, `interval`, set_by) VALUES
(?, ?, ?, ?, ?, ?, FROM_UNIXTIME(?), ?, FROM_UNIXTIME(?), ?, (SELECT id FROM users WHERE user = ? LIMIT 1))
",
generate_uid(),
content.content,
content.tts,
content.attachment,
content.attachment_name,
db_channel_id,
time,
timezone,
expires,
interval,
user_id
)
.execute(pool)
.await
.unwrap();
let uid = generate_uid();
Ok(())
}
sqlx::query!(
"
INSERT INTO reminders (
uid,
content,
tts,
attachment,
attachment_name,
channel_id,
`utc_time`,
expires,
`interval_seconds`,
set_by
) VALUES (
?,
?,
?,
?,
?,
?,
DATE_ADD(FROM_UNIXTIME(0), INTERVAL ? SECOND),
DATE_ADD(FROM_UNIXTIME(0), INTERVAL ? SECOND),
?,
(SELECT id FROM users WHERE user = ? LIMIT 1)
)
",
uid,
content.content,
content.tts,
content.attachment,
content.attachment_name,
db_channel_id,
time,
expires,
interval,
user_id
)
.execute(pool)
.await
.unwrap();
let reminder = Reminder::from_uid(ctx, uid).await.unwrap();
Ok(reminder)
} else if time < 0 {
// case required for if python returns -1
Err(ReminderError::InvalidTime)

View File

@ -12,8 +12,10 @@ use serenity::{
use std::fmt;
use crate::models::CtxGuildData;
use crate::{command_help, get_ctx_data, models::UserData};
use crate::{
command_help, get_ctx_data,
models::{user_data::UserData, CtxGuildData},
};
use sqlx::MySqlPool;
use std::convert::TryFrom;
@ -272,7 +274,12 @@ DELETE FROM todos WHERE user_id = (SELECT id FROM users WHERE user = ?) AND guil
self.add(extra, pool).await.unwrap();
let _ = msg.channel_id.say(&ctx, content).await;
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.content(content).allowed_mentions(|m| m.empty_parse())
})
.await;
}
SubCommand::Remove => {
@ -284,7 +291,12 @@ DELETE FROM todos WHERE user_id = (SELECT id FROM users WHERE user = ?) AND guil
1,
);
let _ = msg.channel_id.say(&ctx, content).await;
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.content(content).allowed_mentions(|m| m.empty_parse())
})
.await;
} else {
let _ = msg
.channel_id

View File

@ -51,7 +51,7 @@ lazy_static! {
.split(',')
.filter_map(|item| { item.parse::<u64>().ok() })
.collect::<Vec<u64>>())
.unwrap_or_else(|_| vec![])
.unwrap_or_else(|_| Vec::new())
);
pub static ref CNC_GUILD: Option<u64> = env::var("CNC_GUILD")

View File

@ -8,7 +8,7 @@ use serenity::{
model::{
channel::{Channel, GuildChannel, Message},
guild::{Guild, Member},
id::ChannelId,
id::{ChannelId, MessageId},
},
Result as SerenityResult,
};
@ -19,9 +19,11 @@ use regex::{Match, Regex, RegexBuilder};
use std::{collections::HashMap, fmt};
use crate::language_manager::LanguageManager;
use crate::models::{CtxGuildData, GuildData, UserData};
use crate::{models::ChannelData, SQLPool};
use crate::{
language_manager::LanguageManager,
models::{channel_data::ChannelData, guild_data::GuildData, user_data::UserData, CtxGuildData},
LimitExecutors, SQLPool,
};
type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, ()>;
@ -238,7 +240,7 @@ impl RegexFramework {
let mut command_names_vec =
self.commands.keys().map(|k| &k[..]).collect::<Vec<&str>>();
command_names_vec.sort_unstable_by(|a, b| b.len().cmp(&a.len()));
command_names_vec.sort_unstable_by_key(|a| a.len());
command_names = command_names_vec.join("|");
}
@ -274,7 +276,7 @@ impl RegexFramework {
})
.collect::<Vec<&str>>();
command_names_vec.sort_unstable_by(|a, b| b.len().cmp(&a.len()));
command_names_vec.sort_unstable_by_key(|a| a.len());
dm_command_names = command_names_vec.join("|");
}
@ -297,9 +299,9 @@ impl RegexFramework {
}
enum PermissionCheck {
None, // No permissions
Basic(bool, bool, bool, bool), // Send + Embed permissions (sufficient to reply)
All, // Above + Manage Webhooks (sufficient to operate)
None, // No permissions
Basic(bool, bool), // Send + Embed permissions (sufficient to reply)
All, // Above + Manage Webhooks (sufficient to operate)
}
#[async_trait]
@ -310,10 +312,10 @@ impl Framework for RegexFramework {
guild: &Guild,
channel: &GuildChannel,
) -> SerenityResult<PermissionCheck> {
let user_id = ctx.cache.current_user_id().await;
let user_id = ctx.cache.current_user_id();
let guild_perms = guild.member_permissions(&ctx, user_id).await?;
let channel_perms = channel.permissions_for_user(ctx, user_id).await?;
let channel_perms = channel.permissions_for_user(ctx, user_id)?;
let basic_perms = channel_perms.send_messages();
@ -324,8 +326,6 @@ impl Framework for RegexFramework {
PermissionCheck::Basic(
guild_perms.manage_webhooks(),
channel_perms.embed_links(),
channel_perms.add_reactions(),
channel_perms.manage_messages(),
)
} else {
PermissionCheck::None
@ -345,144 +345,155 @@ impl Framework for RegexFramework {
// gate to prevent analysing messages unnecessarily
if (msg.author.bot && self.ignore_bots) || msg.content.is_empty() {
}
// Guild Command
else if let (Some(guild), Some(Channel::Guild(channel))) =
(msg.guild(&ctx).await, msg.channel(&ctx).await)
{
let data = ctx.data.read().await;
} else {
// Guild Command
if let (Some(guild), Ok(Channel::Guild(channel))) =
(msg.guild(&ctx), msg.channel(&ctx).await)
{
let data = ctx.data.read().await;
let pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
let pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
if let Some(full_match) = self.command_matcher.captures(&msg.content) {
if check_prefix(&ctx, &guild, full_match.name("prefix")).await {
let lm = data.get::<LanguageManager>().unwrap();
if let Some(full_match) = self.command_matcher.captures(&msg.content) {
if check_prefix(&ctx, &guild, full_match.name("prefix")).await {
let lm = data.get::<LanguageManager>().unwrap();
let language = UserData::language_of(&msg.author, &pool);
let language = UserData::language_of(&msg.author, &pool);
match check_self_permissions(&ctx, &guild, &channel).await {
Ok(perms) => match perms {
PermissionCheck::All => {
let command = self
.commands
.get(&full_match.name("cmd").unwrap().as_str().to_lowercase())
match check_self_permissions(&ctx, &guild, &channel).await {
Ok(perms) => match perms {
PermissionCheck::All => {
let command = self
.commands
.get(
&full_match
.name("cmd")
.unwrap()
.as_str()
.to_lowercase(),
)
.unwrap();
let channel_data = ChannelData::from_channel(
msg.channel(&ctx).await.unwrap(),
&pool,
)
.await
.unwrap();
let channel_data = ChannelData::from_channel(
msg.channel(&ctx).await.unwrap(),
&pool,
)
.await
.unwrap();
if !command.can_blacklist || !channel_data.blacklisted {
let args = full_match
.name("args")
.map(|m| m.as_str())
.unwrap_or("")
.to_string();
if !command.can_blacklist || !channel_data.blacklisted {
let args = full_match
.name("args")
.map(|m| m.as_str())
.unwrap_or("")
.to_string();
let member = guild.member(&ctx, &msg.author).await.unwrap();
let member = guild.member(&ctx, &msg.author).await.unwrap();
if command.check_permissions(&ctx, &guild, &member).await {
dbg!(command.name);
if command.check_permissions(&ctx, &guild, &member).await {
dbg!(command.name);
{
let guild_id = guild.id.as_u64().to_owned();
GuildData::from_guild(guild, &pool)
.await
.unwrap_or_else(|_| {
panic!(
"Failed to create new guild object for {}",
guild_id
)
});
}
if msg.id == MessageId(0)
|| !ctx.check_executing(msg.author.id).await
{
ctx.set_executing(msg.author.id).await;
(command.func)(&ctx, &msg, args).await;
ctx.drop_executing(msg.author.id).await;
}
} else if command.required_perms
== PermissionLevel::Restricted
{
let guild_id = guild.id.as_u64().to_owned();
GuildData::from_guild(guild, &pool).await.expect(
&format!(
"Failed to create new guild object for {}",
guild_id
),
);
let _ = msg
.channel_id
.say(
&ctx,
lm.get(&language.await, "no_perms_restricted"),
)
.await;
} else if command.required_perms == PermissionLevel::Managed
{
let _ = msg
.channel_id
.say(
&ctx,
lm.get(&language.await, "no_perms_managed")
.replace(
"{prefix}",
&ctx.prefix(msg.guild_id).await,
),
)
.await;
}
(command.func)(&ctx, &msg, args).await;
} else if command.required_perms == PermissionLevel::Restricted
{
let _ = msg
.channel_id
.say(
&ctx,
lm.get(&language.await, "no_perms_restricted"),
)
.await;
} else if command.required_perms == PermissionLevel::Managed {
let _ = msg
.channel_id
.say(
&ctx,
lm.get(&language.await, "no_perms_managed")
.replace(
"{prefix}",
&ctx.prefix(msg.guild_id).await,
),
)
.await;
}
}
PermissionCheck::Basic(manage_webhooks, embed_links) => {
let response = lm
.get(&language.await, "no_perms_general")
.replace(
"{manage_webhooks}",
if manage_webhooks { "" } else { "" },
)
.replace(
"{embed_links}",
if embed_links { "" } else { "" },
);
let _ = msg.channel_id.say(&ctx, response).await;
}
PermissionCheck::None => {
warn!("Missing enough permissions for guild {}", guild.id);
}
},
Err(e) => {
error!(
"Error occurred getting permissions in guild {}: {:?}",
guild.id, e
);
}
PermissionCheck::Basic(
manage_webhooks,
embed_links,
add_reactions,
manage_messages,
) => {
let response = lm
.get(&language.await, "no_perms_general")
.replace(
"{manage_webhooks}",
if manage_webhooks { "" } else { "" },
)
.replace("{embed_links}", if embed_links { "" } else { "" })
.replace(
"{add_reactions}",
if add_reactions { "" } else { "" },
)
.replace(
"{manage_messages}",
if manage_messages { "" } else { "" },
);
let _ = msg.channel_id.say(&ctx, response).await;
}
PermissionCheck::None => {
warn!("Missing enough permissions for guild {}", guild.id);
}
},
Err(e) => {
error!(
"Error occurred getting permissions in guild {}: {:?}",
guild.id, e
);
}
}
}
}
}
// DM Command
else if self.dm_enabled {
if let Some(full_match) = self.dm_regex_matcher.captures(&msg.content[..]) {
let command = self
.commands
.get(&full_match.name("cmd").unwrap().as_str().to_lowercase())
.unwrap();
let args = full_match
.name("args")
.map(|m| m.as_str())
.unwrap_or("")
.to_string();
// DM Command
else if self.dm_enabled {
if let Some(full_match) = self.dm_regex_matcher.captures(&msg.content[..]) {
let command = self
.commands
.get(&full_match.name("cmd").unwrap().as_str().to_lowercase())
.unwrap();
let args = full_match
.name("args")
.map(|m| m.as_str())
.unwrap_or("")
.to_string();
dbg!(command.name);
dbg!(command.name);
(command.func)(&ctx, &msg, args).await;
if msg.id == MessageId(0) || !ctx.check_executing(msg.author.id).await {
ctx.set_executing(msg.author.id).await;
(command.func)(&ctx, &msg, args).await;
ctx.drop_executing(msg.author.id).await;
}
}
}
}
}

View File

@ -14,7 +14,7 @@ pub struct LanguageManager {
impl LanguageManager {
pub fn from_compiled(content: &'static str) -> Result<Self, Box<dyn Error + Send + Sync>> {
let new: Self = from_str(content.as_ref())?;
let new: Self = from_str(content)?;
Ok(new)
}
@ -23,13 +23,13 @@ impl LanguageManager {
self.strings
.get(language)
.map(|sm| sm.get(name))
.expect(&format!(r#"Language does not exist: "{}""#, language))
.unwrap_or_else(|| panic!(r#"Language does not exist: "{}""#, language))
.unwrap_or_else(|| {
self.strings
.get(&*LOCAL_LANGUAGE)
.map(|sm| {
sm.get(name)
.expect(&format!(r#"String does not exist: "{}""#, name))
.unwrap_or_else(|| panic!(r#"String does not exist: "{}""#, name))
})
.expect("LOCAL_LANGUAGE is not available")
})

View File

@ -11,13 +11,15 @@ mod time_parser;
use serenity::{
async_trait,
cache::Cache,
client::{bridge::gateway::GatewayIntents, Client},
client::Client,
futures::TryFutureExt,
http::{client::Http, CacheHttp},
model::{
channel::GuildChannel,
channel::Message,
guild::{Guild, GuildUnavailable},
guild::Guild,
id::{GuildId, UserId},
interactions::Interaction,
},
prelude::{Context, EventHandler, TypeMapKey},
utils::shard_id,
@ -27,18 +29,16 @@ use sqlx::mysql::MySqlPool;
use dotenv::dotenv;
use std::{collections::HashMap, env, sync::Arc};
use std::{collections::HashMap, env, sync::Arc, time::Instant};
use crate::{
commands::{info_cmds, moderation_cmds, reminder_cmds, todo_cmds},
consts::{CNC_GUILD, DEFAULT_PREFIX, SUBSCRIPTION_ROLES, THEME_COLOR},
framework::RegexFramework,
language_manager::LanguageManager,
models::GuildData,
models::{guild_data::GuildData, user_data::UserData},
};
use serenity::futures::TryFutureExt;
use inflector::Inflector;
use log::info;
@ -46,10 +46,13 @@ use dashmap::DashMap;
use tokio::sync::RwLock;
use chrono::Utc;
use chrono_tz::Tz;
use serenity::model::interactions::{Interaction, InteractionType};
use serenity::model::prelude::ApplicationCommandOptionType;
use std::collections::HashSet;
use serenity::model::gateway::GatewayIntents;
use serenity::model::guild::UnavailableGuild;
use serenity::model::prelude::{
InteractionApplicationCommandCallbackDataFlags, InteractionResponseType,
};
struct GuildDataCache;
@ -81,6 +84,65 @@ impl TypeMapKey for PopularTimezones {
type Value = Arc<Vec<Tz>>;
}
struct CurrentlyExecuting;
impl TypeMapKey for CurrentlyExecuting {
type Value = Arc<RwLock<HashMap<UserId, Instant>>>;
}
#[async_trait]
trait LimitExecutors {
async fn check_executing(&self, user: UserId) -> bool;
async fn set_executing(&self, user: UserId);
async fn drop_executing(&self, user: UserId);
}
#[async_trait]
impl LimitExecutors for Context {
async fn check_executing(&self, user: UserId) -> bool {
let currently_executing = self
.data
.read()
.await
.get::<CurrentlyExecuting>()
.cloned()
.unwrap();
let lock = currently_executing.read().await;
lock.get(&user)
.map_or(false, |now| now.elapsed().as_secs() < 4)
}
async fn set_executing(&self, user: UserId) {
let currently_executing = self
.data
.read()
.await
.get::<CurrentlyExecuting>()
.cloned()
.unwrap();
let mut lock = currently_executing.write().await;
lock.insert(user, Instant::now());
}
async fn drop_executing(&self, user: UserId) {
let currently_executing = self
.data
.read()
.await
.get::<CurrentlyExecuting>()
.cloned()
.unwrap();
let mut lock = currently_executing.write().await;
lock.remove(&user);
}
}
struct Handler;
#[async_trait]
@ -118,20 +180,20 @@ DELETE FROM channels WHERE channel = ?
.cloned()
.expect("Could not get SQLPool from data");
GuildData::from_guild(guild, &pool).await.expect(&format!(
"Failed to create new guild object for {}",
guild_id
));
GuildData::from_guild(guild, &pool)
.await
.unwrap_or_else(|_| {
panic!("Failed to create new guild object for {}", guild_id)
});
}
if let Ok(token) = env::var("DISCORDBOTS_TOKEN") {
let shard_count = ctx.cache.shard_count().await;
let shard_count = ctx.cache.shard_count();
let current_shard_id = shard_id(guild_id, shard_count);
let guild_count = ctx
.cache
.guilds()
.await
.iter()
.filter(|g| shard_id(g.as_u64().to_owned(), shard_count) == current_shard_id)
.count() as u64;
@ -153,7 +215,7 @@ DELETE FROM channels WHERE channel = ?
.post(
format!(
"https://top.gg/api/bots/{}/stats",
ctx.cache.current_user_id().await.as_u64()
ctx.cache.current_user_id().as_u64()
)
.as_str(),
)
@ -169,7 +231,12 @@ DELETE FROM channels WHERE channel = ?
}
}
async fn guild_delete(&self, ctx: Context, guild: GuildUnavailable, _guild: Option<Guild>) {
async fn guild_delete(
&self,
ctx: Context,
deleted_guild: UnavailableGuild,
_guild: Option<Guild>,
) {
let pool = ctx
.data
.read()
@ -185,13 +252,13 @@ DELETE FROM channels WHERE channel = ?
.get::<GuildDataCache>()
.cloned()
.unwrap();
guild_data_cache.remove(&guild.id);
guild_data_cache.remove(&deleted_guild.id);
sqlx::query!(
"
DELETE FROM guilds WHERE guild = ?
",
guild.id.as_u64()
deleted_guild.id.as_u64()
)
.execute(&pool)
.await
@ -199,25 +266,84 @@ DELETE FROM guilds WHERE guild = ?
}
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
match interaction.kind {
InteractionType::ApplicationCommand => {
if let Some(data) = &interaction.data {
match data.name.as_str() {
"timezone" => {
moderation_cmds::timezone_interaction(&ctx, interaction).await
let (pool, lm) = get_ctx_data(&&ctx).await;
match interaction {
Interaction::MessageComponent(interaction) => {
if let Some(member) = interaction.clone().member {
let data = interaction.data.clone();
if data.custom_id.starts_with("timezone:") {
let mut user_data = UserData::from_user(&member.user, &ctx, &pool)
.await
.unwrap();
let new_timezone = data.custom_id.replace("timezone:", "").parse::<Tz>();
if let Ok(timezone) = new_timezone {
user_data.timezone = timezone.to_string();
user_data.commit_changes(&pool).await;
let _ = interaction.create_interaction_response(&ctx, |r| {
r.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|d| {
let footer_text = lm.get(&user_data.language, "timezone/footer").replacen(
"{timezone}",
&user_data.timezone,
1,
);
let now = Utc::now().with_timezone(&user_data.timezone());
let content = lm
.get(&user_data.language, "timezone/set_p")
.replacen("{timezone}", &user_data.timezone, 1)
.replacen(
"{time}",
&now.format("%H:%M").to_string(),
1,
);
d.create_embed(|e| e.title(lm.get(&user_data.language, "timezone/set_p_title"))
.color(*THEME_COLOR)
.description(content)
.footer(|f| f.text(footer_text)))
.flags(InteractionApplicationCommandCallbackDataFlags::EPHEMERAL);
d
})
}).await;
}
} else if data.custom_id.starts_with("lang:") {
let mut user_data = UserData::from_user(&member.user, &ctx, &pool)
.await
.unwrap();
let lang_code = data.custom_id.replace("lang:", "");
if let Some(lang) = lm.get_language(&lang_code) {
user_data.language = lang.to_string();
user_data.commit_changes(&pool).await;
let _ = interaction
.create_interaction_response(&ctx, |r| {
r.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|d| {
d.create_embed(|e| {
e.title(
lm.get(&user_data.language, "lang/set_p_title"),
)
.color(*THEME_COLOR)
.description(
lm.get(&user_data.language, "lang/set_p"),
)
})
.flags(InteractionApplicationCommandCallbackDataFlags::EPHEMERAL)
})
})
.await;
}
"lang" => moderation_cmds::language_interaction(&ctx, interaction).await,
"prefix" => moderation_cmds::prefix_interaction(&ctx, interaction).await,
"help" => info_cmds::help_interaction(&ctx, interaction).await,
"info" => info_cmds::info_interaction(&ctx, interaction).await,
"donate" => info_cmds::donate_interaction(&ctx, interaction).await,
"clock" => info_cmds::clock_interaction(&ctx, interaction).await,
"remind" => reminder_cmds::set_reminder(&ctx, interaction).await,
_ => {}
}
}
}
_ => {}
}
}
@ -237,6 +363,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.get_current_user()
.map_ok(|user| user.id.as_u64().to_owned())
.await?;
let application_id = http.get_current_application_info().await?.id;
let dm_enabled = env::var("DM_ENABLED").map_or(true, |var| var == "1");
@ -278,7 +405,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND)
.add_command("restrict", &moderation_cmds::RESTRICT_COMMAND)
.add_command("timezone", &moderation_cmds::TIMEZONE_COMMAND)
.add_command("meridian", &moderation_cmds::CHANGE_MERIDIAN_COMMAND)
.add_command("prefix", &moderation_cmds::PREFIX_COMMAND)
.add_command("lang", &moderation_cmds::LANGUAGE_COMMAND)
.add_command("pause", &reminder_cmds::PAUSE_COMMAND)
@ -302,20 +428,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
| GatewayIntents::GUILDS
| GatewayIntents::GUILD_MESSAGE_REACTIONS
})
.application_id(application_id.0)
.event_handler(Handler)
.framework_arc(framework_arc.clone())
.await
.expect("Error occurred creating client");
let language_manager = Arc::new(
LanguageManager::from_compiled(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/assets/",
env!("STRINGS_FILE")
)))
.unwrap(),
);
{
let guild_data_cache = dashmap::DashMap::new();
@ -325,6 +443,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.await
.unwrap();
let language_manager = LanguageManager::from_compiled(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/assets/",
env!("STRINGS_FILE")
)))
.unwrap();
let popular_timezones = sqlx::query!(
"SELECT timezone FROM users GROUP BY timezone ORDER BY COUNT(timezone) DESC LIMIT 21"
)
@ -338,21 +463,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut data = client.data.write().await;
data.insert::<GuildDataCache>(Arc::new(guild_data_cache));
data.insert::<CurrentlyExecuting>(Arc::new(RwLock::new(HashMap::new())));
data.insert::<SQLPool>(pool);
data.insert::<PopularTimezones>(Arc::new(popular_timezones));
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));
data.insert::<FrameworkCtx>(framework_arc.clone());
data.insert::<LanguageManager>(language_manager.clone())
data.insert::<LanguageManager>(Arc::new(language_manager))
}
create_interactions(
&client.cache_and_http,
framework_arc.clone(),
language_manager.clone(),
)
.await;
if let Ok((Some(lower), Some(upper))) = env::var("SHARD_RANGE").map(|sr| {
let mut split = sr
.split(',')
@ -396,165 +514,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Ok(())
}
async fn create_interactions(
cache_http: impl CacheHttp,
framework: Arc<RegexFramework>,
lm: Arc<LanguageManager>,
) {
let http = cache_http.http();
let app_id = {
let app_info = http.get_current_application_info().await.unwrap();
app_info.id.as_u64().to_owned()
};
if let Some(guild_id) = env::var("TEST_GUILD")
.map(|i| i.parse::<u64>().ok().map(|u| GuildId(u)))
.ok()
.flatten()
{
guild_id
.create_application_command(&http, app_id, |command| {
command
.name("timezone")
.description("Select your local timezone. Do `/timezone` for more information")
.create_interaction_option(|option| {
option
.name("region")
.description("Name of your time region")
.kind(ApplicationCommandOptionType::String)
})
})
.await
.unwrap();
guild_id
.create_application_command(&http, app_id, |command| {
command
.name("lang")
.description("Select your language")
.create_interaction_option(|option| {
option
.name("language")
.description("Name of supported language you wish to use")
.kind(ApplicationCommandOptionType::String)
.required(true);
for (code, language) in lm.all_languages() {
option.add_string_choice(language, code);
}
option
})
})
.await
.unwrap();
guild_id
.create_application_command(&http, app_id, |command| {
command
.name("prefix")
.description("Select the prefix for normal commands")
.create_interaction_option(|option| {
option
.name("prefix")
.description("New prefix to use")
.kind(ApplicationCommandOptionType::String)
.required(true)
})
})
.await
.unwrap();
guild_id
.create_application_command(&http, app_id, |command| {
command
.name("info")
.description("Get information about the bot")
})
.await
.unwrap();
guild_id
.create_application_command(&http, app_id, |command| {
command
.name("donate")
.description("View information about the Patreon")
})
.await
.unwrap();
guild_id
.create_application_command(&http, app_id, |command| {
command
.name("clock")
.description("View the current time in your timezone")
})
.await
.unwrap();
guild_id
.create_application_command(&http, app_id, |command| {
command
.name("help")
.description("Get details about commands. Do `/help` to view all commands")
.create_interaction_option(|option| {
option
.name("command")
.description("Name of the command to view help for")
.kind(ApplicationCommandOptionType::String);
let mut command_set = HashSet::new();
command_set.insert("help");
command_set.insert("info");
command_set.insert("donate");
for (_, command) in &framework.commands {
if !command_set.contains(command.name) {
option.add_string_choice(&command.name, &command.name);
command_set.insert(command.name);
}
}
option
})
})
.await
.unwrap();
guild_id
.create_application_command(&http, app_id, |command| {
command
.name("remind")
.description("Set a reminder")
.create_interaction_option(|option| {
option
.name("message")
.description("Message to send with the reminder")
.kind(ApplicationCommandOptionType::String)
.required(true)
})
.create_interaction_option(|option| {
option
.name("time")
.description("Time to send the reminder")
.kind(ApplicationCommandOptionType::String)
.required(true)
})
.create_interaction_option(|option| {
option
.name("channel")
.description("Channel to send reminder to (default: this channel)")
.kind(ApplicationCommandOptionType::Channel)
.required(false)
})
})
.await
.unwrap();
}
}
pub async fn check_subscription(cache_http: impl CacheHttp, user_id: impl Into<UserId>) -> bool {
if let Some(subscription_guild) = *CNC_GUILD {
let guild_member = GuildId(subscription_guild)
@ -580,7 +539,7 @@ pub async fn check_subscription_on_message(
msg: &Message,
) -> bool {
check_subscription(&cache_http, &msg.author).await
|| if let Some(guild) = msg.guild(&cache_http).await {
|| if let Some(guild) = msg.guild(&cache_http) {
check_subscription(&cache_http, guild.owner_id).await
} else {
false

View File

@ -1,452 +0,0 @@
use serenity::{
async_trait,
http::CacheHttp,
model::{
channel::Channel,
guild::Guild,
id::{GuildId, UserId},
user::User,
},
prelude::Context,
};
use sqlx::MySqlPool;
use chrono::NaiveDateTime;
use chrono_tz::Tz;
use log::error;
use crate::{
consts::{DEFAULT_PREFIX, LOCAL_LANGUAGE, LOCAL_TIMEZONE},
GuildDataCache, SQLPool,
};
use std::sync::Arc;
use tokio::sync::RwLock;
#[async_trait]
pub trait CtxGuildData {
async fn guild_data<G: Into<GuildId> + Send + Sync>(
&self,
guild_id: G,
) -> Result<Arc<RwLock<GuildData>>, sqlx::Error>;
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String;
}
#[async_trait]
impl CtxGuildData for Context {
async fn guild_data<G: Into<GuildId> + Send + Sync>(
&self,
guild_id: G,
) -> Result<Arc<RwLock<GuildData>>, sqlx::Error> {
let guild_id = guild_id.into();
let guild = guild_id.to_guild_cached(&self.cache).await.unwrap();
let guild_cache = self
.data
.read()
.await
.get::<GuildDataCache>()
.cloned()
.unwrap();
let x = if let Some(guild_data) = guild_cache.get(&guild_id) {
Ok(guild_data.clone())
} else {
let pool = self.data.read().await.get::<SQLPool>().cloned().unwrap();
match GuildData::from_guild(guild, &pool).await {
Ok(d) => {
let lock = Arc::new(RwLock::new(d));
guild_cache.insert(guild_id, lock.clone());
Ok(lock)
}
Err(e) => Err(e),
}
};
x
}
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String {
if let Some(guild_id) = guild_id {
self.guild_data(guild_id)
.await
.unwrap()
.read()
.await
.prefix
.clone()
} else {
DEFAULT_PREFIX.clone()
}
}
}
pub struct GuildData {
pub id: u32,
pub name: Option<String>,
pub prefix: String,
}
impl GuildData {
pub async fn from_guild(guild: Guild, pool: &MySqlPool) -> Result<Self, sqlx::Error> {
let guild_id = guild.id.as_u64().to_owned();
match sqlx::query_as!(
Self,
"
SELECT id, name, prefix FROM guilds WHERE guild = ?
",
guild_id
)
.fetch_one(pool)
.await
{
Ok(mut g) => {
g.name = Some(guild.name);
Ok(g)
}
Err(sqlx::Error::RowNotFound) => {
sqlx::query!(
"
INSERT INTO guilds (guild, name, prefix) VALUES (?, ?, ?)
",
guild_id,
guild.name,
*DEFAULT_PREFIX
)
.execute(&pool.clone())
.await?;
Ok(sqlx::query_as!(
Self,
"
SELECT id, name, prefix FROM guilds WHERE guild = ?
",
guild_id
)
.fetch_one(pool)
.await?)
}
Err(e) => {
error!("Unexpected error in guild query: {:?}", e);
Err(e)
}
}
}
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE guilds SET name = ?, prefix = ? WHERE id = ?
",
self.name,
self.prefix,
self.id
)
.execute(pool)
.await
.unwrap();
}
}
pub struct ChannelData {
pub id: u32,
pub name: Option<String>,
pub nudge: i16,
pub blacklisted: bool,
pub webhook_id: Option<u64>,
pub webhook_token: Option<String>,
pub paused: bool,
pub paused_until: Option<NaiveDateTime>,
}
impl ChannelData {
pub async fn from_channel(
channel: Channel,
pool: &MySqlPool,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let channel_id = channel.id().as_u64().to_owned();
if let Ok(c) = sqlx::query_as_unchecked!(Self,
"
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
", channel_id)
.fetch_one(pool)
.await {
Ok(c)
}
else {
let props = channel.guild().map(|g| (g.guild_id.as_u64().to_owned(), g.name));
let (guild_id, channel_name) = if let Some((a, b)) = props {
(Some(a), Some(b))
} else {
(None, None)
};
sqlx::query!(
"
INSERT IGNORE INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM guilds WHERE guild = ?))
", channel_id, channel_name, guild_id)
.execute(&pool.clone())
.await?;
Ok(sqlx::query_as_unchecked!(Self,
"
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
", channel_id)
.fetch_one(pool)
.await?)
}
}
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE channels SET name = ?, nudge = ?, blacklisted = ?, webhook_id = ?, webhook_token = ?, paused = ?, paused_until = ? WHERE id = ?
", self.name, self.nudge, self.blacklisted, self.webhook_id, self.webhook_token, self.paused, self.paused_until, self.id)
.execute(pool)
.await.unwrap();
}
}
pub struct UserData {
pub id: u32,
pub user: u64,
pub name: String,
pub dm_channel: u32,
pub language: String,
pub timezone: String,
pub meridian_time: bool,
}
pub struct MeridianType(bool);
impl MeridianType {
pub fn fmt_str(&self) -> &str {
if self.0 {
"%Y-%m-%d %I:%M:%S %p"
} else {
"%Y-%m-%d %H:%M:%S"
}
}
pub fn fmt_str_short(&self) -> &str {
if self.0 {
"%I:%M %p"
} else {
"%H:%M"
}
}
}
impl UserData {
pub async fn language_of<U>(user: U, pool: &MySqlPool) -> String
where
U: Into<UserId>,
{
let user_id = user.into().as_u64().to_owned();
match sqlx::query!(
"
SELECT language FROM users WHERE user = ?
",
user_id
)
.fetch_one(pool)
.await
{
Ok(r) => r.language,
Err(_) => LOCAL_LANGUAGE.clone(),
}
}
pub async fn timezone_of<U>(user: U, pool: &MySqlPool) -> Tz
where
U: Into<UserId>,
{
let user_id = user.into().as_u64().to_owned();
match sqlx::query!(
"
SELECT timezone FROM users WHERE user = ?
",
user_id
)
.fetch_one(pool)
.await
{
Ok(r) => r.timezone,
Err(_) => LOCAL_TIMEZONE.clone(),
}
.parse()
.unwrap()
}
pub async fn meridian_of<U>(user: U, pool: &MySqlPool) -> MeridianType
where
U: Into<UserId>,
{
let user_id = user.into().as_u64().to_owned();
match sqlx::query!(
"
SELECT meridian_time FROM users WHERE user = ?
",
user_id
)
.fetch_one(pool)
.await
{
Ok(r) => MeridianType(r.meridian_time != 0),
Err(_) => MeridianType(false),
}
}
pub async fn from_user(
user: &User,
ctx: impl CacheHttp,
pool: &MySqlPool,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let user_id = user.id.as_u64().to_owned();
match sqlx::query_as_unchecked!(
Self,
"
SELECT id, user, name, dm_channel, IF(language IS NULL, ?, language) AS language, IF(timezone IS NULL, ?, timezone) AS timezone, meridian_time FROM users WHERE user = ?
",
*LOCAL_LANGUAGE, *LOCAL_TIMEZONE, user_id
)
.fetch_one(pool)
.await
{
Ok(c) => Ok(c),
Err(sqlx::Error::RowNotFound) => {
let dm_channel = user.create_dm_channel(ctx).await?;
let dm_id = dm_channel.id.as_u64().to_owned();
let pool_c = pool.clone();
sqlx::query!(
"
INSERT IGNORE INTO channels (channel) VALUES (?)
",
dm_id
)
.execute(&pool_c)
.await?;
sqlx::query!(
"
INSERT INTO users (user, name, dm_channel, language, timezone) VALUES (?, ?, (SELECT id FROM channels WHERE channel = ?), ?, ?)
", user_id, user.name, dm_id, *LOCAL_LANGUAGE, *LOCAL_TIMEZONE)
.execute(&pool_c)
.await?;
Ok(sqlx::query_as_unchecked!(
Self,
"
SELECT id, user, name, dm_channel, language, timezone, meridian_time FROM users WHERE user = ?
",
user_id
)
.fetch_one(pool)
.await?)
}
Err(e) => {
error!("Error querying for user: {:?}", e);
Err(Box::new(e))
},
}
}
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE users SET name = ?, language = ?, timezone = ?, meridian_time = ? WHERE id = ?
",
self.name,
self.language,
self.timezone,
self.meridian_time,
self.id
)
.execute(pool)
.await
.unwrap();
}
pub fn timezone(&self) -> Tz {
self.timezone.parse().unwrap()
}
pub fn meridian(&self) -> MeridianType {
MeridianType(self.meridian_time)
}
}
pub struct Timer {
pub name: String,
pub start_time: NaiveDateTime,
pub owner: u64,
}
impl Timer {
pub async fn from_owner(owner: u64, pool: &MySqlPool) -> Vec<Self> {
sqlx::query_as_unchecked!(
Timer,
"
SELECT name, start_time, owner FROM timers WHERE owner = ?
",
owner
)
.fetch_all(pool)
.await
.unwrap()
}
pub async fn count_from_owner(owner: u64, pool: &MySqlPool) -> u32 {
sqlx::query!(
"
SELECT COUNT(1) as count FROM timers WHERE owner = ?
",
owner
)
.fetch_one(pool)
.await
.unwrap()
.count as u32
}
pub async fn create(name: &str, owner: u64, pool: &MySqlPool) {
sqlx::query!(
"
INSERT INTO timers (name, owner) VALUES (?, ?)
",
name,
owner
)
.execute(pool)
.await
.unwrap();
}
}

View File

@ -0,0 +1,67 @@
use serenity::model::channel::Channel;
use sqlx::MySqlPool;
use chrono::NaiveDateTime;
pub struct ChannelData {
pub id: u32,
pub name: Option<String>,
pub nudge: i16,
pub blacklisted: bool,
pub webhook_id: Option<u64>,
pub webhook_token: Option<String>,
pub paused: bool,
pub paused_until: Option<NaiveDateTime>,
}
impl ChannelData {
pub async fn from_channel(
channel: Channel,
pool: &MySqlPool,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let channel_id = channel.id().as_u64().to_owned();
if let Ok(c) = sqlx::query_as_unchecked!(Self,
"
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
", channel_id)
.fetch_one(pool)
.await {
Ok(c)
}
else {
let props = channel.guild().map(|g| (g.guild_id.as_u64().to_owned(), g.name));
let (guild_id, channel_name) = if let Some((a, b)) = props {
(Some(a), Some(b))
} else {
(None, None)
};
sqlx::query!(
"
INSERT IGNORE INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM guilds WHERE guild = ?))
", channel_id, channel_name, guild_id)
.execute(&pool.clone())
.await?;
Ok(sqlx::query_as_unchecked!(Self,
"
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
", channel_id)
.fetch_one(pool)
.await?)
}
}
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE channels SET name = ?, nudge = ?, blacklisted = ?, webhook_id = ?, webhook_token = ?, paused = ?, paused_until = ? WHERE id = ?
", self.name, self.nudge, self.blacklisted, self.webhook_id, self.webhook_token, self.paused, self.paused_until, self.id)
.execute(pool)
.await.unwrap();
}
}

79
src/models/guild_data.rs Normal file
View File

@ -0,0 +1,79 @@
use serenity::model::guild::Guild;
use sqlx::MySqlPool;
use log::error;
use crate::consts::DEFAULT_PREFIX;
pub struct GuildData {
pub id: u32,
pub name: Option<String>,
pub prefix: String,
}
impl GuildData {
pub async fn from_guild(guild: Guild, pool: &MySqlPool) -> Result<Self, sqlx::Error> {
let guild_id = guild.id.as_u64().to_owned();
match sqlx::query_as!(
Self,
"
SELECT id, name, prefix FROM guilds WHERE guild = ?
",
guild_id
)
.fetch_one(pool)
.await
{
Ok(mut g) => {
g.name = Some(guild.name);
Ok(g)
}
Err(sqlx::Error::RowNotFound) => {
sqlx::query!(
"
INSERT INTO guilds (guild, name, prefix) VALUES (?, ?, ?)
",
guild_id,
guild.name,
*DEFAULT_PREFIX
)
.execute(&pool.clone())
.await?;
Ok(sqlx::query_as!(
Self,
"
SELECT id, name, prefix FROM guilds WHERE guild = ?
",
guild_id
)
.fetch_one(pool)
.await?)
}
Err(e) => {
error!("Unexpected error in guild query: {:?}", e);
Err(e)
}
}
}
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE guilds SET name = ?, prefix = ? WHERE id = ?
",
self.name,
self.prefix,
self.id
)
.execute(pool)
.await
.unwrap();
}
}

78
src/models/mod.rs Normal file
View File

@ -0,0 +1,78 @@
pub mod channel_data;
pub mod guild_data;
pub mod reminder;
pub mod timer;
pub mod user_data;
use serenity::{async_trait, model::id::GuildId, prelude::Context};
use crate::{consts::DEFAULT_PREFIX, GuildDataCache, SQLPool};
use guild_data::GuildData;
use std::sync::Arc;
use tokio::sync::RwLock;
#[async_trait]
pub trait CtxGuildData {
async fn guild_data<G: Into<GuildId> + Send + Sync>(
&self,
guild_id: G,
) -> Result<Arc<RwLock<GuildData>>, sqlx::Error>;
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String;
}
#[async_trait]
impl CtxGuildData for Context {
async fn guild_data<G: Into<GuildId> + Send + Sync>(
&self,
guild_id: G,
) -> Result<Arc<RwLock<GuildData>>, sqlx::Error> {
let guild_id = guild_id.into();
let guild = guild_id.to_guild_cached(&self.cache).unwrap();
let guild_cache = self
.data
.read()
.await
.get::<GuildDataCache>()
.cloned()
.unwrap();
let x = if let Some(guild_data) = guild_cache.get(&guild_id) {
Ok(guild_data.clone())
} else {
let pool = self.data.read().await.get::<SQLPool>().cloned().unwrap();
match GuildData::from_guild(guild, &pool).await {
Ok(d) => {
let lock = Arc::new(RwLock::new(d));
guild_cache.insert(guild_id, lock.clone());
Ok(lock)
}
Err(e) => Err(e),
}
};
x
}
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String {
if let Some(guild_id) = guild_id {
self.guild_data(guild_id)
.await
.unwrap()
.read()
.await
.prefix
.clone()
} else {
DEFAULT_PREFIX.clone()
}
}
}

346
src/models/reminder.rs Normal file
View File

@ -0,0 +1,346 @@
use serenity::{
client::Context,
model::id::{ChannelId, GuildId, UserId},
};
use chrono::NaiveDateTime;
use crate::{
consts::{DAY, HOUR, MINUTE, REGEX_CHANNEL},
SQLPool,
};
use num_integer::Integer;
fn longhand_displacement(seconds: u64) -> String {
let (days, seconds) = seconds.div_rem(&DAY);
let (hours, seconds) = seconds.div_rem(&HOUR);
let (minutes, seconds) = seconds.div_rem(&MINUTE);
let mut sections = vec![];
for (var, name) in [days, hours, minutes, seconds]
.iter()
.zip(["days", "hours", "minutes", "seconds"].iter())
{
if *var > 0 {
sections.push(format!("{} {}", var, name));
}
}
sections.join(", ")
}
#[derive(Debug)]
pub struct Reminder {
pub id: u32,
pub uid: String,
pub channel: u64,
pub utc_time: NaiveDateTime,
pub interval_seconds: Option<u32>,
pub expires: Option<NaiveDateTime>,
pub enabled: bool,
pub content: String,
pub embed_description: String,
pub set_by: Option<u64>,
}
impl Reminder {
pub async fn from_uid(ctx: &Context, uid: String) -> Option<Self> {
let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap();
sqlx::query_as_unchecked!(
Self,
"
SELECT
reminders.id,
reminders.uid,
channels.channel,
reminders.utc_time,
reminders.interval_seconds,
reminders.expires,
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
reminders.uid = ?
",
uid
)
.fetch_one(&pool)
.await
.ok()
}
pub async fn from_channel<C: Into<ChannelId>>(
ctx: &Context,
channel_id: C,
flags: &LookFlags,
) -> Vec<Self> {
let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap();
let enabled = if flags.show_disabled { "0,1" } else { "1" };
let channel_id = channel_id.into();
sqlx::query_as_unchecked!(
Self,
"
SELECT
reminders.id,
reminders.uid,
channels.channel,
reminders.utc_time,
reminders.interval_seconds,
reminders.expires,
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
channels.channel = ? AND
FIND_IN_SET(reminders.enabled, ?)
ORDER BY
reminders.utc_time
LIMIT
?
",
channel_id.as_u64(),
enabled,
flags.limit
)
.fetch_all(&pool)
.await
.unwrap()
}
pub async fn from_guild(ctx: &Context, guild_id: Option<GuildId>, user: UserId) -> Vec<Self> {
let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap();
if let Some(guild_id) = guild_id {
let guild_opt = guild_id.to_guild_cached(&ctx);
if let Some(guild) = guild_opt {
let channels = guild
.channels
.keys()
.into_iter()
.map(|k| k.as_u64().to_string())
.collect::<Vec<String>>()
.join(",");
sqlx::query_as_unchecked!(
Self,
"
SELECT
reminders.id,
reminders.uid,
channels.channel,
reminders.utc_time,
reminders.interval_seconds,
reminders.expires,
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
FROM
reminders
LEFT JOIN
channels
ON
channels.id = reminders.channel_id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
FIND_IN_SET(channels.channel, ?)
",
channels
)
.fetch_all(&pool)
.await
} else {
sqlx::query_as_unchecked!(
Self,
"
SELECT
reminders.id,
reminders.uid,
channels.channel,
reminders.utc_time,
reminders.interval_seconds,
reminders.expires,
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
FROM
reminders
LEFT JOIN
channels
ON
channels.id = reminders.channel_id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
channels.guild_id = (SELECT id FROM guilds WHERE guild = ?)
",
guild_id.as_u64()
)
.fetch_all(&pool)
.await
}
} else {
sqlx::query_as_unchecked!(
Self,
"
SELECT
reminders.id,
reminders.uid,
channels.channel,
reminders.utc_time,
reminders.interval_seconds,
reminders.expires,
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
FROM
reminders
INNER JOIN
channels
ON
channels.id = reminders.channel_id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
channels.id = (SELECT dm_channel FROM users WHERE user = ?)
",
user.as_u64()
)
.fetch_all(&pool)
.await
}
.unwrap()
}
pub fn display_content(&self) -> &str {
if self.content.is_empty() {
&self.embed_description
} else {
&self.content
}
}
pub fn display(&self, flags: &LookFlags, inter: &str) -> String {
let time_display = match flags.time_display {
TimeDisplayType::Absolute => format!("<t:{}>", self.utc_time.timestamp()),
TimeDisplayType::Relative => format!("<t:{}:R>", self.utc_time.timestamp()),
};
if let Some(interval) = self.interval_seconds {
format!(
"'{}' *{}* **{}**, repeating every **{}** (set by {})",
self.display_content(),
&inter,
time_display,
longhand_displacement(interval as u64),
self.set_by
.map(|i| format!("<@{}>", i))
.unwrap_or_else(|| "unknown".to_string())
)
} else {
format!(
"'{}' *{}* **{}** (set by {})",
self.display_content(),
&inter,
time_display,
self.set_by
.map(|i| format!("<@{}>", i))
.unwrap_or_else(|| "unknown".to_string())
)
}
}
}
enum TimeDisplayType {
Absolute,
Relative,
}
pub struct LookFlags {
pub limit: u16,
pub show_disabled: bool,
pub channel_id: Option<ChannelId>,
time_display: TimeDisplayType,
}
impl Default for LookFlags {
fn default() -> Self {
Self {
limit: u16::MAX,
show_disabled: true,
channel_id: None,
time_display: TimeDisplayType::Relative,
}
}
}
impl LookFlags {
pub fn from_string(args: &str) -> Self {
let mut new_flags: Self = Default::default();
for arg in args.split(' ') {
match arg {
"enabled" => {
new_flags.show_disabled = false;
}
"time" => {
new_flags.time_display = TimeDisplayType::Absolute;
}
param => {
if let Ok(val) = param.parse::<u16>() {
new_flags.limit = val;
} else if let Some(channel) = REGEX_CHANNEL
.captures(&arg)
.map(|cap| cap.get(1))
.flatten()
.map(|c| c.as_str().parse::<u64>().unwrap())
{
new_flags.channel_id = Some(ChannelId(channel));
}
}
}
}
new_flags
}
}

50
src/models/timer.rs Normal file
View File

@ -0,0 +1,50 @@
use sqlx::MySqlPool;
use chrono::NaiveDateTime;
pub struct Timer {
pub name: String,
pub start_time: NaiveDateTime,
pub owner: u64,
}
impl Timer {
pub async fn from_owner(owner: u64, pool: &MySqlPool) -> Vec<Self> {
sqlx::query_as_unchecked!(
Timer,
"
SELECT name, start_time, owner FROM timers WHERE owner = ?
",
owner
)
.fetch_all(pool)
.await
.unwrap()
}
pub async fn count_from_owner(owner: u64, pool: &MySqlPool) -> u32 {
sqlx::query!(
"
SELECT COUNT(1) as count FROM timers WHERE owner = ?
",
owner
)
.fetch_one(pool)
.await
.unwrap()
.count as u32
}
pub async fn create(name: &str, owner: u64, pool: &MySqlPool) {
sqlx::query!(
"
INSERT INTO timers (name, owner) VALUES (?, ?)
",
name,
owner
)
.execute(pool)
.await
.unwrap();
}
}

146
src/models/user_data.rs Normal file
View File

@ -0,0 +1,146 @@
use serenity::{
http::CacheHttp,
model::{id::UserId, user::User},
};
use sqlx::MySqlPool;
use chrono_tz::Tz;
use log::error;
use crate::consts::{LOCAL_LANGUAGE, LOCAL_TIMEZONE};
pub struct UserData {
pub id: u32,
pub user: u64,
pub name: String,
pub dm_channel: u32,
pub language: String,
pub timezone: String,
}
impl UserData {
pub async fn language_of<U>(user: U, pool: &MySqlPool) -> String
where
U: Into<UserId>,
{
let user_id = user.into().as_u64().to_owned();
match sqlx::query!(
"
SELECT language FROM users WHERE user = ?
",
user_id
)
.fetch_one(pool)
.await
{
Ok(r) => r.language,
Err(_) => LOCAL_LANGUAGE.clone(),
}
}
pub async fn timezone_of<U>(user: U, pool: &MySqlPool) -> Tz
where
U: Into<UserId>,
{
let user_id = user.into().as_u64().to_owned();
match sqlx::query!(
"
SELECT timezone FROM users WHERE user = ?
",
user_id
)
.fetch_one(pool)
.await
{
Ok(r) => r.timezone,
Err(_) => LOCAL_TIMEZONE.clone(),
}
.parse()
.unwrap()
}
pub async fn from_user(
user: &User,
ctx: impl CacheHttp,
pool: &MySqlPool,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let user_id = user.id.as_u64().to_owned();
match sqlx::query_as_unchecked!(
Self,
"
SELECT id, user, name, dm_channel, IF(language IS NULL, ?, language) AS language, IF(timezone IS NULL, ?, timezone) AS timezone FROM users WHERE user = ?
",
*LOCAL_LANGUAGE, *LOCAL_TIMEZONE, user_id
)
.fetch_one(pool)
.await
{
Ok(c) => Ok(c),
Err(sqlx::Error::RowNotFound) => {
let dm_channel = user.create_dm_channel(ctx).await?;
let dm_id = dm_channel.id.as_u64().to_owned();
let pool_c = pool.clone();
sqlx::query!(
"
INSERT IGNORE INTO channels (channel) VALUES (?)
",
dm_id
)
.execute(&pool_c)
.await?;
sqlx::query!(
"
INSERT INTO users (user, name, dm_channel, language, timezone) VALUES (?, ?, (SELECT id FROM channels WHERE channel = ?), ?, ?)
", user_id, user.name, dm_id, *LOCAL_LANGUAGE, *LOCAL_TIMEZONE)
.execute(&pool_c)
.await?;
Ok(sqlx::query_as_unchecked!(
Self,
"
SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ?
",
user_id
)
.fetch_one(pool)
.await?)
}
Err(e) => {
error!("Error querying for user: {:?}", e);
Err(Box::new(e))
},
}
}
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ?
",
self.name,
self.language,
self.timezone,
self.id
)
.execute(pool)
.await
.unwrap();
}
pub fn timezone(&self) -> Tz {
self.timezone.parse().unwrap()
}
}

View File

@ -32,7 +32,7 @@ enum ParseType {
}
pub struct TimeParser {
pub timezone: Tz,
timezone: Tz,
inverted: bool,
time_string: String,
parse_type: ParseType,
@ -112,7 +112,7 @@ impl TimeParser {
DateTime::with_second,
]) {
time = setter(&time, t.parse().map_err(|_| InvalidTime::ParseErrorHMS)?)
.map_or_else(|| Err(InvalidTime::ParseErrorHMS), |inner| Ok(inner))?;
.map_or_else(|| Err(InvalidTime::ParseErrorHMS), Ok)?;
}
if let Some(dmy) = segments.next() {
@ -128,7 +128,7 @@ impl TimeParser {
{
if let Some(t) = t {
time = setter(&time, t.parse().map_err(|_| InvalidTime::ParseErrorDMY)?)
.map_or_else(|| Err(InvalidTime::ParseErrorDMY), |inner| Ok(inner))?;
.map_or_else(|| Err(InvalidTime::ParseErrorDMY), Ok)?;
}
}
@ -136,7 +136,7 @@ impl TimeParser {
if year.len() == 4 {
time = time
.with_year(year.parse().map_err(|_| InvalidTime::ParseErrorDMY)?)
.map_or_else(|| Err(InvalidTime::ParseErrorDMY), |inner| Ok(inner))?;
.map_or_else(|| Err(InvalidTime::ParseErrorDMY), Ok)?;
} else if year.len() == 2 {
time = time
.with_year(
@ -144,9 +144,9 @@ impl TimeParser {
.parse()
.map_err(|_| InvalidTime::ParseErrorDMY)?,
)
.map_or_else(|| Err(InvalidTime::ParseErrorDMY), |inner| Ok(inner))?;
.map_or_else(|| Err(InvalidTime::ParseErrorDMY), Ok)?;
} else {
Err(InvalidTime::ParseErrorDMY)?;
return Err(InvalidTime::ParseErrorDMY);
}
}
}
@ -157,10 +157,10 @@ impl TimeParser {
fn process_displacement(&self) -> Result<i64, InvalidTime> {
let mut current_buffer = "0".to_string();
let mut seconds = 0 as i64;
let mut minutes = 0 as i64;
let mut hours = 0 as i64;
let mut days = 0 as i64;
let mut seconds = 0_i64;
let mut minutes = 0_i64;
let mut hours = 0_i64;
let mut days = 0_i64;
for character in self.time_string.chars() {
match character {
@ -205,7 +205,7 @@ impl TimeParser {
}
}
pub(crate) async fn natural_parser(time: &str, timezone: &str) -> Option<i64> {
pub async fn natural_parser(time: &str, timezone: &str) -> Option<i64> {
Command::new(&*PYTHON_LOCATION)
.arg("-c")
.arg(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/dp.py")))