Skip to content

Commit

Permalink
fix bump notifications on restart, make them a bit more consistent i …
Browse files Browse the repository at this point in the history
…think
  • Loading branch information
bristermitten committed Oct 29, 2024
1 parent 39d6a31 commit 53cb010
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions src/modules/core/bump.listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import {ChannelType, InteractionType} from "discord-api-types/v10";
import {getOrCreateUserById} from "../../store/models/DDUser.js";
import {logger} from "../../logging.js";
import {config} from "../../Config.js";
import {Client} from "discord.js";

export const BumpListener: EventListener = {
interactionCreate: async (client, message) => {

ready: async (client) => {
scheduleBumpReminder(client)
},
messageCreate: async (client, message) => {
const interaction = message.interactionMetadata


if (!interaction || !(interaction.type == InteractionType.ApplicationCommand)) return
if (message.author.id != '302050872383242240') return // /disboard user id
// noinspection JSDeprecatedSymbols dont think theres another way of doing this
// noinspection JSDeprecatedSymbols don't think there's another way of doing this
if (message.interaction?.commandName !== 'bump') return

// since the bump failed message is ephemeral, we know if we can see the message then the bump succeeded!
Expand All @@ -24,35 +25,52 @@ export const BumpListener: EventListener = {
logger.info(`User ${message.author.tag} bumped! Total bumps: ${ddUser.bumps}`)
await ddUser.save()

setTimeout(async () => {
const botCommands = await client.channels.fetch(config.channels.botCommands)
if (!botCommands) {
logger.error('Bot commands channel not found')
return
}
if (botCommands.type != ChannelType.GuildText) {
logger.error('Bot commands channel is not a text channel')
return
}
lastBumpTime = new Date()
scheduleBumpReminder(client)
}
}

const bumpNotificationsRoleId = config.roles.bumpNotifications;
if (!bumpNotificationsRoleId) {
logger.error('Bump notifications role not found')
return
}
const bumpNotificationsRole = await message.guild?.roles.fetch(bumpNotificationsRoleId)
let lastBumpTime = new Date()

function scheduleBumpReminder(client: Client) {
// schedule a bump reminder for 2 hours from now
setTimeout(async () => await sendBumpNotification(client), 60 * 60 * 1000 * 2)
logger.info('Scheduled bump reminder for 2 hours from now')
}

if (!bumpNotificationsRole) {
logger.error('Bump notifications role not found')
return
}

await botCommands.send({
content: `<@&${bumpNotificationsRole}>, The server is ready to be bumped! </bump:947088344167366698>`
})
async function sendBumpNotification(client: Client) {
// if the last bump was less than 2 hours ago, don't send another notification
if (new Date().getTime() - lastBumpTime.getTime() < 60 * 60 * 1000 * 2) {
logger.info(`Last bump was less than 2 hours ago (${lastBumpTime.toUTCString()}), not sending bump notification`)
return
}

const botCommands = await client.channels.fetch(config.channels.botCommands)
if (!botCommands) {
logger.error('Bot commands channel not found')
return
}
if (botCommands.type != ChannelType.GuildText) {
logger.error('Bot commands channel is not a text channel')
return
}

}, 60 * 60 * 1000 * 2) // 2 hours
const bumpNotificationsRoleId = config.roles.bumpNotifications;
if (!bumpNotificationsRoleId) {
logger.error('Bump notifications role not found')
return
}
const bumpNotificationsRole = await (await client.guilds.fetch(config.guildId))
.roles.fetch(bumpNotificationsRoleId)

if (!bumpNotificationsRole) {
logger.error('Bump notifications role not found')
return
}
logger.info('Sending bump notification!')

await botCommands.send({
content: `${bumpNotificationsRole}, The server is ready to be bumped! </bump:947088344167366698>`
})
}

0 comments on commit 53cb010

Please sign in to comment.