From 51149f1ff955d7d13cd1f302508db8bdfaa3dd2a Mon Sep 17 00:00:00 2001 From: Erik Zuuring Date: Fri, 9 Feb 2024 11:46:26 +0200 Subject: [PATCH 1/2] Guild scraper ### Notes This adds a guild scraper into Valkyrie to grab server information about roles, permissions and channel names for the eventually integration into coda. ### To-do --- discord-scripts/guild-scraper.ts | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 discord-scripts/guild-scraper.ts diff --git a/discord-scripts/guild-scraper.ts b/discord-scripts/guild-scraper.ts new file mode 100644 index 00000000..40bcb9fc --- /dev/null +++ b/discord-scripts/guild-scraper.ts @@ -0,0 +1,46 @@ +import { Client, TextChannel, OverwriteType } from "discord.js" +import { Robot } from "hubot" + +export default async function guildScraper(discordClient: Client, robot: Robot): Promise { + const { application } = discordClient + + if (application) { + robot.logger.info("Guild scraper loaded this info from connected guild") + + discordClient.guilds.cache.forEach(async (guild) => { + try { + const roles = await guild.roles.fetch() + + roles.forEach((role) => { + robot.logger.info(`Role: ${role.name}`) + robot.logger.info("Permissions:") + role.permissions.toArray().forEach((permission) => { + robot.logger.info(permission) + }) + }) + + } catch (error) { + robot.logger.error(`Error while scrapping roles in guild ${guild.id}:`, error) + } + }) + + discordClient.channels.cache.forEach((channel) => { + if (channel instanceof TextChannel) { + robot.logger.info(`Channel name: ${channel.name}`) + + const rolePermissions: string[] = [] + + channel.permissionOverwrites.cache.forEach((overwrite) => { + if (overwrite.type === OverwriteType.Role) { + const role = channel.guild.roles.cache.get(overwrite.id) + if (role) { + rolePermissions.push(role.name) + } + } + }) + + robot.logger.info(`Assigned roles: ${rolePermissions.join(', ')}`) + } + }) + } +} From da0b326841c04c9477af972823cb506a852ebd34 Mon Sep 17 00:00:00 2001 From: Erik Zuuring Date: Mon, 12 Feb 2024 14:13:20 +0200 Subject: [PATCH 2/2] Setup data for json package This get's the role information and packages it up to a array formatted by the list of roles and with a list of each permission set. --- discord-scripts/guild-scraper.ts | 114 ++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 39 deletions(-) diff --git a/discord-scripts/guild-scraper.ts b/discord-scripts/guild-scraper.ts index 40bcb9fc..54f3a680 100644 --- a/discord-scripts/guild-scraper.ts +++ b/discord-scripts/guild-scraper.ts @@ -1,46 +1,82 @@ import { Client, TextChannel, OverwriteType } from "discord.js" import { Robot } from "hubot" +import axios from "axios" -export default async function guildScraper(discordClient: Client, robot: Robot): Promise { - const { application } = discordClient - - if (application) { - robot.logger.info("Guild scraper loaded this info from connected guild") - - discordClient.guilds.cache.forEach(async (guild) => { - try { - const roles = await guild.roles.fetch() - - roles.forEach((role) => { - robot.logger.info(`Role: ${role.name}`) - robot.logger.info("Permissions:") - role.permissions.toArray().forEach((permission) => { - robot.logger.info(permission) - }) - }) - - } catch (error) { - robot.logger.error(`Error while scrapping roles in guild ${guild.id}:`, error) - } - }) +export default async function guildScraper( + discordClient: Client, + robot: Robot, +): Promise { + const { application } = discordClient + + if (application) { + robot.logger.info("Guild scraper loaded this info from connected guild") + + discordClient.guilds.cache.forEach(async (guild) => { + try { + const roles = await guild.roles.fetch() + const webhookUrl = process.env.CODA_WEBHOOK_URL + const apiToken = process.env.CODA_WEBHOOK_APIKEY + + if (typeof webhookUrl !== "string") { + throw new Error("Webhook URL is not defined.") + } + + if (typeof apiToken !== "string") { + throw new Error("API Token is not defined.") + } - discordClient.channels.cache.forEach((channel) => { - if (channel instanceof TextChannel) { - robot.logger.info(`Channel name: ${channel.name}`) - - const rolePermissions: string[] = [] - - channel.permissionOverwrites.cache.forEach((overwrite) => { - if (overwrite.type === OverwriteType.Role) { - const role = channel.guild.roles.cache.get(overwrite.id) - if (role) { - rolePermissions.push(role.name) - } - } - }) - - robot.logger.info(`Assigned roles: ${rolePermissions.join(', ')}`) + const rolesData = roles.map((role) => ({ + name: role.name, + permissions: role.permissions.toArray(), + })) + + robot.logger.info( + "Collected roles data for guild ${guild.id}: ${JSON.stringify(rolesData)}", + ) + + await axios + .post(webhookUrl, rolesData, { + headers: { + Authorization: `Bearer ${apiToken}`, + "Content-Type": "application/json", + }, + }) + .then(() => + robot.logger.info( + `sent roles data to webhook for guild ${guild.id}`, + ), + ) + .catch((error: any) => + robot.logger.error( + `error sending roles data to webhook for guild ${guild.id}:`, + error, + ), + ) + } catch (error) { + robot.logger.error( + `error while scraping roles in guild ${guild.id}:`, + error, + ) + } + }) + + discordClient.channels.cache.forEach((channel) => { + if (channel instanceof TextChannel) { + robot.logger.info(`Channel name: ${channel.name}`) + + const rolePermissions: string[] = [] + + channel.permissionOverwrites.cache.forEach((overwrite) => { + if (overwrite.type === OverwriteType.Role) { + const role = channel.guild.roles.cache.get(overwrite.id) + if (role) { + rolePermissions.push(role.name) } + } }) - } + + robot.logger.info(`Assigned roles: ${rolePermissions.join(", ")}`) + } + }) + } }