Skip to content

Commit

Permalink
Auto Delete Custom Roles (#290)
Browse files Browse the repository at this point in the history
Co-authored-by: Platformer444 <Platformer444@users.noreply.github.com>
Co-authored-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com>
Co-authored-by: RedGuy12 <RedGuy12@users.noreply.github.com>
  • Loading branch information
4 people committed Aug 6, 2023
1 parent 94c7422 commit 60886ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
20 changes: 8 additions & 12 deletions modules/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ defineCommand(
content: `${constants.emojis.statuses.no} You don’t have permission to create a custom role!`,
});

const existingRole = interaction.member.roles
.valueOf()
.find((role) => role.name.startsWith(PREFIX));
const existingRole = getCustomRole(interaction.member);

await interaction.showModal({
title: "Create Custom Role",
Expand Down Expand Up @@ -168,10 +166,7 @@ defineModal("customRole", async (interaction) => {
const name = interaction.fields.fields.get("name")?.value;
const color = interaction.fields.fields.get("color")?.value;

const existingRole =
interaction.member instanceof GuildMember
? interaction.member.roles.valueOf().find((role) => role.name.startsWith(PREFIX))
: undefined;
const existingRole = getCustomRole(interaction.member);

if (!name) {
if (!existingRole) {
Expand Down Expand Up @@ -295,10 +290,7 @@ defineEvent("guildMemberUpdate", async (_, member) => {
if (member.guild.id !== config.guild.id) return;

if (!(await qualifiesForRole(member))) {
await member.roles
.valueOf()
.find((role) => role.name.startsWith(PREFIX))
?.delete("No longer qualifies");
await getCustomRole(member)?.delete("No longer qualifies");
}
});

Expand All @@ -318,7 +310,11 @@ defineEvent("applicationCommandPermissionsUpdate", async (permissions) => {
}
});

async function qualifiesForRole(member: GuildMember) {
export function getCustomRole(member: GuildMember) {
return member.roles.valueOf().find((role) => role.name.startsWith(PREFIX));
}

export async function qualifiesForRole(member: GuildMember) {
if (member.roles.premiumSubscriberRole) return true;

const recentXp = [...recentXpDatabase.data].sort((one, two) => one.time - two.time);
Expand Down
13 changes: 13 additions & 0 deletions modules/xp/weekly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { nth } from "../../util/numbers.js";
import { remindersDatabase, SpecialReminders } from "../reminders.js";
import { getFullWeeklyData, recentXpDatabase, xpDatabase } from "./misc.js";
import constants from "../../common/constants.js";
import { getCustomRole, qualifiesForRole } from "../roles.js";

export async function getChatters() {
const weeklyWinners = getFullWeeklyData();
Expand Down Expand Up @@ -146,6 +147,18 @@ export default async function getWeekly(nextWeeklyDate: Date) {
]);
}

weeklyWinners.forEach(async (weeklyWinner) => {
await config.guild.members.fetch(weeklyWinner.user).then(async (guildMember) => {
if (await qualifiesForRole(guildMember)) return;

const customRole = getCustomRole(guildMember);

if (!customRole) return;

customRole.delete("No longer meets custom role requirements");
});
});

return `__**🏆 Weekly Winners week of ${
[
"January",
Expand Down

0 comments on commit 60886ba

Please sign in to comment.