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

Guild/Role scraper #291

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
82 changes: 82 additions & 0 deletions discord-scripts/guild-scraper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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<void> {
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.")
}

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)}",

Check failure on line 34 in discord-scripts/guild-scraper.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected template string expression
)

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) =>

Check failure on line 49 in discord-scripts/guild-scraper.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
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(", ")}`)
}
})
}
}
Loading