mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 17:49:09 +00:00
add /roleypoly and /pickable-roles slash commands, fix framework issues
This commit is contained in:
parent
fd7ed13e9d
commit
5c5258ef5e
6 changed files with 329 additions and 19 deletions
|
@ -1,7 +1,7 @@
|
|||
jest.mock('../utils/discord');
|
||||
jest.mock('../utils/legacy');
|
||||
|
||||
import { CategoryType, Features, GuildData } from '@roleypoly/types';
|
||||
import { CategoryType, Features, Guild, GuildData, RoleSafety } from '@roleypoly/types';
|
||||
import { APIGuild, discordFetch } from '../utils/discord';
|
||||
import {
|
||||
fetchLegacyServer,
|
||||
|
@ -9,7 +9,7 @@ import {
|
|||
transformLegacyGuild,
|
||||
} from '../utils/legacy';
|
||||
import { configContext } from '../utils/testHelpers';
|
||||
import { getGuild, getGuildData, getGuildMember } from './getters';
|
||||
import { getGuild, getGuildData, getGuildMember, getPickableRoles } from './getters';
|
||||
|
||||
const mockDiscordFetch = discordFetch as jest.Mock;
|
||||
const mockFetchLegacyServer = fetchLegacyServer as jest.Mock;
|
||||
|
@ -241,3 +241,81 @@ describe('getGuildMember', () => {
|
|||
expect(result!.nick).toBe('test2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPickableRoles', () => {
|
||||
it('returns all pickable roles for a given guild', async () => {
|
||||
const guildData: GuildData = {
|
||||
id: '123',
|
||||
message: 'Hello world!',
|
||||
categories: [
|
||||
{
|
||||
id: '123',
|
||||
name: 'test',
|
||||
position: 0,
|
||||
roles: ['role-1', 'role-2', 'role-unsafe'],
|
||||
hidden: false,
|
||||
type: CategoryType.Multi,
|
||||
},
|
||||
{
|
||||
id: '123',
|
||||
name: 'test',
|
||||
position: 0,
|
||||
roles: ['role-3', 'role-4'],
|
||||
hidden: true,
|
||||
type: CategoryType.Multi,
|
||||
},
|
||||
],
|
||||
features: Features.None,
|
||||
auditLogWebhook: null,
|
||||
accessControl: {
|
||||
allowList: [],
|
||||
blockList: [],
|
||||
blockPending: true,
|
||||
},
|
||||
};
|
||||
|
||||
const guild: Guild = {
|
||||
id: '123',
|
||||
name: 'test',
|
||||
icon: '',
|
||||
roles: [
|
||||
{
|
||||
id: 'role-1',
|
||||
name: 'test',
|
||||
position: 0,
|
||||
managed: false,
|
||||
color: 0,
|
||||
safety: RoleSafety.Safe,
|
||||
permissions: '0',
|
||||
},
|
||||
{
|
||||
id: 'role-3',
|
||||
name: 'test',
|
||||
position: 0,
|
||||
managed: false,
|
||||
color: 0,
|
||||
safety: RoleSafety.Safe,
|
||||
permissions: '0',
|
||||
},
|
||||
{
|
||||
id: 'role-unsafe',
|
||||
name: 'test',
|
||||
position: 0,
|
||||
managed: false,
|
||||
color: 0,
|
||||
safety: RoleSafety.DangerousPermissions,
|
||||
permissions: '0',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = getPickableRoles(guildData, guild);
|
||||
|
||||
expect(result).toMatchObject([
|
||||
{
|
||||
category: guildData.categories[0],
|
||||
roles: [guild.roles[0]],
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,6 +10,7 @@ import {
|
|||
import { fetchLegacyServer, transformLegacyGuild } from '@roleypoly/api/src/utils/legacy';
|
||||
import { evaluatePermission, permissions } from '@roleypoly/misc-utils/hasPermission';
|
||||
import {
|
||||
Category,
|
||||
Features,
|
||||
Guild,
|
||||
GuildData,
|
||||
|
@ -199,3 +200,31 @@ const calculateRoleSafety = (role: Role | APIRole, highestBotRolePosition: numbe
|
|||
|
||||
return safety;
|
||||
};
|
||||
|
||||
export const getPickableRoles = (
|
||||
guildData: GuildData,
|
||||
guild: Guild
|
||||
): { category: Category; roles: Role[] }[] => {
|
||||
const pickableRoles: { category: Category; roles: Role[] }[] = [];
|
||||
|
||||
for (const category of guildData.categories) {
|
||||
if (category.roles.length === 0 || category.hidden) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const roles = category.roles
|
||||
.map((roleID) => guild.roles.find((r) => r.id === roleID))
|
||||
.filter((role) => role !== undefined && role.safety === RoleSafety.Safe) as Role[];
|
||||
|
||||
if (roles.length > 0) {
|
||||
pickableRoles.push({
|
||||
category,
|
||||
roles,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
console.log({ pickableRoles });
|
||||
|
||||
return pickableRoles;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue