-
Notifications
You must be signed in to change notification settings - Fork 0
Automate dFunk roles #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
crystopherross
wants to merge
101
commits into
main
Choose a base branch
from
iss41
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Automate dFunk roles #196
Changes from 65 commits
Commits
Show all changes
101 commits
Select commit
Hold shift + click to select a range
853e60b
Big update
crystopherross f0121c6
Merge remote-tracking branch 'origin/main' into iss41
crystopherross 33085c6
nothing special
crystopherross 9cb3c2e
Add permission to new manual dfunkt update command
crystopherross b663083
Linter and Prettier :D
crystopherross c817900
Add more testing info, remove code duplication.
crystopherross 95289af
Provide more detailed test fail info.
crystopherross 1caa99d
Provide more detailed test info.
crystopherross 0de9435
Atomize update logic further.
crystopherross d282c04
Fix console log message
crystopherross 65a530e
Skip cache while fetching for testting purposes.
crystopherross 0e6e069
Lint + prettier
crystopherross 78a14d2
Big update
crystopherross be0594b
nothing special
crystopherross 647a199
Add permission to new manual dfunkt update command
crystopherross 0e0f647
Linter and Prettier :D
crystopherross cfb2aae
Add more testing info, remove code duplication.
crystopherross 6603d4d
Provide more detailed test fail info.
crystopherross c342a30
Provide more detailed test info.
crystopherross b7f78db
Atomize update logic further.
crystopherross c5ac5f7
Fix console log message
crystopherross 9d4929d
Skip cache while fetching for testting purposes.
crystopherross 19b3c9b
Lint + prettier
crystopherross 89182be
try rebase
crystopherross 00c07a2
Merge branch 'iss41' of https://github.com/datasektionen/harmony into…
crystopherross 2881885
Try rebase 2
crystopherross b1348ce
Update src/commands/testMandate/test.command.ts
crystopherross 132c52f
Add subcommands because apparently I like work.
crystopherross 460875d
Merge branch 'iss41' of https://github.com/datasektionen/harmony into…
crystopherross 4473a50
Add EV check for Hive credentials.
crystopherross 754aaa0
Redo the `dfunk` command.
crystopherross 2d4c934
Redo the dfunk update routine to use Hive.
crystopherross 3bbb8e5
Add functionality to fetch data (API calls) from Hive.
crystopherross 27a5485
Temporarily modify test cases (some roles still missing on Hive)
crystopherross ff37b91
Adapt test script for dfunk role update to the new version using Hive.
crystopherross 7988ceb
Remove hard-coded translation tables.
crystopherross da6872f
Change all appereances of `dfunkt` to `dfunk`.
crystopherross ffbbde6
Variable rename
crystopherross 4750980
Fix problems with previous variable renaming and translation tables.
crystopherross 23faf0a
Make the dfunk role update cronjob target all servers the bot is in.
crystopherross c2deaa7
Remove unnecesary import.
crystopherross f267ec9
Format and linting
crystopherross 0348d21
Update test cases.
crystopherross 949e82e
Change Test Case Format
crystopherross 93ea5c2
Change Testing logic following changes to test format.
crystopherross 13436ce
Äntligen klar med issuen:
crystopherross 96f46bb
Fix test validator
crystopherross d133e4b
Change test evaluation logic
crystopherross a0939dc
Remove unnecessary artifacts.
crystopherross dbadbaa
Linting
crystopherross dab874a
Reduced code duplication (still a lot of boilerplate).
crystopherross 7a04300
Mattermost webhook endpoint moved to top of file.
crystopherross ccd8b53
Added 'toggle' and 'status' subcommands for 'dfunk'
crystopherross c6c4e92
Linting
crystopherross d54bc78
Update comments
crystopherross 9ed1df3
Linting
crystopherross 16dc4fe
Merge remote-tracking branch 'origin/main' into iss41
crystopherross 7d579f6
Use custom-made log functions.
crystopherross 1725c45
Restore the dfunk CronTime to each Saturday
crystopherross d355f0b
Modify log functions to take arbitrary inputs.
crystopherross 9b77491
Use project's log functions.
crystopherross 2f3e763
Rename update-dfunk-roles-get-post.ts to update-dfunk-roles.ts
crystopherross 59acec8
Revert wide automatic role update.
crystopherross df2dbef
Lint
crystopherross 6007d7d
Merge branch 'main' into iss41
crystopherross eb91934
Update src/commands/dfunk/subcommands/status/dfunk-status.handler.ts
crystopherross 88f0e16
It is "elegibles", not "legibles".
crystopherross a18d0aa
Add envvars for Hive and Mattermost webhook in template.
crystopherross 9741ec0
Use envvar to access Mattermost webhook token instead.
crystopherross f857dfb
Merge branch 'iss41' of https://github.com/datasektionen/harmony into…
crystopherross e96c035
Remove unnecessary local variables.
crystopherross f251b63
Remove unusued API functions for dfunk, retain the endpoints for futu…
crystopherross 48cc015
Temporarily remove the tests.
crystopherross b6c9c03
Remove explicit test dependencies.
crystopherross 5f0b43e
It is "elegibles", not "legibles".
crystopherross a3669b9
Initialize "test frmework" barebones.
crystopherross 6939ad1
Isolate tests from production.
crystopherross ea518d1
Add 'test' command registration logic
crystopherross 9863066
Finalize 'test framework' barebones
crystopherross a73556c
Patch .gitignore
crystopherross a038eb7
Stop tracking "test framework" barebone files.
crystopherross ab7eefe
Formatting
crystopherross 53b08fd
Merge remote-tracking branch 'origin/main' into iss41
crystopherross c2725a1
Remove `/dfunk test` command.
crystopherross 59f6f21
Remove `/dfunk test` command
crystopherross c110357
Change hive API error messages, rename `HIVE` envvar.
crystopherross 177640e
Separate testing logic from the dfunk update.
crystopherross 8f55a55
Format
crystopherross 90ca541
Remove old files from the `include` field in `tsconfig.json`
crystopherross f14716e
Remove unnecessary endpoints, add comments.
crystopherross 15aabcb
Format
crystopherross 6ffff80
Add environment variables for Hive and Mattermost.
crystopherross e41a6ee
Remove Mattermost Token
crystopherross 101c7e8
Add assets to dev environment.
crystopherross 5fdba8c
Add `log` command.
crystopherross fa20be8
Add file for storing log channel ID.
crystopherross f4fb1a6
Change error reporting.
crystopherross 2325067
Fix `/dfunk status` Discord bot message.
crystopherross 011c13d
Add error handling when trying to execute job on unkown guild
crystopherross 82665a3
Reset crontime to every Saturday 00.00
crystopherross 2112010
Make the cronjob actually execute the update (again)
crystopherross File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,6 @@ | ||
| export enum DfunkSubcommands { | ||
| UPDATE = "update", | ||
| TEST = "test", | ||
| STATUS = "status", | ||
| TOGGLE = "toggle", | ||
| } |
This file contains hidden or 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,36 @@ | ||
| import { SlashCommandBuilder, PermissionFlagsBits } from "discord.js"; | ||
| import { CommandNames } from "../commands.names"; | ||
| import { DfunkSubcommands } from "./dfunk-subcommands"; | ||
|
|
||
| const command = new SlashCommandBuilder() | ||
| .setName(CommandNames.DFUNK) | ||
| .setDescription("Command related to the dfunk automatic update.") | ||
| .setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild); | ||
|
|
||
| command.addSubcommand((subCommand) => | ||
| subCommand | ||
| .setName(DfunkSubcommands.UPDATE) | ||
| .setDescription("Manually start the dfunk role update routine.") | ||
| ); | ||
|
|
||
| command.addSubcommand((subCommand) => | ||
| subCommand | ||
| .setName(DfunkSubcommands.TEST) | ||
| .setDescription("Start the dfunk role update test routine.") | ||
| ); | ||
|
|
||
| command.addSubcommand((subCommand) => | ||
| subCommand | ||
| .setName(DfunkSubcommands.TOGGLE) | ||
| .setDescription("Toggles the automatic dfunk role update routine.") | ||
| ); | ||
|
|
||
| command.addSubcommand((subCommand) => | ||
| subCommand | ||
| .setName(DfunkSubcommands.STATUS) | ||
| .setDescription( | ||
| "Reveals the current status of the automatic dfunk role update." | ||
| ) | ||
| ); | ||
|
|
||
| export const dfunkCommand = command; |
This file contains hidden or 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,26 @@ | ||
| import { CommandNotFoundError } from "../../shared/errors/command-not-founder.error"; | ||
| import { GuildChatInputCommandInteraction } from "../../shared/types/GuildChatInputCommandType"; | ||
| import { DfunkSubcommands } from "./dfunk-subcommands"; | ||
| import { handleDfunkTest } from "./subcommands/test/dfunk-test.handler"; | ||
| import { handleDfunkUpdate } from "./subcommands/update/dfunk-update.handler"; | ||
| import { handleDfunkToggle } from "./subcommands/toggle/dfunk-toggle.handler"; | ||
| import { handleDfunkStatus } from "./subcommands/status/dfunk-status.handler"; | ||
|
|
||
| export async function handleDfunk( | ||
| interaction: GuildChatInputCommandInteraction | ||
| ): Promise<void> { | ||
| const subcommandName = interaction.options.getSubcommand(true); | ||
|
|
||
| switch (subcommandName) { | ||
| case DfunkSubcommands.UPDATE: | ||
| return await handleDfunkUpdate(interaction); | ||
| case DfunkSubcommands.TEST: | ||
| return await handleDfunkTest(interaction); | ||
| case DfunkSubcommands.STATUS: | ||
| return await handleDfunkStatus(interaction); | ||
| case DfunkSubcommands.TOGGLE: | ||
| return await handleDfunkToggle(interaction); | ||
| default: | ||
| throw new CommandNotFoundError(interaction.commandName); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/commands/dfunk/subcommands/status/dfunk-status.handler.ts
This file contains hidden or 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,30 @@ | ||
| import { MessageFlags } from "discord.js"; | ||
| import { jobs } from "../../../../index"; | ||
| import { GuildChatInputCommandInteraction } from "../../../../shared/types/GuildChatInputCommandType"; | ||
|
|
||
| export const handleDfunkStatus = async ( | ||
| interaction: GuildChatInputCommandInteraction | ||
| ): Promise<void> => { | ||
| // We assume that this job is loaded | ||
| const dfunkUpdateJob = jobs.get("updateDfunkRoles")!; | ||
| const jobStatus = dfunkUpdateJob.job.isActive; | ||
| if (jobStatus) { | ||
| await interaction.reply({ | ||
| content: `The automatic dfunk role update is active. | ||
| The automatic update was last executed ${ | ||
| dfunkUpdateJob.job.lastExecution | ||
| }. | ||
| The next automatic update will be executed ${dfunkUpdateJob.job.nextDate()} | ||
| To toggle this functionality on/off, use the \`/dfunk toggle\` command. | ||
| `, | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| } else { | ||
| await interaction.reply({ | ||
| content: `The automatic dfunk role update is inactive. | ||
| The automatic update was last executed ${dfunkUpdateJob.job.lastExecution}. | ||
| To toggle this functionality on/off, use the \`/dfunk toggle\` command.`, | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| } | ||
| }; | ||
123 changes: 123 additions & 0 deletions
123
src/commands/dfunk/subcommands/test/dfunk-test.handler.ts
This file contains hidden or 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,123 @@ | ||
| import { testCases } from "../../../../tests/dfunk-roles-update/test_cases"; | ||
| import { executeTestCase } from "../../../../tests/dfunk-roles-update/tests"; | ||
| import { GuildChatInputCommandInteraction } from "../../../../shared/types/GuildChatInputCommandType"; | ||
| import { | ||
| Role as DiscordRole, | ||
| Collection, | ||
| GuildMember as DiscordGuildMember, | ||
| } from "discord.js"; | ||
| import { | ||
| createDfunkDiscordRoles /*removeDfunkDiscordRoles,*/, | ||
| } from "../../../../jobs/update-dfunk-roles"; | ||
| import * as log from "../../../../shared/utils/log"; | ||
| export const handleDfunkTest = async ( | ||
| interaction: GuildChatInputCommandInteraction | ||
| ): Promise<void> => { | ||
| // Create dfunk roles in case these are missing | ||
| await createDfunkDiscordRoles(interaction.guild); | ||
| const failedData: Map< | ||
| number, | ||
| { | ||
| processedDfunkData: { | ||
| currentGroups: Map<string, string[]>; | ||
| specialRoles: [ | ||
| { | ||
| roleName: string; | ||
| specialRoleLegibles: Set<string>; | ||
| } | ||
| ]; | ||
| }; | ||
| dbUsers: Map<string, string>; | ||
| discordData: { | ||
| guildRoles: Collection<string, DiscordRole>; | ||
| guildMembers: Collection<string, DiscordGuildMember>; | ||
| }; | ||
| } | ||
| > = new Map(); | ||
| for (let index = 0; index < testCases.length; index++) { | ||
| const testResult = await executeTestCase( | ||
| interaction.guild, | ||
| "1013769549398671542", | ||
| testCases[index], | ||
| index + 1 | ||
| ); | ||
| if (!testResult.result) { | ||
| failedData.set(index + 1, { | ||
| processedDfunkData: testResult.processedDfunkData, | ||
| dbUsers: testResult.dbUsers, | ||
| discordData: testResult.discordData, | ||
| }); | ||
| break; | ||
| } | ||
| } | ||
| log.error("Failed test cases: " + Array.from(failedData.keys()).join(", ")); | ||
| failedData.forEach( | ||
| ( | ||
| caseData: { | ||
| processedDfunkData: { | ||
| currentGroups: Map<string, string[]>; | ||
| specialRoles: [ | ||
| { | ||
| roleName: string; | ||
| specialRoleLegibles: Set<string>; | ||
| } | ||
| ]; | ||
| }; | ||
| dbUsers: Map<string, string>; | ||
| discordData: { | ||
| guildRoles: Collection<string, DiscordRole>; | ||
| guildMembers: Collection<string, DiscordGuildMember>; | ||
| }; | ||
| }, | ||
| caseNumber: number | ||
| ) => { | ||
| log.info("Test case " + caseNumber + ": "); | ||
| caseData.processedDfunkData.currentGroups.forEach( | ||
| (users: string[], group: string) => { | ||
| log.info( | ||
| "The group " + | ||
| group + | ||
| " should have users: " + | ||
| users.join(", ") | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| log.info( | ||
| "Users fetched from database: " + | ||
| Array.from(caseData.dbUsers.keys()).join(", ") | ||
| ); | ||
| log.info("Fetched Discord Data: "); | ||
| log.info( | ||
| "Roles fetched:" + | ||
| caseData.discordData.guildRoles | ||
| .map((role) => role.name) | ||
| .join(", ") | ||
| ); | ||
| caseData.discordData.guildRoles.each((role) => | ||
| log.info( | ||
| "Users having " + | ||
| role.name + | ||
| ": " + | ||
| role.members | ||
| .map((member) => member.displayName) | ||
| .join(", ") | ||
| ) | ||
| ); | ||
| log.info( | ||
| "Users fetched: " + | ||
| caseData.discordData.guildMembers | ||
| .map((member) => member.displayName) | ||
| .join(", ") | ||
| ); | ||
| caseData.discordData.guildMembers.each((member) => | ||
| log.info( | ||
| "(Cached) Roles of user " + | ||
| member.displayName + | ||
| ": " + | ||
| member.roles.cache.map((role) => role.name).join(", ") | ||
| ) | ||
| ); | ||
| } | ||
| ); | ||
| }; |
28 changes: 28 additions & 0 deletions
28
src/commands/dfunk/subcommands/toggle/dfunk-toggle.handler.ts
This file contains hidden or 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,28 @@ | ||||||||
| import { GuildChatInputCommandInteraction } from "../../../../shared/types/GuildChatInputCommandType"; | ||||||||
| import { jobs } from "../../../../index"; | ||||||||
| import { MessageFlags } from "discord.js"; | ||||||||
|
|
||||||||
| export const handleDfunkToggle = async ( | ||||||||
| interaction: GuildChatInputCommandInteraction | ||||||||
| ): Promise<void> => { | ||||||||
| // We assume that this job is loaded | ||||||||
| const dfunkUpdateJob = jobs.get("updateDfunkRoles")!; | ||||||||
| const jobStatus = dfunkUpdateJob.job.isActive; | ||||||||
| if (jobStatus) { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| await dfunkUpdateJob.job.stop(); | ||||||||
| await interaction.reply({ | ||||||||
| content: ` | ||||||||
| The automatic dfunk role update has been turned off. | ||||||||
| Use this same command (\`/dfunk toggle\`) to turn on this functionality.`, | ||||||||
| flags: MessageFlags.Ephemeral, | ||||||||
| }); | ||||||||
| } else { | ||||||||
| await dfunkUpdateJob.job.start(); | ||||||||
| await interaction.reply({ | ||||||||
| content: `The automatic dfunk role update has been turned on. | ||||||||
| The next automatic update will be executed ${dfunkUpdateJob.job.nextDate()}. | ||||||||
| Use this same command (\`/dfunk toggle\`) to turn off this functionality.`, | ||||||||
| flags: MessageFlags.Ephemeral, | ||||||||
| }); | ||||||||
| } | ||||||||
| }; | ||||||||
8 changes: 8 additions & 0 deletions
8
src/commands/dfunk/subcommands/update/dfunk-update.handler.ts
This file contains hidden or 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,8 @@ | ||
| import { updateDiscordDfunkRoles } from "../../../../jobs/update-dfunk-roles"; | ||
| import { GuildChatInputCommandInteraction } from "../../../../shared/types/GuildChatInputCommandType"; | ||
|
|
||
| export const handleDfunkUpdate = async ( | ||
| interaction: GuildChatInputCommandInteraction | ||
| ): Promise<void> => { | ||
| await updateDiscordDfunkRoles(interaction.guild); | ||
| }; |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.