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

New channel permissions helper #293

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Changes from 2 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
46 changes: 46 additions & 0 deletions discord-scripts/channel-management.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
Client,
GuildChannel,
PermissionOverwrites,
TextChannel,
} from "discord.js"
import { Robot } from "hubot"

async function sendChannelPermissions(
channel: GuildChannel,
permissions: Map<string, PermissionOverwrites>,
) {
if (channel.parent && channel.parent.name === "defense") {
const textChannel = channel as TextChannel
let permissionsText =
"This channel is now configured with the following permissions from the base category:\n"
permissions.forEach((perm: PermissionOverwrites) => {
const roleName =
channel.guild.roles.cache.get(perm.id)?.name || "Unknown Role/User"
const allowedPermissions =
perm.allow.toArray().join(", ") || "no permissions"
const deniedPermissions =
perm.deny.toArray().join(", ") || "no permissions"
permissionsText += `${roleName}: Allow: ${allowedPermissions} | Deny: ${deniedPermissions}\n`
})
await textChannel.send(permissionsText)
}
}

export default async function manageChannelPermissions(
discordClient: Client,
robot: Robot,
) {
const { application } = discordClient

if (application) {
discordClient.on("channelCreate", async (channel) => {
if (channel.parent && channel.parent.name === "defense") {
const permissions = channel.parent.permissionOverwrites.cache
await channel.permissionOverwrites.set(permissions)
robot.logger.info("Channel permissions set to base category")
sendChannelPermissions(channel, permissions)
}
})
}
}
Loading