-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from en3sis/feat/standup-plugin
New: standup plugin
- Loading branch information
Showing
12 changed files
with
789 additions
and
768 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { CommandInteraction, TextChannel } from 'discord.js' | ||
import * as cron from 'cron' | ||
import { StandupScheduleMetadata } from '../../types/plugins' | ||
import { updateMetadataGuildPlugin } from '../bot/plugins.controller' | ||
import supabase from '../../libs/supabase' | ||
import { scheduledTasks } from '../tasks/cron-jobs' | ||
import { Hans } from '../..' | ||
|
||
// TODO: @en3sis: Allow multiples standup schedules | ||
export const standupPluginController = async ( | ||
interaction: CommandInteraction, | ||
metadata: StandupScheduleMetadata, | ||
) => { | ||
try { | ||
const { channelId, expression, role } = metadata | ||
|
||
const _expression = `0 ${expression} * * 1-5` | ||
const isValidExpression = RegExp(/^[0-9,]+$/).test(expression) | ||
|
||
if (isValidExpression) { | ||
await updateMetadataGuildPlugin( | ||
{ ...metadata, expression: _expression }, | ||
'standup', | ||
interaction.guildId, | ||
) | ||
|
||
await interaction.editReply({ | ||
content: `Enabled Standup Notifications in <#${channelId}> from **Monday to Friday** at **${expression}h** and mentioning the role ${role} \n\n You can disable it by running **/plugins toggle standup false**`, | ||
}) | ||
} else { | ||
await interaction.editReply({ | ||
content: `Invalid cron expression: ${expression}, please provide a 24h format (eg: 9, 12, 15)`, | ||
}) | ||
} | ||
} catch (error) { | ||
console.error('❌ ERROR: standupPluginController(): ', error) | ||
} | ||
} | ||
|
||
export const initStadupsSchedules = async () => { | ||
try { | ||
const { error, data } = await supabase | ||
.from('guilds_plugins') | ||
.select('owner, metadata, enabled') | ||
.eq('name', 'standup') | ||
|
||
if (error) throw error | ||
|
||
data.forEach(async (standupGuildPlugin) => { | ||
if (!standupGuildPlugin.metadata || !standupGuildPlugin.enabled) return | ||
|
||
await registerStandupSchedule( | ||
standupGuildPlugin.owner, | ||
standupGuildPlugin.metadata as StandupScheduleMetadata, | ||
) | ||
}) | ||
} catch (error) { | ||
console.error('❌ ERROR: registerStandupSchedule(): ', error) | ||
} | ||
} | ||
|
||
export const registerStandupSchedule = async (owner: string, metadata: StandupScheduleMetadata) => { | ||
try { | ||
const { channelId, expression, role, message } = metadata | ||
|
||
const job = new cron.CronJob(expression, () => { | ||
const channel = Hans.channels.cache.get(channelId) as TextChannel | ||
|
||
if (channel) { | ||
// INFO: Current date in format Tuesday, 1st of December 2020 | ||
const currentDate = new Date().toLocaleDateString('en-GB', { | ||
weekday: 'long', | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
}) | ||
|
||
// INFO: Opens a tread on the messaged sent to the channel. | ||
channel.send(`📆 Standup: **${currentDate}** | ${role ?? ''}`).then((msg) => { | ||
msg.startThread({ | ||
name: `${message ?? '✍️ Please write down your standup'}`, | ||
autoArchiveDuration: 1440, | ||
}) | ||
}) | ||
} | ||
}) | ||
|
||
job.start() | ||
// TODO: @en3sis: Expand to more than one job | ||
scheduledTasks[`${owner}#standup`] = job | ||
if (!!process.env.ISDEV) { | ||
console.debug(`✅ Standup schedule registered count:`, Object.keys(scheduledTasks).length) | ||
} | ||
} catch (error) { | ||
console.error('❌ ERROR: registerStandupSchedule(): ', error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
import { Client } from 'discord.js' | ||
import { initStadupsSchedules } from '../plugins/standup.controller' | ||
|
||
export const CronJobsTasks = async (Hans: Client) => { | ||
// INFO: Stores the scheduled task so we can stop it later. | ||
export const scheduledTasks = {} | ||
|
||
/** Schedule cron jobs. */ | ||
export const scheduleCronJobs = async () => { | ||
try { | ||
// await redditPluginInit(Hans) | ||
// cron.schedule( | ||
// process.env.ISDEV === 'true' ? CRON_JOB_TIME_DEV : CRON_JOB_TIME, | ||
// async () => await redditPluginInit(Hans), | ||
// ) | ||
// INFO: Standup Plugin | ||
await initStadupsSchedules() | ||
} catch (error) { | ||
console.error('❌ ERROR: CronJobsTasks(): ', error) | ||
console.error('❌ ERROR: scheduleCronJobs(): ', error) | ||
} | ||
} | ||
|
||
/** | ||
* Stops a specific cron job. | ||
* @param {string} id - The id of the cron job. | ||
* */ | ||
export const stopSpecificCronJob = (id: string) => { | ||
if (scheduledTasks[id]) { | ||
scheduledTasks[id].stop() | ||
delete scheduledTasks[id] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.