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

Send discord memberlist to n8n #268

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
54 changes: 50 additions & 4 deletions discord-scripts/fjord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
ApplicationCommandOptionType,
Client,
CommandInteraction,
Guild,
GuildMember,
TextChannel,
} from "discord.js"
import axios from "axios"
Expand All @@ -10,6 +12,7 @@ import { Robot } from "hubot"
// This is the WIP discord implementation of commands to trigger certain workflows on the thesis n8n platform. Most of the integration uses webhooks and chat commands with response headers .
export default async function manageFjord(discordClient: Client, robot: Robot) {
const { application } = discordClient
const webhookUrl = process.env.HUBOT_N8N_WEBHOOK

if (application) {
const existingFjordCommand = (await application.commands.fetch()).find(
Expand Down Expand Up @@ -121,6 +124,53 @@ export default async function manageFjord(discordClient: Client, robot: Robot) {
}

if (process.env.HUBOT_N8N_WEBHOOK) {
if (discordClient.user) {
const handleGuild = async (guild: Guild) => {
await guild.members.fetch()

const membersList: {
username: string
nickname: string | null
id: string
}[] = []

guild.members.cache.forEach((member: GuildMember) => {
membersList.push({
username: member.user.username,
nickname: member.nickname,
id: member.id,
})
})

const membersListString = encodeURIComponent(
JSON.stringify(membersList),
)

const options = {
headers: {
workflowType: "member-list",
},
}

await axios
.get(`${webhookUrl}?membersList=${membersListString}`, options)
.then(() => {
robot.logger.info("Discord Memberlist sent to n8n")
})
.catch((error) => {
robot.logger.info(`Memberlist failed to send: ${error.message}`)
})
}

const storeMemberList = async () => {
const guilds = Array.from(discordClient.guilds.cache.values())
const guildPromises = guilds.map(handleGuild)
await Promise.all(guildPromises)
}

storeMemberList()
}

discordClient.on("interactionCreate", async (interaction) => {
if (
(interaction.isButton() && interaction.customId.startsWith("debug")) ||
Expand Down Expand Up @@ -165,7 +215,6 @@ export default async function manageFjord(discordClient: Client, robot: Robot) {
const repositoryOwner = repositoryOwnerOption.value
const repositoryName = repositoryNameOption.value

const webhookUrl = process.env.HUBOT_N8N_WEBHOOK
const queryParams = new URLSearchParams({
repositoryOwner,
repositoryName,
Expand Down Expand Up @@ -218,7 +267,6 @@ export default async function manageFjord(discordClient: Client, robot: Robot) {
const repositoryOwner = repositoryOwnerOption.value
const repositoryName = repositoryNameOption.value

const webhookUrl = process.env.HUBOT_N8N_WEBHOOK
const queryParams = new URLSearchParams({
repositoryOwner,
repositoryName,
Expand Down Expand Up @@ -272,7 +320,6 @@ export default async function manageFjord(discordClient: Client, robot: Robot) {
const repositoryOwner = repositoryOwnerOption.value
const repositoryName = repositoryNameOption.value

const webhookUrl = process.env.HUBOT_N8N_WEBHOOK
const queryParams = new URLSearchParams({
repositoryOwner,
repositoryName,
Expand Down Expand Up @@ -322,7 +369,6 @@ export default async function manageFjord(discordClient: Client, robot: Robot) {
) {
const workflowName = workflowNameOption.value

const webhookUrl = process.env.HUBOT_N8N_WEBHOOK
const queryParams = new URLSearchParams({
workflowName,
})
Expand Down