Skip to content
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

Restructure BotManager logic to use switch cases. #3

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tergytrg
Copy link

@tergytrg tergytrg commented Apr 5, 2023

This PR refactors the existing interaction handling logic to use switch statements instead of if-else blocks. This makes the code more concise, easier to read, and it better represents the Interaction categories.

Changes Made:

  • Replaced the if-else block with a switch statement that handles each interaction based on its customId.
  • Moved the splitting of the customId into a separate variable to make the code more readable.
  • Grouped the interactions into categories (onboarding, diplomacy, vote, etc.) for easier management.

Disclaimer:
I have not tested this code as setting up a Discord Application would require a lot of work. I would greatly appreciate if someone with a test-application available could make sure the functionality is unchanged.

@theelicht
Copy link
Member

Wat betreft code ben ik het eens dat dit er beter uit ziet, maar ook ik heb nog geen test bot. @Sheepolution kun jij testen of dit werkt?

@Sheepolution
Copy link
Member

The idea is good, but I'm not a fan of the nested switch statements.
Instead, we should create an InteractionHandler, with methods like OnDiplomacyButton, OnVoteModal and OnThreadSelectMenu. Even if these methods would consist of a single line.

Don't forget to add breaks for each case, and use idParts instead, since id_parts breaks the eslint rules.

} else if (interaction.customId.startsWith('coordinate_claim')) {
ArtHandler.OnClaimPixel(messageInfo, interaction.customId.split('_')[2]);
const messageInfo: IMessageInfo = await DiscordUtils.ParseInteractionToInfo(interaction);
const id_parts = interaction.customId.split('_');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use idParts instead.

Suggested change
const id_parts = interaction.customId.split('_');
const idParts = interaction.customId.split('_');

const messageInfo: IMessageInfo = await DiscordUtils.ParseInteractionToInfo(interaction);
const id_parts = interaction.customId.split('_');
switch(id_parts[0]) {
case 'onboarding': {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to place a break after each case.

const id_parts = interaction.customId.split('_');
switch(id_parts[0]) {
case 'onboarding': {
switch(id_parts[1]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a nested switch statement, create an InteractionHandler and place the inner switch statement in a method there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An InteractionHandler is a good idea! At that point, is there any reason to distinguish between Button, Modal, and ThreadMenu interactions? I feel like we can leave that to the BotManager, and let the InteractionHandler contain methods like onDiplomacyInteraction and onVoteInteraction to reduce the complexity.

@tergytrg tergytrg marked this pull request as draft April 6, 2023 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants