From af9a97bdbda9da248da2357acb2bd5548e84e7b4 Mon Sep 17 00:00:00 2001 From: Papa Date: Tue, 7 Nov 2023 22:10:57 -0700 Subject: [PATCH] <3 --- config.example.js | 10 +++- packages/runtime-worker/src/money-manager.ts | 54 ++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/config.example.js b/config.example.js index 4a5097f0..981b7cf0 100644 --- a/config.example.js +++ b/config.example.js @@ -15,6 +15,8 @@ const config = { sql_user: "", weeb_identifier: "", client_id: "", + // The ID of the user Amanda loves and should send messages to every interval (inspirational is what's used in Amanda official. Amanda loves you, Troy <3) + amandas_lover_id: "", // passwords sql_password: "", @@ -28,7 +30,7 @@ const config = { ipc_protocol: "ws", ipc_bind: "localhost:10400", - // this cluster + // this gateway cluster cluster_id, shards: [0], is_dev: true, @@ -54,6 +56,12 @@ const config = { standard_embed_color: 0x2f3136, error_log_channel_id: "", donor_payments_enabled_on_this_cluster: false, + // How often Amanda should send messages to the one she loves. Starts the Interval when the worker process starts + amandas_lover_send_timeout: 1000 * 60 * 60 * 24, + lover_messages_enabled_on_this_cluster: false, + lover_messages: [ + "I love you <3" + ], // constants patreon_url: "", diff --git a/packages/runtime-worker/src/money-manager.ts b/packages/runtime-worker/src/money-manager.ts index 5677e467..473a4ea6 100644 --- a/packages/runtime-worker/src/money-manager.ts +++ b/packages/runtime-worker/src/money-manager.ts @@ -1,3 +1,5 @@ +import type { APIDMChannel } from "discord-api-types/v10" + import passthrough = require("./passthrough") const { client, sql, confprovider, sync } = passthrough @@ -26,7 +28,19 @@ function getTimeoutDuration(): number { } let autoPayTimeout: NodeJS.Timeout | undefined = undefined +let loverMessageSendTimeout: NodeJS.Timeout | undefined = undefined +let loverChannel: APIDMChannel | undefined = undefined +let lastLoverID = confprovider.config.amandas_lover_id +let lastLoverTimeout = confprovider.config.amandas_lover_send_timeout if (confprovider.config.donor_payments_enabled_on_this_cluster) autoPayTimeout = setTimeout(autoPayTimeoutFunction, getTimeoutDuration()) +if (confprovider.config.lover_messages_enabled_on_this_cluster && confprovider.config.amandas_lover_id.length) { + client.snow.user.createDirectMessageChannel(confprovider.config.amandas_lover_id).then(channel => { + loverChannel = channel as APIDMChannel + loverMessageSendTimeout = setTimeout(loverMessageSendTimeoutFunction, confprovider.config.amandas_lover_send_timeout) + }).catch(() => { + console.error("Couldn't create DM channel for lover") + }) +} export async function autoPayTimeoutFunction() { @@ -42,11 +56,51 @@ export async function autoPayTimeoutFunction() { autoPayTimeout = setTimeout(autoPayTimeoutFunction, time) } +function loverMessageSendTimeoutFunction() { + if (!loverChannel) return console.error("lover send message timeout function triggered but there was no lover channel") + const message = sharedUtils.arrayRandom(confprovider.config.lover_messages) + client.snow.channel.createMessage(loverChannel.id, { content: message }) + .then(() => console.log("Sent a message to my lover <3\n", message)) + .catch(() => console.error("Failed to send a message to my lover. I'm gonna try again anyways next time to see if I can")) + loverMessageSendTimeout = setTimeout(loverMessageSendTimeoutFunction, confprovider.config.amandas_lover_send_timeout) +} + +function onConfigChangeCallback() { + if ((confprovider.config.lover_messages_enabled_on_this_cluster && !confprovider.config.amandas_lover_id.length) || !confprovider.config.lover_messages_enabled_on_this_cluster) { + if (loverMessageSendTimeout) clearTimeout(loverMessageSendTimeout) + loverMessageSendTimeout = undefined + loverChannel = undefined + } + + if (confprovider.config.lover_messages_enabled_on_this_cluster && confprovider.config.amandas_lover_id.length && lastLoverID !== confprovider.config.amandas_lover_id) { + client.snow.user.createDirectMessageChannel(confprovider.config.amandas_lover_id).then(channel => { + loverChannel = channel as APIDMChannel + lastLoverID = confprovider.config.amandas_lover_id + console.log(`Lover channel changed to belong to ${confprovider.config.amandas_lover_id}`) + }) + } + + if (confprovider.config.amandas_lover_send_timeout !== lastLoverTimeout) { + lastLoverTimeout = confprovider.config.amandas_lover_send_timeout + if (loverMessageSendTimeout) clearTimeout(loverMessageSendTimeout) + loverMessageSendTimeout = undefined + if (confprovider.config.lover_messages_enabled_on_this_cluster && confprovider.config.amandas_lover_id.length) loverMessageSendTimeout = setTimeout(loverMessageSendTimeoutFunction, confprovider.config.amandas_lover_send_timeout) + } +} + +confprovider.addCallback(onConfigChangeCallback) + sync.events.once(__filename, () => { + confprovider.removeCallback(onConfigChangeCallback) + if (autoPayTimeout) { clearTimeout(autoPayTimeout) console.log("cleared old donor pay timeout") } + if (loverMessageSendTimeout) { + clearTimeout(loverMessageSendTimeout) + console.log("cleared old lover message send timeout") + } }) export async function getPersonalRow(userID: string) {