Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Role management #294

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions discord-scripts/role-management.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Client } from "discord.js"

export default async function manageRole(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add this as an unexported helper to the renamed role-management script from #293 . If we do move to a directory for role management due to complexity, this can end up in a helpers file or similar.

discordClient: Client,
guildId: string,
memberId: string,
roleId: string,
Comment on lines +5 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced we'll find ourselves wanting to do this without having handles to at least the guild and maybe member objects already... For example, the first use we're pondering, which would be in reaction to the guildMemberAdd event, would receive a GuildMember already—allowing us to bypass most of the steps here.

action: "add" | "remove",
): Promise<void> {
if (!discordClient) {
throw new Error("Discord client is not initialized.")
}

const guild = await discordClient.guilds.fetch(guildId)
if (!guild) throw new Error("Guild not found.")

const member = await guild.members.fetch(memberId)
if (!member) throw new Error("Member not found.")

const role = await guild.roles.fetch(roleId)
if (!role) throw new Error("Role not found.")

if (action === "add") {
if (member.roles.cache.has(roleId)) {
return
}
await member.roles.add(role)
} else if (action === "remove") {
if (!member.roles.cache.has(roleId)) {
return
}
await member.roles.remove(role)
}
}
Loading