Skip to content

Commit

Permalink
<3
Browse files Browse the repository at this point in the history
  • Loading branch information
PapiOphidian committed Nov 8, 2023
1 parent fc02068 commit af9a97b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
10 changes: 9 additions & 1 deletion config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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,
Expand All @@ -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: "",
Expand Down
54 changes: 54 additions & 0 deletions packages/runtime-worker/src/money-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { APIDMChannel } from "discord-api-types/v10"

import passthrough = require("./passthrough")
const { client, sql, confprovider, sync } = passthrough

Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down

0 comments on commit af9a97b

Please sign in to comment.