Created
October 6, 2023 11:18
-
-
Save Slyracoon23/e6a5f6219c597b362a92e57bb56978f9 to your computer and use it in GitHub Desktop.
discord trigger.dev job
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { client } from "@/trigger" | |
import { eventTrigger } from "@trigger.dev/sdk" | |
import { SupabaseManagement, Supabase } from "@trigger.dev/supabase" | |
import { z } from "zod" | |
import _ from "lodash" | |
const supabase = new Supabase({ | |
id: "supabase", | |
supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL!, | |
supabaseKey: process.env.SUPABASE_SERVICE_ROLE_KEY!, | |
}) | |
const botToken = process.env.DISCORD_BOT_TOKEN! // Assuming BOT_TOKEN is set as an environment variable | |
// Define the Trigger.dev job | |
client.defineJob({ | |
id: "discord-fetch-all-messages", | |
name: "Discord - Fetch All Messages", | |
version: "0.0.1", | |
trigger: eventTrigger({ | |
name: "discord.fetch.all.messages", | |
// @ts-ignore -- TODO: fix this | |
schema: z.object({ | |
guild_id: z.string(), | |
org_id: z.string(), | |
}) as any, | |
}), | |
integrations: { | |
supabase, | |
}, | |
run: async (payload, io) => { | |
// Upsert data into Supabase tables | |
const { data: allMessages, error } = await io.supabase.runTask("upsert-discord-data", async (db) => { | |
// Fetch Guild ID from payload | |
const guildId = payload.guild_id | |
const orgId = payload.org_id | |
console.log("payload:", payload) | |
console.log("guildId:", guildId) | |
console.log("orgId:", orgId) | |
// Fetch channels | |
const channelsEndpoint = `https://discord.com/api/v10/guilds/${guildId}/channels` | |
const channelsResponse = await fetch(channelsEndpoint, { | |
method: "GET", | |
headers: { | |
Authorization: `Bot ${botToken}`, | |
"User-Agent": "DiscordBot (https://your-bot-url.com, v1)", | |
}, | |
}) | |
const channels = await channelsResponse.json() | |
// Filter for text channels | |
const textChannels = _.filter(channels, { type: 0 }) | |
// Initialize object to hold all messages | |
let allMessages: any = {} | |
// Fetch messages for each text channel | |
for (const channel of textChannels) { | |
const messagesEndpoint = `https://discord.com/api/v10/channels/${channel.id}/messages?limit=100` | |
const messagesResponse = await fetch(messagesEndpoint, { | |
method: "GET", | |
headers: { | |
Authorization: `Bot ${botToken}`, | |
"User-Agent": "DiscordBot (https://your-bot-url.com, v1)", | |
}, | |
}) | |
if (messagesResponse.ok) { | |
const messages = await messagesResponse.json() | |
// Sort messages by timestamp in ascending order | |
const sortedMessages = _.orderBy(messages, ["timestamp"], ["asc"]) | |
allMessages[channel.id] = { | |
id: channel.id, | |
name: channel.name, | |
posts: sortedMessages, | |
} | |
// Upsert the org with the new guildId | |
const { data: orgData, error: orgError } = await db | |
.from("orgs") | |
.upsert([{ id: orgId, guildId: guildId }]) | |
.select() | |
if (orgError) { | |
io.logger.error("Failed to upsert org with guildId:", orgError) | |
} | |
// Upsert the channel | |
const { data: channelData, error: channelError } = await db | |
.from("discord_channels") | |
.upsert(channel) | |
.select() | |
if (channelError) { | |
io.logger.error("Failed to upsert channel:", channelError) | |
} else { | |
const channelId = channel.id | |
const { error: channelJunctionError } = await db | |
.from("junc_orgs_discord_channels") | |
.upsert([{ org_id: orgId, channel_id: channelId }]) | |
if (channelJunctionError) { | |
io.logger.error( | |
"Failed to upsert org-channel relationship in junction table:", | |
channelJunctionError | |
) | |
} | |
} | |
// Upsert authors and messages | |
for (const message of sortedMessages) { | |
const { data: authorData, error: authorError } = await db | |
.from("discord_members") | |
.upsert(message.author) | |
.select() | |
if (authorError) { | |
io.logger.error("Failed to upsert author:", authorError) | |
} else { | |
const authorId = message.author.id | |
const { error: memberJunctionError } = await db | |
.from("junc_orgs_discord_members") | |
.upsert([{ org_id: orgId, member_id: authorId }]) | |
if (memberJunctionError) { | |
io.logger.error( | |
"Failed to upsert org-member relationship in junction table:", | |
memberJunctionError | |
) | |
} | |
} | |
const sanitizedMessage = _.omit(message, ["author"]) | |
sanitizedMessage.author_id = message.author.id // Adding author_id field | |
const { data: messageData, error: messageError } = await db | |
.from("discord_messages") | |
.upsert(sanitizedMessage) | |
.select() | |
if (messageError) { | |
io.logger.error("Failed to upsert message:", messageError) | |
} else { | |
const messageId = sanitizedMessage.id | |
const { error: messageJunctionError } = await db | |
.from("junc_orgs_discord_messages") | |
.upsert([{ org_id: orgId, message_id: messageId }]) | |
if (messageJunctionError) { | |
io.logger.error( | |
"Failed to upsert org-message relationship in junction table:", | |
messageJunctionError | |
) | |
} | |
} | |
} | |
} else { | |
io.logger.error("Failed to fetch messages:") | |
} | |
// Added break to make it sync only a single discord channel. Remove in the future | |
break; | |
} | |
const { data: updateData, error: updateError } = await db | |
.from("discord_job_queue") | |
.update({ | |
status: "COMPLETED", | |
completed_at: new Date().toISOString(), | |
}) | |
.eq("triggerdev_event_id", io._context.event.id); | |
if (updateError) { | |
io.logger.error("Failed to update discord_job_queue:", updateError) | |
} | |
return allMessages; | |
}) | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment