Skip to content

Commit

Permalink
Add supporter role hoisting
Browse files Browse the repository at this point in the history
  • Loading branch information
encode42 committed Apr 30, 2024
1 parent 2d5be66 commit 672e10f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ VERIFIED_ROLE=

MINIMUM_MESSAGES=30
MESSAGE_COOLDOWN=3

SUPPORTER_ROLE=
DONATOR_ROLE=
BOOSTER_ROLE=
2 changes: 1 addition & 1 deletion src/discord/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { string } from "../util/env";
log.info("Creating Discord client...");

export const client = new Client({
"intents": [GatewayIntentBits.MessageContent, GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]
"intents": [GatewayIntentBits.MessageContent, GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers]
});

const success = !!(await client.login(string("DISCORD_TOKEN")));
Expand Down
46 changes: 46 additions & 0 deletions src/discord/events/checkSupporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Events } from "discord.js";
import { client } from "../client";
import { log } from "../../log";
import { string } from "../../util/env";

const roles = {
"supporter": string("SUPPORTER_ROLE"),
"donator": string("DONATOR_ROLE"),
"booster": string("BOOSTER_ROLE")
};

client.on(Events.GuildMemberUpdate, (old, current) => {
const hadSupporter = old.roles.cache.has(roles.supporter);
const hasSupporter = current.roles.cache.has(roles.supporter);
if (hadSupporter !== hasSupporter) {
return;
}

const hadDonator = old.roles.cache.has(roles.donator);
const hasDonator = current.roles.cache.has(roles.donator);

const hadBooster = old.roles.cache.has(roles.booster);
const hasBooster = current.roles.cache.has(roles.booster);

log.debug(`Roles updated for ${current.displayName}.`);
log.debug(`Donator role: ${hadDonator} -> ${hasDonator}`);
log.debug(`Booster role: ${hadBooster} -> ${hasBooster}`);

if (hadDonator === hasDonator && hadBooster === hasBooster) {
log.debug("Nothing changed! Not modifying roles.");
return;
}

if ((!hadDonator && hasDonator) || (!hadBooster && hasBooster)) {
log.debug("Adding the supporter role!");

current.roles.add(roles.supporter);
return;
}

if (hadBooster && !hasBooster) {
log.debug("Removing the supporter role!");

current.roles.remove(roles.supporter);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { log } from "../../log";
const roleId = string("VERIFIED_ROLE");
const minimumMessages = number("MINIMUM_MESSAGES");

// TODO: Abstract this so it can be used for other roles
let roleValid = false;
for (const [_, guild] of client.guilds.cache) {
const roles = await guild.roles.fetch();
Expand Down
1 change: 1 addition & 0 deletions src/discord/events/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { log } from "../../log";
const self = basename(__filename);
const path = resolve(__dirname);

// TODO: Abstract this so it can be used for other listeners (commands)
export async function register() {
log.info("Registering all Discord listeners...");

Expand Down

0 comments on commit 672e10f

Please sign in to comment.