Skip to content

Commit

Permalink
Merge pull request #29 from iluxonchik/feature/govbot-0.0.6
Browse files Browse the repository at this point in the history
Version 0.0.6
  • Loading branch information
iluxonchik authored Aug 16, 2024
2 parents 322bf7d + 95d98c5 commit 59f5420
Show file tree
Hide file tree
Showing 20 changed files with 1,363 additions and 986 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mina-govbot",
"version": "0.0.5",
"version": "0.0.6",
"description": "Discord bot for collective decision making for Mina Protocol",
"main": "index.js",
"directories": {
Expand Down
70 changes: 53 additions & 17 deletions src/CustomIDOracle.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
import { ExclusionConstraintError } from 'sequelize';
import type { Dashboard, Screen, Action, TrackedInteraction } from './core/BaseClasses';
import { InteractionProperties } from './core/Interaction';
import { EndUserError } from './Errors';
import { EndUserError, NotFoundEndUserError } from './Errors';
import logger from './logging';
import { AnyInteractionWithValues } from './types/common';


export class CustomIDOracle {
static readonly SEPARATOR = ':';
static readonly MAX_LENGTH = 100;
protected customId: string;

constructor(customId: string) {
this.customId = customId;
}

static generateCustomId(dashboard: Dashboard, screen?: Screen, action?: Action, operation?: string, ...args: string[]): string {
const parts = [dashboard.ID];

if (screen) {
parts.push(screen.ID);
}

if (action) {
parts.push(action.ID);
}

if (operation) {
parts.push(operation);
}

parts.push(...args);

const customId = parts.join(this.SEPARATOR);

if (customId.length > this.MAX_LENGTH) {
throw new EndUserError(`CustomId length of ${customId.length} exceeds the maximum allowed value of ${this.MAX_LENGTH} characters.`);
}

return customId;
}

Expand All @@ -41,23 +47,23 @@ export class CustomIDOracle {
if (screenId) {
parts.push(screenId);
}

if (actionId) {
parts.push(actionId);
}

if (operationId) {
parts.push(operationId);
}

parts.push(...args);

const customId = parts.join(this.SEPARATOR);

if (customId.length > this.MAX_LENGTH) {
throw new EndUserError(`CustomId length of ${customId.length} exceeds the maximum allowed value of ${this.MAX_LENGTH} characters.`);
}

return customId;
}

Expand Down Expand Up @@ -86,6 +92,22 @@ export class CustomIDOracle {
return undefined;
}

/**
* Sets or updates an argument in the interaction's custom ID. If the argument doesn't exist, it will be added. If it does exist, it will be updated.
*/
public setArgument(argName: string, value: string): string {
const args: string[] = CustomIDOracle.getArguments(this.customId);
const argIndex: number = args.indexOf(argName);
if (argIndex === -1) {
args.push(argName, value);
} else {
args[argIndex + 1] = value;
}
const updatedCustomId: string = CustomIDOracle.customIdFromRawParts(CustomIDOracle.getDashboardId(this.customId), CustomIDOracle.getScreenId(this.customId), CustomIDOracle.getActionId(this.customId), CustomIDOracle.getOperationId(this.customId), ...args);

return updatedCustomId;
}

static parseCustomId(customId: string): string[] {
return customId.split(this.SEPARATOR);
}
Expand Down Expand Up @@ -124,9 +146,22 @@ export class ArgumentOracle {
PHASE: 'phase',
}

static getNamedArgument(intreaction: TrackedInteraction, argName: string, valuesIndex?:number): string {
static isArgumentEquals(intreaction: TrackedInteraction, argName: string, value: string): boolean {
try {
const argValue = this.getNamedArgument(intreaction, argName);
const result: boolean = argValue.toLowerCase() === value.toLowerCase();
logger.debug(`Comparing argument ${argName}'s ${argValue} with ${value}. Result: ${result}`);
return result;
} catch (error) {
const argValueFromContext: string | undefined = intreaction.Context.get(argName);
const result: boolean = argValueFromContext?.toLowerCase() === value.toLowerCase();
return result;
}
}

static getNamedArgument(intreaction: TrackedInteraction, argName: string, valuesIndex?: number, inputIndex?: string): string {
const argFromCustomId: string | undefined = CustomIDOracle.getNamedArgument(intreaction.customId, argName);

if (argFromCustomId) {
logger.debug(`Found argument ${argName} in custom ID: ${argFromCustomId}`);
return argFromCustomId.toLowerCase();
Expand All @@ -145,10 +180,11 @@ export class ArgumentOracle {
logger.debug(`Found argument ${argName} in values: ${argFromValues}`);
return argFromValues.toLowerCase();
} else {
throw new EndUserError(`Argument ${argName} not found in custom ID, context or values.`);
throw new NotFoundEndUserError(`Argument ${argName} not found in custom ID, context or values.`);
}
}

throw new EndUserError(`Argument ${argName} not found in custom ID or context.`);
throw new NotFoundEndUserError(`Argument ${argName} not found in custom ID or context.`);
}

}
4 changes: 4 additions & 0 deletions src/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ export class EndUserError extends GovBotError {

}

export class NotFoundEndUserError extends EndUserError {
}


export class EndUserInfo extends GovBotError {
}
31 changes: 22 additions & 9 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,32 @@ client.once('ready', async () => {
});

client.on('interactionCreate', async (interaction: Interaction<CacheType>) => {
logger.info("Start handling interaction");
try {

if (!interaction.isButton() && !interaction.isStringSelectMenu() && !interaction.isModalSubmit() && !interaction.isMessageComponent()){
logger.info(`Interaction type not supported: ${interaction.type}`);
return;
}
if (!interaction.isButton() && !interaction.isStringSelectMenu() && !interaction.isModalSubmit() && !interaction.isMessageComponent()) {
logger.info(`Interaction type not supported: ${interaction.type}`);
return;
}

logger.debug(`Before handling interaction...`);
await dashboardManager.handleInteraction(interaction);
logger.debug(`After handling interaction...`);
} catch (error) {
logger.debug(`Start handling error in interaction...`);

try {
logger.error(error);
const trackedInteratction = new TrackedInteraction(interaction as AnyInteraction);
await DiscordStatus.handleException(trackedInteratction, error);

await dashboardManager.handleInteraction(interaction);
} catch (error) {
logger.error(error);
const trackedInteratction = new TrackedInteraction(interaction as AnyInteraction);
await DiscordStatus.handleException(trackedInteratction, error);
} catch (error) {
logger.error(`Unrecoverable error: ${error}`);
}
}

logger.info("Finished handling interaction");

});

client.login(process.env.DISCORD_TOKEN);
1 change: 0 additions & 1 deletion src/channels/DiscordStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class DiscordStatus {
},

async handleError(interaction: TrackedInteraction, error: EndUserError | Error | unknown): Promise<void> {

if (error instanceof EndUserError) {
const parentError: Error | undefined | unknown = error.parentError;
let message: string;
Expand Down
Loading

0 comments on commit 59f5420

Please sign in to comment.