107 lines
3.6 KiB
TypeScript
107 lines
3.6 KiB
TypeScript
import 'dotenv/config'
|
|
import { type Task, type WorkerUtils } from 'graphile-worker';
|
|
import { type ChatInputCommandInteraction, Client, Events, GatewayIntentBits, Partials, Message, TextChannel } from 'discord.js'
|
|
|
|
|
|
// export interface DiscordMessageUpdateJob extends Job {
|
|
// data: {
|
|
// captureJobId: string;
|
|
// }
|
|
// }
|
|
|
|
interface Payload {
|
|
discord_message_id: string;
|
|
capture_job_id: string;
|
|
}
|
|
|
|
function assertPayload(payload: any): asserts payload is Payload {
|
|
if (typeof payload !== "object" || !payload) throw new Error("invalid payload");
|
|
if (typeof payload.discord_message_id !== "string") throw new Error("invalid discord_message_id");
|
|
if (typeof payload.capture_job_id.to !== "string") throw new Error("invalid capture_job_id");
|
|
}
|
|
|
|
|
|
if (!process.env.AUTOMATION_USER_JWT) throw new Error(`AUTOMATION_USER_JWT was missing from env`);
|
|
if (!process.env.DISCORD_TOKEN) throw new Error("DISCORD_TOKEN was missing from env");
|
|
if (!process.env.DISCORD_CHANNEL_ID) throw new Error("DISCORD_CHANNEL_ID was missing from env");
|
|
if (!process.env.DISCORD_GUILD_ID) throw new Error("DISCORD_GUILD_ID was missing from env");
|
|
|
|
/**
|
|
* discordMessageUpdate is the task where we edit a previously sent discord message to display new status information sent to us from @futureporn/capture
|
|
*
|
|
* Sometimes the update is changing the state, one of Pending|Recording|Aborted|Ended.
|
|
* Sometimes the update is updating the Filesize of the recording in-progress
|
|
* Sometimes the update is adding a thumbnail image to the message
|
|
*/
|
|
export default async function discordMessageUpdate <Task> (payload: Payload) {
|
|
assertPayload(payload)
|
|
// const { captureJobId } = job.data
|
|
console.log(`discordMessageUpdate job has begun with captureJobId=${payload.capture_job_id}`)
|
|
|
|
|
|
// // find the discord_interactions record containing the captureJobId
|
|
// const res = await fetch(`http://postgrest.futureporn.svc.cluster.local:9000/discord_interactions?capture_job_id=eq.${captureJobId}`)
|
|
// if (!res.ok) throw new Error('failed to fetch the discord_interactions');
|
|
// const body = await res.json() as DiscordInteraction
|
|
// console.log('discord_interactions as follows')
|
|
// console.log(body)
|
|
|
|
// // create a discord.js client
|
|
// const client = new Client({
|
|
// intents: [
|
|
// GatewayIntentBits.Guilds,
|
|
// GatewayIntentBits.GuildMessages
|
|
// ],
|
|
// partials: [
|
|
// Partials.Message,
|
|
// Partials.Channel,
|
|
// ]
|
|
// });
|
|
|
|
// // const messageManager = client.
|
|
// // const guild = client.guilds.cache.get(process.env.DISCORD_GUILD_ID!);
|
|
// // if (!guild) throw new Error('guild was undefined')
|
|
|
|
|
|
// const channel = await client.channels.fetch(process.env.DISCORD_CHANNEL_ID!) as TextChannel
|
|
// if (!channel) throw new Error(`discord channel was undefined`);
|
|
|
|
// // console.log('we got the following channel')
|
|
// // console.log(channel)
|
|
|
|
// const message = await channel.messages.fetch(body.discord_message_id)
|
|
|
|
// console.log('we got the following message')
|
|
// console.log(message)
|
|
|
|
// get the message
|
|
// client.channel.messages.get()
|
|
// client.rest.
|
|
|
|
|
|
|
|
// const message: Message = {
|
|
// id: body.discord_message_id
|
|
// };
|
|
// await message.fetchReference()
|
|
// message.edit('test message update payload thingy')
|
|
|
|
|
|
// client.rest.updateMessage(message, "My new content");
|
|
// TextChannel.fetchMessage(msgId).then(console.log);
|
|
|
|
|
|
|
|
// TextChannel
|
|
|
|
// channel.messages.fetch(`Your Message ID`).then(message => {
|
|
// message.edit("New message Text");
|
|
// }).catch(err => {
|
|
// console.error(err);
|
|
// });
|
|
|
|
// using the discord_interaction's discord_message_id, use discord.js to update the discord message.
|
|
|
|
|
|
}
|