Skip to content

Commit

Permalink
feat: allow categories to hide or display their related react roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhuh committed Sep 10, 2023
1 parent 8bb6682 commit e8a7f16
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
9 changes: 9 additions & 0 deletions commands/category/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const enum CommandOptionNames {
Name = 'name',
Description = 'description',
MutuallyExclusive = 'mutually-exclusive',
DisplayRoles = 'display-roles',
RequiredRole = 'required-role',
ExcludedRole = 'excluded-role',
DisplayOrder = 'display-order',
Expand Down Expand Up @@ -44,6 +45,11 @@ export class CreateSubCommand extends SlashSubCommand {
description: 'Make roles from this category mutually exclusive.',
type: ApplicationCommandOptionType.Boolean,
},
{
name: CommandOptionNames.DisplayRoles,
description: 'If you want to display the roles in the embed or not.',
type: ApplicationCommandOptionType.Boolean,
},
{
name: CommandOptionNames.RequiredRole,
description:
Expand Down Expand Up @@ -108,6 +114,8 @@ export class CreateSubCommand extends SlashSubCommand {
const displayString = interaction.options.getString(CommandOptionNames.DisplayOrder);

// Embed styling options
const displayRoles =
interaction.options.getBoolean(CommandOptionNames.DisplayRoles) ?? false;
const imageTypeString = interaction.options.getString(CommandOptionNames.ImageType);
const imageUrl = interaction.options.getString(CommandOptionNames.ImageUrl);
let embedColor = interaction.options.getString(CommandOptionNames.EmbedColor);
Expand Down Expand Up @@ -143,6 +151,7 @@ export class CreateSubCommand extends SlashSubCommand {
name,
description,
mutuallyExclusive,
displayRoles,
requiredRoleId,
excludedRoleId,
displayOrder: displayOrder ?? DisplayType.alpha,
Expand Down
9 changes: 9 additions & 0 deletions commands/category/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const enum CommandOptionNames {
Category = 'category',
Name = 'new-name',
Description = 'new-description',
DisplayRoles = 'display-roles',
MutuallyExclusive = 'mutually-exclusive',
RemoveRoleType = 'remove-role-type',
RequiredRole = 'new-required-role',
Expand Down Expand Up @@ -44,6 +45,11 @@ export class EditSubCommand extends SlashSubCommand {
description: 'Description of the category, use [remove] to remove it.',
type: ApplicationCommandOptionType.String,
},
{
name: CommandOptionNames.DisplayRoles,
description: 'If you want to display the roles in the embed or not.',
type: ApplicationCommandOptionType.Boolean,
},
{
name: CommandOptionNames.MutuallyExclusive,
description:
Expand Down Expand Up @@ -158,6 +164,8 @@ export class EditSubCommand extends SlashSubCommand {
const newName = interaction.options.getString(CommandOptionNames.Name);
const mutuallyExclusive =
interaction.options.getBoolean(CommandOptionNames.MutuallyExclusive);
const displayRoles =
interaction.options.getBoolean(CommandOptionNames.DisplayRoles);
const removeRoleType = interaction.options.getString(CommandOptionNames.RemoveRoleType);
const newRequiredRoleId =
interaction.options.getRole(CommandOptionNames.RequiredRole)?.id ?? undefined;
Expand Down Expand Up @@ -196,6 +204,7 @@ export class EditSubCommand extends SlashSubCommand {
name: newName ?? category.name,
description: newDesc ?? category.description,
mutuallyExclusive: mutuallyExclusive ?? category.mutuallyExclusive,
displayRoles: displayRoles ?? category.displayRoles,
displayOrder: displayOrder ?? category.displayOrder,
imageType: imageType ?? category.imageType,
imageUrl: removeImageUrl ? null : imageUrl ?? category.imageUrl,
Expand Down
6 changes: 3 additions & 3 deletions commands/category/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { reactRoleButtons } from '../../utilities/utilButtons';
import { requiredPermissions } from '../../utilities/utilErrorMessages';
import { reactToMessage } from '../../utilities/utils';
import { SlashSubCommand } from '../command';
import { reactRoleEmbed, reactRoleEmbedless } from '../../utilities/utilEmbedHelpers';
import { reactRoleEmbed, categoryEmbedDescription } from '../../utilities/utilEmbedHelpers';

const enum CommandOptionNames {
MessageLink = 'message-link',
Expand Down Expand Up @@ -210,7 +210,7 @@ export class UpdateSubCommand extends SlashSubCommand {
? []
: [reactRoleEmbed(roles, category, config?.hideEmojis)],
content: config.hideEmbed
? reactRoleEmbedless(roles, category, config?.hideEmojis)
? categoryEmbedDescription(roles, category, config?.hideEmojis)
: '',
components: buttons,
};
Expand Down Expand Up @@ -250,7 +250,7 @@ export class UpdateSubCommand extends SlashSubCommand {
const editedMessage = {
embeds: hideEmbed ? [] : [reactRoleEmbed(roles, category)],
content: hideEmbed
? reactRoleEmbedless(roles, category)
? categoryEmbedDescription(roles, category)
: '',
components: [],
};
Expand Down
4 changes: 2 additions & 2 deletions commands/react/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { reactRoleButtons } from '../../utilities/utilButtons';
import { requiredPermissions } from '../../utilities/utilErrorMessages';
import { reactToMessage } from '../../utilities/utils';
import { SlashSubCommand } from '../command';
import { reactRoleEmbed, reactRoleEmbedless } from '../../utilities/utilEmbedHelpers';
import { reactRoleEmbed, categoryEmbedDescription } from '../../utilities/utilEmbedHelpers';

const enum CommandOptionNames {
Channel = 'channel',
Expand Down Expand Up @@ -230,7 +230,7 @@ export class ChannelSubCommand extends SlashSubCommand {
? [reactRoleEmbed(roles, category, hideEmojis)]
: [],
content: config.hideEmbed
? reactRoleEmbedless(roles, category, hideEmojis)
? categoryEmbedDescription(roles, category, hideEmojis)
: '',
components:
config.reactType === GuildReactType.button
Expand Down
6 changes: 5 additions & 1 deletion src/database/entities/category.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface ICategory {
guildId: string;
name: string;
description?: string | null;
displayRoles: boolean;
mutuallyExclusive?: boolean;
requiredRoleId: string | null;
excludedRoleId: string | null;
Expand All @@ -37,8 +38,11 @@ export class Category extends BaseEntity {
@Column({ type: 'text', nullable: true })
description: string | null;

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

@Column({ default: true })
displayRoles: boolean;

@Column({ type: 'text', nullable: true })
requiredRoleId: string | null;
Expand Down
40 changes: 25 additions & 15 deletions utilities/utilEmbedHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function reactRoleEmbed(
) {
const embed = new EmbedBuilder();

const description = reactRoleEmbedless(
const description = categoryEmbedDescription(
reactRoles,
category,
hideEmojis
Expand All @@ -160,7 +160,7 @@ export function reactRoleEmbed(
try {
embed
.setTitle(category.name)
.setDescription(description)
.setDescription(description || null)
.setColor(category.embedColor ? `#${category.embedColor}` : null);

if (category.imageType === 'card') {
Expand All @@ -175,27 +175,37 @@ export function reactRoleEmbed(
return embed;
}

export function reactRoleEmbedless(
export function categoryEmbedDescription(
reactRoles: ReactRole[],
category: ICategory,
hideEmojis = false
) {
const reactRolesString = reactRolesFormattedString(
reactRoles,
hideEmojis
);

const requiredRole = category.requiredRoleId
? `\nRequired: ${RolePing(category.requiredRoleId)}`
: '';
let reactRolesInfo = '';

const excludedRole = category.excludedRoleId
? `\nExcluded: ${RolePing(category.excludedRoleId)}`
: '';
/**
* Some users want to hide their react roles from their embed
* So hide all info if they dont want it displayed
*/
if (category.displayRoles) {
const reactRolesString = reactRolesFormattedString(
reactRoles,
hideEmojis
);

const requiredRole = category.requiredRoleId
? `\nRequired: ${RolePing(category.requiredRoleId)}`
: '';

const excludedRole = category.excludedRoleId
? `\nExcluded: ${RolePing(category.excludedRoleId)}`
: '';

reactRolesInfo = `${requiredRole}${excludedRole}\n\n${reactRolesString}`;
}

return `${
category.description?.split('\\n').join('\n') ?? ''
}${requiredRole}${excludedRole}\n\n${reactRolesString}`;
}${reactRolesInfo}`;
}

export function tutorialEmbed(pageId: number) {
Expand Down

0 comments on commit e8a7f16

Please sign in to comment.