-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
scripty_bot_utils/src/background_tasks/tasks/bot_vote_reminder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use std::{fmt, time::Duration}; | ||
|
||
use serenity::{ | ||
all::UserId, | ||
builder::{CreateEmbed, CreateMessage}, | ||
client::Context as SerenityContext, | ||
futures::StreamExt, | ||
Check warning on line 7 in scripty_bot_utils/src/background_tasks/tasks/bot_vote_reminder.rs GitHub Actions / Clippy Outputunused import: `futures::StreamExt`
|
||
}; | ||
|
||
use crate::{background_tasks::core::BackgroundTask, Error}; | ||
|
||
/// Sends vote reminders to users every minute. | ||
pub struct VoteReminderTask { | ||
ctx: SerenityContext, | ||
} | ||
|
||
#[async_trait] | ||
impl BackgroundTask for VoteReminderTask { | ||
async fn init(ctx: SerenityContext) -> Result<Self, Error> { | ||
Ok(Self { ctx }) | ||
} | ||
|
||
fn interval(&mut self) -> Duration { | ||
Duration::from_secs(60) | ||
} | ||
|
||
async fn run(&mut self) { | ||
let mut vote_query = sqlx::query!( | ||
"DELETE FROM vote_reminders WHERE next_reminder < NOW() RETURNING user_id, site_id, \ | ||
next_reminder" | ||
) | ||
.fetch_many(scripty_db::get_db()); | ||
|
||
while let Some(user) = vote_query.next().await { | ||
let user = match user.map(|u| u.right()) { | ||
Ok(Some(user)) => user, | ||
Ok(None) => { | ||
error!("got no user from vote reminder query"); | ||
continue; | ||
} | ||
Err(err) => { | ||
error!("failed to get vote reminder: {}", err); | ||
continue; | ||
} | ||
}; | ||
let site: VoteList = user.site_id.into(); | ||
let user_id = user.user_id as u64; | ||
let reminder_unix_ts = user.next_reminder.assume_utc().unix_timestamp(); | ||
|
||
let msg = | ||
CreateMessage::new().embed(CreateEmbed::new().title("Vote reminder").description( | ||
format!( | ||
"You can vote for Scripty on {} again, as of <t:{}:R>. You can do so at \ | ||
{}. Thanks for your support!", | ||
site, | ||
reminder_unix_ts, | ||
site.vote_url() | ||
), | ||
)); | ||
let ctx2 = self.ctx.clone(); | ||
tokio::spawn(async move { | ||
let res = match UserId::new(user_id).create_dm_channel(&ctx2.http).await { | ||
Ok(channel) => channel.send_message(&ctx2.http, msg).await.map(|_| ()), | ||
Err(e) => Err(e), | ||
}; | ||
if let Err(e) = res { | ||
error!("failed to send vote reminder: {}", e); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
fn timeout(&mut self) -> Option<Duration> { | ||
Some(Duration::from_secs(5)) | ||
} | ||
} | ||
|
||
pub enum VoteList { | ||
TopGg = 1, | ||
DiscordServicesNet = 2, | ||
WumpusStore = 3, | ||
} | ||
impl From<i16> for VoteList { | ||
fn from(i: i16) -> Self { | ||
match i { | ||
1 => Self::TopGg, | ||
2 => Self::DiscordServicesNet, | ||
3 => Self::WumpusStore, | ||
_ => panic!("invalid vote list id"), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for VoteList { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::TopGg => write!(f, "top.gg"), | ||
Self::DiscordServicesNet => write!(f, "discordservices.net"), | ||
Self::WumpusStore => write!(f, "wumpus.store"), | ||
} | ||
} | ||
} | ||
|
||
impl VoteList { | ||
pub fn vote_url(&self) -> &'static str { | ||
match self { | ||
Self::TopGg => "https://top.gg/bot/699453633624064849/vote", | ||
Self::DiscordServicesNet => "https://discordservices.net/bot/scripty", | ||
Self::WumpusStore => "https://wumpus.store/bot/811652199100317726/vote", | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
mod basic_stats_update; | ||
mod bot_list_poster; | ||
mod bot_vote_reminder; | ||
mod cmd_latency_clear; | ||
mod prometheus_latency_update; | ||
mod status_update; | ||
|
||
pub use basic_stats_update::*; | ||
pub use bot_list_poster::*; | ||
pub use bot_vote_reminder::*; | ||
pub use cmd_latency_clear::*; | ||
pub use prometheus_latency_update::*; | ||
pub use status_update::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::{Context, Error}; | ||
|
||
/// Opt in or out of vote reminders | ||
#[poise::command(prefix_command, slash_command)] | ||
pub async fn vote_reminder(ctx: Context<'_>, enabled: bool) -> Result<(), Error> { | ||
let resolved_language = | ||
scripty_i18n::get_resolved_language(ctx.author().id.get(), ctx.guild_id().map(|g| g.get())) | ||
.await; | ||
|
||
let db = scripty_db::get_db(); | ||
sqlx::query!( | ||
"INSERT INTO users (user_id) VALUES ($1) ON CONFLICT ON CONSTRAINT users_pkey DO NOTHING", | ||
ctx.author().id.get() as i64 | ||
) | ||
.execute(db) | ||
.await?; | ||
sqlx::query!( | ||
"UPDATE users SET vote_reminder_disabled = $1 WHERE user_id = $2", | ||
enabled, | ||
ctx.author().id.get() as i64 | ||
) | ||
.execute(db) | ||
.await?; | ||
|
||
ctx.say(format_message!( | ||
resolved_language, | ||
if enabled { | ||
"vote-reminders-enabled" | ||
} else { | ||
"vote-reminders-disabled" | ||
} | ||
)) | ||
.await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters