Skip to content

Commit

Permalink
Patreon updater (#150)
Browse files Browse the repository at this point in the history
* working patreon background loop

* role updater
  • Loading branch information
circuitsacul authored Jan 18, 2023
1 parent 91225a9 commit ffd26dd
Show file tree
Hide file tree
Showing 9 changed files with 560 additions and 2 deletions.
158 changes: 158 additions & 0 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,38 @@
},
"query": "UPDATE overrides SET name=$1 WHERE name=$2 AND guild_id=$3 RETURNING *"
},
"29f33bd6db6938eafebc842e5a8ccdc22c67299762ba828c12f4da6f3b28fb69": {
"describe": {
"columns": [
{
"name": "patreon_id",
"ordinal": 0,
"type_info": "Varchar"
},
{
"name": "discord_id",
"ordinal": 1,
"type_info": "Int8"
},
{
"name": "last_patreon_total_cents",
"ordinal": 2,
"type_info": "Int8"
}
],
"nullable": [
false,
true,
false
],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "SELECT * FROM patrons WHERE patreon_id=$1"
},
"2a46ead99e78a1709b76021c12fe29efbd86668d704b9701f4d5af8d05054113": {
"describe": {
"columns": [
Expand Down Expand Up @@ -2616,6 +2648,19 @@
},
"query": "SELECT * FROM starboards WHERE id=$1"
},
"76f0d2873e1e40c5936f08cb165bd7eb0e7fa26b422be37c747a8ce8d93495ba": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8",
"Text"
]
}
},
"query": "UPDATE patrons SET last_patreon_total_cents=$1 WHERE patreon_id=$2"
},
"7e14b14e400319e69ca7b7a581257ee46e1b60ee05872f90474bc3c107a47e08": {
"describe": {
"columns": [
Expand Down Expand Up @@ -3016,6 +3061,19 @@
},
"query": "SELECT count(*) FROM posroles WHERE guild_id=$1"
},
"86a6918e81e9ade16762da1d29ea1e031aaf97fca5fcbbc90e48e6fa568bd6fd": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8",
"Text"
]
}
},
"query": "UPDATE patrons SET discord_id=$1 WHERE patreon_id=$2"
},
"88357902cae7c5ecc5625d473a99b55a5acd4323de003c6fa89afd9543c7fe25": {
"describe": {
"columns": [
Expand Down Expand Up @@ -3088,6 +3146,38 @@
},
"query": "DELETE FROM overrides WHERE guild_id=$1 AND name=$2 RETURNING *"
},
"8922d567b5bf4617bde6089db2954a7bd25227fe7b2c10ff877f4975258794e0": {
"describe": {
"columns": [
{
"name": "patreon_id",
"ordinal": 0,
"type_info": "Varchar"
},
{
"name": "discord_id",
"ordinal": 1,
"type_info": "Int8"
},
{
"name": "last_patreon_total_cents",
"ordinal": 2,
"type_info": "Int8"
}
],
"nullable": [
false,
true,
false
],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "SELECT * FROM patrons WHERE discord_id=$1"
},
"894b0279e3aa8ee0f633c1773d0c253313a0b2f5fb7abbf2f7f3723e5967d217": {
"describe": {
"columns": [
Expand Down Expand Up @@ -3347,6 +3437,19 @@
},
"query": "UPDATE messages SET forced_to=$1 WHERE message_id=$2 RETURNING *"
},
"9d724f18b7a680d92c5d182379fb111f1d1d4da55650e8ceeee18ed8bba01e49": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int4",
"Int8"
]
}
},
"query": "UPDATE users SET credits = credits + $1 WHERE user_id=$2"
},
"9dad93f96c1564677617a97044b59d8c62f5f1eca0de3ad975843575e2835b31": {
"describe": {
"columns": [
Expand Down Expand Up @@ -3388,6 +3491,19 @@
},
"query": "INSERT INTO starboard_messages\n (message_id, starboard_id, starboard_message_id, last_known_point_count)\n VALUES ($1, $2, $3, $4)\n RETURNING *"
},
"9f703e380d7482765beb5b56ce9681344cae0dca572a74f0c67204407a60baf7": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int2",
"Int8"
]
}
},
"query": "UPDATE users SET patreon_status=$1 WHERE user_id=$2"
},
"ae21651e28e5e8de6d8d5889653c3a0609a56f4db7879652447c01c019cab317": {
"describe": {
"columns": [
Expand Down Expand Up @@ -4169,6 +4285,48 @@
},
"query": "SELECT COUNT(*) as count FROM votes WHERE message_id=$1 AND starboard_id=$2\n AND is_downvote=false"
},
"ce5911bc8c121ea9fb2066022bda191d93ba8b6409dc43ad11ccf18183dffadc": {
"describe": {
"columns": [
{
"name": "user_id",
"ordinal": 0,
"type_info": "Int8"
},
{
"name": "is_bot",
"ordinal": 1,
"type_info": "Bool"
},
{
"name": "credits",
"ordinal": 2,
"type_info": "Int4"
},
{
"name": "donated_cents",
"ordinal": 3,
"type_info": "Int8"
},
{
"name": "patreon_status",
"ordinal": 4,
"type_info": "Int2"
}
],
"nullable": [
false,
false,
false,
false,
false
],
"parameters": {
"Left": []
}
},
"query": "SELECT * FROM users WHERE patreon_status!=0 OR donated_cents!=0"
},
"d199f08a2ada23a1b5bf29fc97a3f14fa479f58b382ba51f264aa6234234f42b": {
"describe": {
"columns": [
Expand Down
13 changes: 13 additions & 0 deletions src/client/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ use std::env;

pub struct Config {
pub token: String,
pub patreon_token: Option<String>,
pub sentry: Option<String>,
pub shards: u64,
pub db_url: String,
pub error_channel: Option<u64>,
pub development: bool,
pub owner_ids: Vec<u64>,
pub bot_id: u64,
pub main_guild: Option<u64>,
pub patron_role: Option<u64>,
pub supporter_role: Option<u64>,
}

impl Config {
Expand All @@ -19,6 +23,7 @@ impl Config {
Err(why) => eprintln!("Failed to load .env: {why}"),
};
let token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN not set");
let patreon_token = env::var("PATREON_TOKEN").ok();
let sentry = env::var("SENTRY_URL").ok();
let shards = env::var("SHARDS")
.unwrap_or_else(|_| "1".to_string())
Expand All @@ -42,15 +47,23 @@ impl Config {
.parse()
.expect("Invalid BOT_ID");

let main_guild = env::var("MAIN_GUILD").ok().map(|v| v.parse().unwrap());
let patron_role = env::var("PATRON_ROLE").ok().map(|v| v.parse().unwrap());
let supporter_role = env::var("SUPPORTER_ROLE").ok().map(|v| v.parse().unwrap());

Config {
token,
patreon_token,
sentry,
shards,
db_url,
error_channel,
development,
owner_ids: owner_ids.unwrap_or_default(),
bot_id,
main_guild,
patron_role,
supporter_role,
}
}
}
9 changes: 8 additions & 1 deletion src/client/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ use twilight_gateway::cluster::Events;

use crate::{
client::bot::StarboardBot,
core::{posroles::loop_update_posroles, premium::expire::loop_expire_premium},
core::{
posroles::loop_update_posroles,
premium::{
expire::loop_expire_premium, patreon::patreon_loop, roles::loop_update_supporter_roles,
},
},
events::handle_event,
};

Expand Down Expand Up @@ -49,6 +54,8 @@ pub async fn run(mut events: Events, bot: StarboardBot) {
// start background tasks
tokio::spawn(loop_update_posroles(bot.clone()));
tokio::spawn(loop_expire_premium(bot.clone()));
tokio::spawn(patreon_loop(bot.clone()));
tokio::spawn(loop_update_supporter_roles(bot.clone()));

// handle events
while let Some((shard_id, event)) = events.next().await {
Expand Down
2 changes: 2 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub const REVIEW_URL: &str = "https://top.gg/bot/700796664276844612#reviews";
// Tasks
pub const UPDATE_PRS_DELAY: Duration = Duration::from_secs(60 * 60);
pub const CHECK_EXPIRED_PREMIUM: Duration = Duration::from_secs(60 * 60);
pub const UPDATE_PATREON_DELAY: Duration = Duration::from_secs(60);
pub const UPDATE_SUPPORTER_ROLES_DELAY: Duration = Duration::from_secs(60);

// Cache size
pub const MAX_MESSAGES: u32 = 10_000;
Expand Down
2 changes: 2 additions & 0 deletions src/core/premium/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod expire;
pub mod is_premium;
pub mod locks;
pub mod patreon;
pub mod redeem;
pub mod roles;
Loading

0 comments on commit ffd26dd

Please sign in to comment.