Skip to content

Commit

Permalink
add state lead committee bypass for state pings (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewdcario authored Mar 11, 2024
1 parent 3725103 commit c1ffa88
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ LINKED_EMOJI=🔗
REFUSED_EMOJI=
STATE_LEAD_RENAMEABLE_CHANNELIDS=
STATE_LEAD_ROLE_ID=
STATE_COMMITTEE_ROLE_ID=
REGIONAL_ROLE_ID=
SME_ROLE_IDS=
WEBSITE_FORM_FILLED_ROLE_ID=
Expand Down
5 changes: 2 additions & 3 deletions src/commands/chat/execution/lead/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ChatInputCommandInteraction, MessageCreateOptions, PermissionFlagsBits
} from 'discord.js';
import {
Channels, getSMERole, isMemberStateLead, memberStates, states
Channels, getSMERole, hasStateRole, isMemberStateLead, states
} from '../../../../structures';

/**
Expand Down Expand Up @@ -47,7 +47,6 @@ export default async function ping(interaction: ChatInputCommandInteraction<'cac
const stateRole = stateAbbreviation && interaction.guild.roles.cache.find((r) => stateAbbreviation.toLowerCase() === r.name.toLowerCase());

const isStateLead = stateAbbreviation && isMemberStateLead(interaction.member);
const hasStateRole = memberStates(interaction.member).some((r) => r.id === stateRole?.id);

if (!stateChannel && !smeChannel) {
return interaction.followUp({
Expand All @@ -70,7 +69,7 @@ export default async function ping(interaction: ChatInputCommandInteraction<'cac
});
}

if (!hasStateRole) {
if (!hasStateRole(interaction.member, stateRole)) {
return interaction.followUp({
content: t({
key: 'ping-no-state-role',
Expand Down
2 changes: 1 addition & 1 deletion src/structures/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { isMemberStateLead, isStateLeadRole, memberStates, states } from './states';
export { hasStateRole, isMemberStateLead, isStateLeadRole, memberStates, states } from './states';

export * as prototype from './prototypes';

Expand Down
13 changes: 13 additions & 0 deletions src/structures/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from 'discord.js';

const stateLeadRoleID = process.env.STATE_LEAD_ROLE_ID;
const stateOrganizingCommitteeRoleID = process.env.STATE_COMMITTEE_ROLE_ID;

export interface state {
name: string;
Expand Down Expand Up @@ -72,7 +73,15 @@ export const statesConfig = [
export const states = new Collection<StateAbbreviation, state>();
statesConfig.map((s) => states.set(s.abbreviation.toLocaleLowerCase() as StateAbbreviation, s));

export function isStateCommitteeMember(member: GuildMember) {
if (!stateOrganizingCommitteeRoleID) throw Error('Missing STATE_COMMITTEE_ROLE_ID in .env');
const bypassRole = member.guild.roles.cache.get(stateOrganizingCommitteeRoleID);
if (!bypassRole) throw Error('STATE_COMMITTEE_ROLE_ID is not a valid role');
return member.roles.cache.has(stateOrganizingCommitteeRoleID);
}

export function isMemberStateLead(member: GuildMember) {
if (isStateCommitteeMember(member)) return true;
if (!stateLeadRoleID) throw Error('Missing STATE_LEAD_ROLE_ID in .env');
const role = member.guild.roles.cache.get(stateLeadRoleID);
if (!role) throw Error('Invalid role ID please check STATE_LEAD_ROLE_ID in .env');
Expand All @@ -87,3 +96,7 @@ export function isStateLeadRole(role: Role) {
export function memberStates(member: GuildMember) {
return member.roles.cache.filter((r) => states.has(r.name.toLowerCase() as StateAbbreviation));
}

export function hasStateRole(member: GuildMember, stateRole: Role) {
return memberStates(member).some((r) => r.id === stateRole.id) || isStateCommitteeMember(member);
}

0 comments on commit c1ffa88

Please sign in to comment.