-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6136ee6
commit addab57
Showing
12 changed files
with
1,686 additions
and
33 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,26 @@ | ||
import { MigrationInterface, QueryFailedError, QueryRunner } from 'typeorm'; | ||
|
||
export class RoleDefaultName1693675771967 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
try { | ||
await queryRunner.query( | ||
`ALTER TABLE \`guild\` ADD \`role_default_name\` varchar(255) NOT NULL`, | ||
); | ||
await queryRunner.query( | ||
'UPDATE `guild` SET `role_default_name` = ""', | ||
); | ||
} catch (e) { | ||
// TODO LOL! | ||
if (!(e instanceof QueryFailedError)) { | ||
throw e; | ||
} | ||
console.log(`IGNORING: ` + e); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE \`guild\` DROP COLUMN \`role_default_name\``, | ||
); | ||
} | ||
} |
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,31 @@ | ||
import { SlashCommandSubcommandBuilder } from 'discord.js'; | ||
import { DB } from '../../../db'; | ||
import { GuildEntity } from '../../../db/entities'; | ||
import { Subcommand } from '../command'; | ||
|
||
const SetRoleDefaultNameCommand: Subcommand = { | ||
data: new SlashCommandSubcommandBuilder() | ||
.setName('role_default_name') | ||
.setDescription( | ||
'The default name of new roles created by the bot. Should be a templated string.', | ||
) | ||
.addStringOption(o => | ||
o | ||
.setName('role_default_name') | ||
.setDescription('The name.') | ||
.setRequired(true), | ||
), | ||
perm: 'mods', | ||
execute: async (interaction, guildEnt) => { | ||
const name = interaction.options.getString('role_default_name', true); | ||
|
||
await DB.getRepository(GuildEntity).update( | ||
{ guild_id: guildEnt.guild_id }, | ||
{ role_default_name: name }, | ||
); | ||
|
||
await interaction.reply(`role_default_name set to \`${name}\`.`); | ||
}, | ||
}; | ||
|
||
export default SetRoleDefaultNameCommand; |
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,66 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { LeaderboardNameData, templateLeaderboardName } from './template_name'; | ||
|
||
describe('role name templating', () => { | ||
const cases: [string, LeaderboardNameData, number][] = [ | ||
[ | ||
'full-game, no sub-categories', | ||
{ game: 'A Game', category: 'A Category' }, | ||
0, | ||
], | ||
[ | ||
'level, no sub-categories', | ||
{ game: 'A Game', level: 'A Level', category: 'A Category' }, | ||
1, | ||
], | ||
[ | ||
'full-game, sub-categories', | ||
{ | ||
game: 'A Game', | ||
category: 'A Category', | ||
subcategories: ['Value 1', 'Value 2'], | ||
}, | ||
2, | ||
], | ||
[ | ||
'level, sub-categories', | ||
{ | ||
game: 'A Game', | ||
level: 'A Level', | ||
category: 'A Category', | ||
subcategories: ['Value 1', 'Value 2'], | ||
}, | ||
3, | ||
], | ||
]; | ||
|
||
describe.each(cases)('%s', (_name, leaderboard, i) => { | ||
const expected = { | ||
'{{%game%}}{{: %level%}} - {{%category%}}{{ (%subcategories%)}} WR': | ||
[ | ||
'A Game - A Category WR', | ||
'A Game: A Level - A Category WR', | ||
'A Game - A Category (Value 1, Value 2) WR', | ||
'A Game: A Level - A Category (Value 1, Value 2) WR', | ||
], | ||
'WR: {{%game%}}{{: %level%}}': [ | ||
'WR: A Game', | ||
'WR: A Game: A Level', | ||
'WR: A Game', | ||
'WR: A Game: A Level', | ||
], | ||
'WR: {{%level%: }}{{%category%}}{{ (%subcategories%)}}': [ | ||
'WR: A Category', | ||
'WR: A Level: A Category', | ||
'WR: A Category (Value 1, Value 2)', | ||
'WR: A Level: A Category (Value 1, Value 2)', | ||
], | ||
} as const; | ||
|
||
it.each(Object.keys(expected))('for template %s', template => { | ||
expect(templateLeaderboardName(template, leaderboard)).toEqual( | ||
expected[template as keyof typeof expected][i], | ||
); | ||
}); | ||
}); | ||
}); |
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,50 @@ | ||
export type LeaderboardNameData = { | ||
game: string; | ||
category: string; | ||
subcategories?: string[]; | ||
level?: string; | ||
}; | ||
|
||
const templateReg = /{{.+?}}/g; | ||
const keyReg = /%.+?%/g; | ||
|
||
export function templateLeaderboardName( | ||
name: string, | ||
leaderboard: LeaderboardNameData, | ||
) { | ||
const names = { | ||
...leaderboard, | ||
subcategories: leaderboard.subcategories?.join(', '), | ||
}; | ||
|
||
let finalName = name; | ||
|
||
const templates = name.matchAll(templateReg); | ||
for (const templateStr of templates) { | ||
const keysToTemplate = templateStr[0].matchAll(keyReg); | ||
let replaceWith = templateStr[0].substring( | ||
2, | ||
templateStr[0].length - 2, | ||
); | ||
|
||
for (const keyToTemplate of keysToTemplate) { | ||
let rawKey = keyToTemplate[0]; | ||
const key = rawKey.substring( | ||
1, | ||
rawKey.length - 1, | ||
) as keyof LeaderboardNameData; | ||
|
||
let value = names[key]; | ||
if (value === undefined) { | ||
replaceWith = ''; | ||
break; | ||
} | ||
|
||
replaceWith = replaceWith.replace(keyToTemplate[0], value); | ||
} | ||
|
||
finalName = finalName.replace(templateStr[0], replaceWith); | ||
} | ||
|
||
return finalName; | ||
} |
Empty file.