Skip to content

Commit 6b45b73

Browse files
Techbot121Meta Construct
authored andcommitted
delete role if it's empty
1 parent a0de3dc commit 6b45b73

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

app/services/discord/modules/commands/Role.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,10 @@ export const SlashRoleCommand: SlashCommand = {
409409
initialize: async bot => {
410410
bot.discord.on("guildMemberRemove", async member => {
411411
const role = member.getCustomRole;
412-
if (role) await role.delete("User left the Guild...");
412+
if (role) role.delete("User left the Guild...");
413+
});
414+
bot.discord.on("roleUpdate", (oldRole, newRole) => {
415+
if (oldRole.isCustomRole && oldRole.members.size === 0) oldRole.delete("Role empty...");
413416
});
414417
},
415418
};

extensions/discord.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ declare module "discord.js" {
3838
hasCustomRole: boolean;
3939
getCustomRole: Discord.Role | undefined;
4040
}
41+
interface Role {
42+
readonly isCustomRole: boolean;
43+
}
4144
}
4245

4346
Object.defineProperty(Discord.User.prototype, "mention", {
@@ -52,14 +55,20 @@ Object.defineProperty(Discord.GuildMember.prototype, "mention", {
5255
},
5356
});
5457

58+
Object.defineProperty(Discord.Role.prototype, "isCustomRole", {
59+
get(this: Discord.Role) {
60+
return this.name.endsWith("\u2063");
61+
},
62+
});
63+
5564
Object.defineProperty(Discord.GuildMember.prototype, "hasCustomRole", {
5665
get(this: Discord.GuildMember) {
57-
return this.roles.cache.some(role => role.name.endsWith("\u2063"));
66+
return this.roles.cache.some(role => role.isCustomRole);
5867
},
5968
});
6069

6170
Object.defineProperty(Discord.GuildMember.prototype, "getCustomRole", {
6271
get(this: Discord.GuildMember) {
63-
return this.roles.cache.find(role => role.name.endsWith("\u2063"));
72+
return this.roles.cache.find(role => role.isCustomRole);
6473
},
6574
});

0 commit comments

Comments
 (0)