/
discord.ts
58 lines (51 loc) · 1.93 KB
/
discord.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { REST } from "@discordjs/rest";
import { API } from "@discordjs/core";
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
import z from "zod";
// hide-code
const client = new TriggerClient({ id: "api-reference" });
// end-hide-code
// SDK: https://discord.js.org/docs/packages/core/1.0.1
// To get the Discord bot token follow the instructions here:
// https://discord.com/developers/docs/getting-started
// Bot needs to be added to a server to be able to send messages.
// Oauth URL generator to add the bot to a server. Scopes: bot, send messages
// Create REST and WebSocket managers directly
const rest = new REST({ version: "10" }).setToken(
process.env.DISCORD_BOT_TOKEN!
);
// Create a client to emit relevant events.
const discordApi = new API(rest);
client.defineJob({
id: "discord-send-message",
name: "Discord send message",
version: "1.0.0",
trigger: eventTrigger({
name: "discord-send-message",
schema: z.object({
// To get the channel ID, right click on the channel and click "Copy ID".
// NB: You need to enable developer mode in Discord settings.
channelId: z.string(),
content: z.string(), // The message content
}),
}),
run: async (payload, io, ctx) => {
const { channelId, content } = payload;
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
await io.runTask(
"Discord send message",
async () => {
// See more https://discord.js.org/docs/packages/core/1.0.1/ChannelsAPI:Class
const channelsAPI = discordApi.channels;
await channelsAPI.createMessage(channelId, { content });
},
// Add metadata to the task to improve how it displays in the logs
{ name: "Discord send message", icon: "discord" }
);
},
});
// hide-code
// These lines can be removed if you don't want to use express
import { createExpressServer } from "@trigger.dev/express";
createExpressServer(client);
// end-hide-code