Skip to content

Commit

Permalink
feat(customcommands): add randomizing of responses (#5644)
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Mar 15, 2023
1 parent 01a34b1 commit 340e5eb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/database/entity/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export class Commands extends BaseEntity {
@Column({ nullable: true, type: String })
group: string | null;

@Column({ default: false })
areResponsesRandomized: boolean;

@Column({ type: (process.env.TYPEORM_CONNECTION ?? 'better-sqlite3') !== 'better-sqlite3' ? 'json' : 'simple-json' })
responses: {
id: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class addRandomResponsesToggle1675089806900 implements MigrationInterface {
name = 'addRandomResponsesToggle1675089806900';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`commands\` ADD \`areResponsesRandomized\` tinyint NOT NULL DEFAULT 0`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
return;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class addRandomResponsesToggle1675089806900 implements MigrationInterface {
name = 'addRandomResponsesToggle1675089806900';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "commands" ADD "areResponsesRandomized" boolean NOT NULL DEFAULT false`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
return;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class addRandomResponsesToggle1675089806900 implements MigrationInterface {
name = 'addRandomResponsesToggle1675089806900';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_1a8c40f0a581447776c325cb4f"`);
await queryRunner.query(`CREATE TABLE "temporary_commands" ("id" varchar PRIMARY KEY NOT NULL, "command" varchar NOT NULL, "enabled" boolean NOT NULL, "visible" boolean NOT NULL, "group" varchar, "responses" text NOT NULL, "areResponsesRandomized" boolean NOT NULL DEFAULT (0))`);
await queryRunner.query(`INSERT INTO "temporary_commands"("id", "command", "enabled", "visible", "group", "responses") SELECT "id", "command", "enabled", "visible", "group", "responses" FROM "commands"`);
await queryRunner.query(`DROP TABLE "commands"`);
await queryRunner.query(`ALTER TABLE "temporary_commands" RENAME TO "commands"`);
await queryRunner.query(`CREATE INDEX "IDX_1a8c40f0a581447776c325cb4f" ON "commands" ("command") `);
}

public async down(queryRunner: QueryRunner): Promise<void> {
return;
}

}
21 changes: 11 additions & 10 deletions src/systems/customcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ import {
} from '@entity/commands';
import * as constants from '@sogebot/ui-helpers/constants';
import { validateOrReject } from 'class-validator';
import _, { merge } from 'lodash';
import { cloneDeep, merge, orderBy, shuffle } from 'lodash';
import { v4 } from 'uuid';

import System from './_interface';
import { parserReply } from '../commons';
import {
command, default_permission, helper,
parser,
timer,
} from '../decorators';
import Expects from '../expects';
import System from './_interface';

import { v4 } from 'uuid';
import { checkFilter } from '~/helpers/checkFilter';
import {
getAllCountOfCommandUsage, getCountOfCommandUsage, incrementCountOfCommandUsage, resetCountOfCommandUsage,
} from '~/helpers/commands/count';
import { prepare } from '~/helpers/commons';
import { info, warning } from '~/helpers/log';
import { app } from '~/helpers/panel';
import { get } from '~/helpers/permissions/get';
import { defaultPermissions } from '~/helpers/permissions/defaultPermissions';
import { check } from '~/helpers/permissions/check';
import { defaultPermissions } from '~/helpers/permissions/defaultPermissions';
import { get } from '~/helpers/permissions/get';
import { adminMiddleware } from '~/socket';
import { translate } from '~/translate';

Expand Down Expand Up @@ -254,7 +254,7 @@ class CustomCommands extends System {
const db_commands = (await Commands.find()).filter(o => o.command === cmdArray.join(' '));
for (const cmd of db_commands) {
commandsSearchProgress.push({
cmdArray: _.cloneDeep(cmdArray),
cmdArray: cloneDeep(cmdArray),
command: cmd,
});
}
Expand Down Expand Up @@ -302,7 +302,8 @@ class CustomCommands extends System {
}
}

for (const r of _.orderBy(cmd.command.responses, 'order', 'asc')) {
const responses = cmd.command.areResponsesRandomized ? shuffle(cmd.command.responses) : orderBy(cmd.command.responses, 'order', 'asc');
for (const r of responses) {
let permission = r.permission ?? groupPermission;
// show warning if null permission
if (!permission) {
Expand All @@ -325,7 +326,7 @@ class CustomCommands extends System {
}

if (!opts.quiet) {
this.sendResponse(_.cloneDeep(_responses), {
this.sendResponse(cloneDeep(_responses), {
param, sender: opts.sender, command: cmd.command.command, processedCommands: opts.processedCommands, discord: opts.discord, id: opts.id,
});
}
Expand All @@ -348,7 +349,7 @@ class CustomCommands extends System {
if (!cmd) {
// print commands
const _commands = commands.filter(o => o.visible && o.enabled);
const response = (_commands.length === 0 ? translate('customcmds.list-is-empty') : translate('customcmds.list-is-not-empty').replace(/\$list/g, _.orderBy(_commands, 'command').map(o => o.command).join(', ')));
const response = (_commands.length === 0 ? translate('customcmds.list-is-empty') : translate('customcmds.list-is-not-empty').replace(/\$list/g, orderBy(_commands, 'command').map(o => o.command).join(', ')));
return [{ response, ...opts }];
} else {
// print responses
Expand All @@ -357,7 +358,7 @@ class CustomCommands extends System {
if (!command_with_responses || command_with_responses.responses.length === 0) {
return [{ response: prepare('customcmds.list-of-responses-is-empty', { command: cmd }), ...opts }];
}
return Promise.all(_.orderBy(command_with_responses.responses, 'order', 'asc').map(async(r) => {
return Promise.all(orderBy(command_with_responses.responses, 'order', 'asc').map(async(r) => {
const perm = r.permission ? await get(r.permission) : { name: '-- unset --' };
const response = prepare('customcmds.response', {
command: cmd, index: ++r.order, response: r.response, after: r.stopIfExecuted ? '_' : 'v', permission: perm?.name ?? 'n/a',
Expand Down

0 comments on commit 340e5eb

Please sign in to comment.