Update ready.ts, start work on notification system
This commit is contained in:
parent
a4362c9bb3
commit
69486b5595
2
deps.ts
2
deps.ts
|
@ -16,8 +16,6 @@ export {
|
||||||
ButtonStyles,
|
ButtonStyles,
|
||||||
ChannelTypes,
|
ChannelTypes,
|
||||||
createBot,
|
createBot,
|
||||||
editBotMember,
|
|
||||||
editBotStatus,
|
|
||||||
getBotIdFromToken,
|
getBotIdFromToken,
|
||||||
Intents,
|
Intents,
|
||||||
InteractionResponseTypes,
|
InteractionResponseTypes,
|
||||||
|
|
|
@ -33,7 +33,7 @@ const execute = async (bot: Bot, interaction: Interaction) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.helpers.editMessage(evtChannelId, evtMessageId, { embeds: [interaction.message.embeds[0]] }).then(() => {
|
bot.helpers.editMessage(evtChannelId, evtMessageId, { embeds: [interaction.message.embeds[0]] }).then(() => {
|
||||||
dbClient.execute(queries.updateEvent, [eventTime, evtChannelId, evtMessageId]).then(() => {
|
dbClient.execute(queries.updateEventTime, [eventTime, evtChannelId, evtMessageId]).then(() => {
|
||||||
// Acknowledge user so discord doesn't get annoyed
|
// Acknowledge user so discord doesn't get annoyed
|
||||||
bot.helpers.sendInteractionResponse(interaction.id, interaction.token, {
|
bot.helpers.sendInteractionResponse(interaction.id, interaction.token, {
|
||||||
type: InteractionResponseTypes.ChannelMessageWithSource,
|
type: InteractionResponseTypes.ChannelMessageWithSource,
|
||||||
|
|
|
@ -13,8 +13,11 @@ export const dbClient = await new Client().connect({
|
||||||
|
|
||||||
export const queries = {
|
export const queries = {
|
||||||
callIncCnt: (cmdName: string) => `CALL INC_CNT("${cmdName}");`,
|
callIncCnt: (cmdName: string) => `CALL INC_CNT("${cmdName}");`,
|
||||||
|
selectEvents: (notifiedFlag: number, lockedFlag: number) => `SELECT * FROM active_events WHERE notifiedFlag = ${notifiedFlag} AND lockedFlag = ${lockedFlag} AND eventTime < ?`,
|
||||||
|
selectFailedEvents: 'SELECT * FROM active_events WHERE (notifiedFlag = -1 OR lockedFlag = -1) AND eventTime < ?',
|
||||||
insertEvent: 'INSERT INTO active_events(messageId,channelId,guildId,ownerId,eventTime) values(?,?,?,?,?)',
|
insertEvent: 'INSERT INTO active_events(messageId,channelId,guildId,ownerId,eventTime) values(?,?,?,?,?)',
|
||||||
updateEvent: 'UPDATE active_events SET eventTime = ? WHERE channelId = ? AND messageId = ?',
|
updateEventTime: 'UPDATE active_events SET eventTime = ? WHERE channelId = ? AND messageId = ?',
|
||||||
|
updateEventFlags: (notifiedFlag: number, lockedFlag: number) => `UPDATE active_events SET notifiedFlag = ${notifiedFlag} AND lockedFlag = ${lockedFlag} WHERE channelId = ? AND messageId = ?`,
|
||||||
deleteEvent: 'DELETE FROM active_events WHERE channelId = ? AND messageId = ?',
|
deleteEvent: 'DELETE FROM active_events WHERE channelId = ? AND messageId = ?',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,50 +1,82 @@
|
||||||
import config from '../../config.ts';
|
import config from '../../config.ts';
|
||||||
import { LOCALMODE } from '../../flags.ts';
|
import { LOCALMODE } from '../../flags.ts';
|
||||||
import { ActivityTypes, Bot, BotWithCache, editBotMember, editBotStatus, log, LT } from '../../deps.ts';
|
import { ActivityTypes, Bot, BotWithCache, log, LT } from '../../deps.ts';
|
||||||
import { getRandomStatus, successColor } from '../commandUtils.ts';
|
import { getRandomStatus, successColor } from '../commandUtils.ts';
|
||||||
|
import { ActiveEvent } from '../types/commandTypes.ts';
|
||||||
import utils from '../utils.ts';
|
import utils from '../utils.ts';
|
||||||
|
import { dbClient, queries } from '../db.ts';
|
||||||
|
|
||||||
|
const tenMinutes = 10 * 60 * 1000;
|
||||||
|
|
||||||
|
// Storing intervalIds in case bot soft reboots to prevent multiple of these intervals from stacking
|
||||||
|
let notificationIntervalId: number;
|
||||||
|
let botStatusIntervalId: number;
|
||||||
|
|
||||||
export const ready = (rawBot: Bot) => {
|
export const ready = (rawBot: Bot) => {
|
||||||
const bot = rawBot as BotWithCache;
|
const bot = rawBot as BotWithCache;
|
||||||
log(LT.INFO, `${config.name} Logged in!`);
|
log(LT.INFO, `${config.name} Logged in!`);
|
||||||
editBotStatus(bot, {
|
bot.helpers.editBotStatus({
|
||||||
activities: [{
|
activities: [{
|
||||||
name: 'Booting up . . .',
|
name: 'Booting up . . .',
|
||||||
type: ActivityTypes.Game,
|
type: ActivityTypes.Game,
|
||||||
createdAt: new Date().getTime(),
|
createdAt: new Date().getTime(),
|
||||||
}],
|
}],
|
||||||
status: 'online',
|
status: 'online',
|
||||||
});
|
}).catch((e) => log(LT.ERROR, `Failed to update status (booting): ${utils.jsonStringifyBig(e)}`));
|
||||||
|
|
||||||
// Interval to rotate the status text every 30 seconds to show off more commands
|
// Interval to rotate the status text every 30 seconds to show off more commands
|
||||||
setInterval(async () => {
|
if (botStatusIntervalId) clearInterval(botStatusIntervalId);
|
||||||
|
botStatusIntervalId = setInterval(async () => {
|
||||||
log(LT.LOG, 'Changing bot status');
|
log(LT.LOG, 'Changing bot status');
|
||||||
try {
|
bot.helpers.editBotStatus({
|
||||||
// Wrapped in try-catch due to hard crash possible
|
activities: [{
|
||||||
editBotStatus(bot, {
|
name: getRandomStatus(bot.guilds.size + bot.dispatchedGuildIds.size),
|
||||||
activities: [{
|
type: ActivityTypes.Game,
|
||||||
name: getRandomStatus(bot.guilds.size + bot.dispatchedGuildIds.size),
|
createdAt: new Date().getTime(),
|
||||||
type: ActivityTypes.Game,
|
}],
|
||||||
createdAt: new Date().getTime(),
|
status: 'online',
|
||||||
}],
|
}).catch((e) => log(LT.ERROR, `Failed to update status (in interval): ${utils.jsonStringifyBig(e)}`));
|
||||||
status: 'online',
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
log(LT.ERROR, `Failed to update status: ${utils.jsonStringifyBig(e)}`);
|
|
||||||
}
|
|
||||||
}, 30000);
|
}, 30000);
|
||||||
|
|
||||||
|
// Interval to handle event notifications and cleanup every minute
|
||||||
|
if (notificationIntervalId) clearInterval(notificationIntervalId);
|
||||||
|
notificationIntervalId = setInterval(() => {
|
||||||
|
const now = new Date().getTime();
|
||||||
|
|
||||||
|
// Notify Members of Events
|
||||||
|
dbClient.execute(queries.selectEvents(0, 0), [new Date(now + tenMinutes)]).then((events) => events.rows?.forEach((event) => console.log(event as ActiveEvent))).catch((e) =>
|
||||||
|
utils.commonLoggers.dbError('ready.ts@notifyMembers', 'SELECT events from', e)
|
||||||
|
);
|
||||||
|
|
||||||
|
// // Notify Alternates of Events (if NOT full) and lock the event message
|
||||||
|
// dbClient.execute(queries.selectEvents(1, 0), [new Date(now)]).then((events) => console.log(events.rows as ActiveEvent[])).catch((e) =>
|
||||||
|
// utils.commonLoggers.dbError('ready.ts@notifyAlternates', 'SELECT events from', e)
|
||||||
|
// );
|
||||||
|
|
||||||
|
// // Delete the event
|
||||||
|
// dbClient.execute(queries.selectEvents(1, 1), [new Date(now - tenMinutes)]).then((events) => console.log(events.rows as ActiveEvent[])).catch((e) =>
|
||||||
|
// utils.commonLoggers.dbError('ready.ts@deleteEvent', 'SELECT events from', e)
|
||||||
|
// );
|
||||||
|
|
||||||
|
// // Handle events that failed at some point
|
||||||
|
// dbClient.execute(queries.selectFailedEvents, [new Date(now + tenMinutes)]).then((events) => console.log(events.rows as ActiveEvent[])).catch((e) =>
|
||||||
|
// utils.commonLoggers.dbError('ready.ts@deleteEvent', 'SELECT events from', e)
|
||||||
|
// );
|
||||||
|
}, 60000);
|
||||||
|
|
||||||
// setTimeout added to make sure the startup message does not error out
|
// setTimeout added to make sure the startup message does not error out
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
LOCALMODE && editBotMember(bot, config.devServer, { nick: `LOCAL - ${config.name}` });
|
LOCALMODE && bot.helpers.editBotMember(config.devServer, { nick: `LOCAL - ${config.name}` });
|
||||||
editBotStatus(bot, {
|
|
||||||
|
bot.helpers.editBotStatus({
|
||||||
activities: [{
|
activities: [{
|
||||||
name: 'Booting Complete',
|
name: 'Booting Complete',
|
||||||
type: ActivityTypes.Game,
|
type: ActivityTypes.Game,
|
||||||
createdAt: new Date().getTime(),
|
createdAt: new Date().getTime(),
|
||||||
}],
|
}],
|
||||||
status: 'online',
|
status: 'online',
|
||||||
});
|
}).catch((e) => log(LT.ERROR, `Failed to update status (boot complete): ${utils.jsonStringifyBig(e)}`));
|
||||||
|
|
||||||
bot.helpers.sendMessage(config.logChannel, {
|
bot.helpers.sendMessage(config.logChannel, {
|
||||||
embeds: [{
|
embeds: [{
|
||||||
title: `${config.name} is now Online`,
|
title: `${config.name} is now Online`,
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
// define functions that take db data and do actions on it
|
||||||
|
// in ready, call db select to create a to do list and send it here in dbclient.execute().then(FUNC FROM THIS FILE).catch()
|
|
@ -43,3 +43,13 @@ export type UrlIds = {
|
||||||
channelId: bigint;
|
channelId: bigint;
|
||||||
messageId: bigint;
|
messageId: bigint;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ActiveEvent = {
|
||||||
|
messageId: bigint;
|
||||||
|
channelId: bigint;
|
||||||
|
guildId: bigint;
|
||||||
|
ownerId: bigint;
|
||||||
|
eventTime: Date;
|
||||||
|
notifiedFlag: number;
|
||||||
|
lockedFlag: number;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue