Skip to content

Commit

Permalink
feat: Post Discord channel message on points earned (#83)
Browse files Browse the repository at this point in the history
* feat: Post Discord channel message on points earned

* added trigger-example in gitignore

* resolve merge conflicts

---------

Co-authored-by: Johannes <[email protected]>
  • Loading branch information
raysubham and jobenjada authored May 17, 2024
1 parent 0976612 commit ba50d99
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 12 deletions.
8 changes: 6 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ GITHUB_APP_PRIVATE_KEY=""
GITHUB_APP_SLUG=

#S3 Bucket credentials
S3_ACCESS_KEY=
S3_ACCESS_KEY=
S3_BUCKET_NAME=
S3_REGION=
S3_SECRET_KEY=
Expand All @@ -54,4 +54,8 @@ TRIGGER_API_URL=

# Tremendous API credentials
TREMENDOUS_API_KEY=
TREMENDOUS_CAMPAIGN_ID=
TREMENDOUS_CAMPAIGN_ID=

# Discord Credentials
DISCORD_BOT_TOKEN=
DISCORD_CHANNEL_ID=
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ yarn-error.log*
next-env.d.ts

.vscode
.contentlayer
.contentlayer

jobs/examples.ts
4 changes: 4 additions & 0 deletions env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const env = createEnv({
VERCEL_URL: z.string().optional(),
TREMENDOUS_API_KEY: z.string().min(1),
TREMENDOUS_CAMPAIGN_ID: z.string().min(1),
DISCORD_BOT_TOKEN: z.string(),
DISCORD_CHANNEL_ID: z.string(),
},
client: {
NEXT_PUBLIC_APP_URL: z.string().min(1),
Expand Down Expand Up @@ -62,5 +64,7 @@ export const env = createEnv({
TRIGGER_API_URL: process.env.TRIGGER_API_URL,
TREMENDOUS_API_KEY: process.env.TREMENDOUS_API_KEY,
TREMENDOUS_CAMPAIGN_ID: process.env.TREMENDOUS_CAMPAIGN_ID,
DISCORD_BOT_TOKEN: process.env.DISCORD_BOT_TOKEN,
DISCORD_CHANNEL_ID: process.env.DISCORD_CHANNEL_ID,
},
});
32 changes: 32 additions & 0 deletions jobs/discordPointsMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DISCORD_POINTS_MESSAGE_TRIGGER_ID } from "@/lib/constants";
import { discordApi } from "@/lib/discord";
import { triggerDotDevClient } from "@/trigger";
import { eventTrigger } from "@trigger.dev/sdk";
import z from "zod";

triggerDotDevClient.defineJob({
id: "discord-points-message",
name: "Discord award points message",
version: "0.0.1",
trigger: eventTrigger({
name: DISCORD_POINTS_MESSAGE_TRIGGER_ID,
schema: z.object({
channelId: z.string(),
message: z.string(),
}),
}),
run: async (payload, io) => {
const { channelId, message } = payload;

await io.runTask(
"Post awarded message to Discord channel",
async () => {
const channelsAPI = discordApi.channels;
await channelsAPI.createMessage(channelId, { content: message });
},

// Add metadata to the task to improve how it displays in the logs
{ name: "Send message", icon: "discord" }
);
},
});
3 changes: 2 additions & 1 deletion jobs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// export all your job files here

export * from "./issueReminder"
export * from "./issueReminder";
export * from "./discordPointsMessage";
6 changes: 6 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ export const WEBAPP_URL =
// Tremendous
export const TREMENDOUS_API_KEY = env.TREMENDOUS_API_KEY;
export const TREMENDOUS_CAMPAIGN_ID = env.TREMENDOUS_CAMPAIGN_ID;

// Discord
export const DISCORD_BOT_TOKEN = env.DISCORD_BOT_TOKEN;
export const DISCORD_POINTS_MESSAGE_TRIGGER_ID = "discord.pointsMessage";
export const DISCORD_AWARD_POINTS_MESSAGE = (username: string, points: number) =>
`Way to go, ${username} 🎉 You've just earned ${points} points. Your contribution is invaluable to our community 🙌 Keep up the fantastic work and let's keep pushing forward! 💪`;
7 changes: 7 additions & 0 deletions lib/discord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { env } from "@/env.mjs";
// @ts-ignore
import { API } from "@discordjs/core/http-only";
import { REST } from "@discordjs/rest";

const rest = new REST({ version: "10" }).setToken(env.DISCORD_BOT_TOKEN!);
export const discordApi = new API(rest);
10 changes: 10 additions & 0 deletions lib/github/hooks/issue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { env } from "@/env.mjs";
import {
ASSIGN_IDENTIFIER,
AWARD_POINTS_IDENTIFIER,
CREATE_IDENTIFIER,
DISCORD_AWARD_POINTS_MESSAGE,
DISCORD_POINTS_MESSAGE_TRIGGER_ID,
EVENT_TRIGGERS,
LEVEL_LABEL,
ON_NEW_ISSUE,
Expand Down Expand Up @@ -390,6 +393,13 @@ export const onAwardPoints = async (webhooks: Webhooks) => {
ossGgRepo?.id
);
comment = `Awarding ${user.login}: ${points} points!` + " " + comment;
await triggerDotDevClient.sendEvent({
name: DISCORD_POINTS_MESSAGE_TRIGGER_ID,
payload: {
channelId: env.DISCORD_CHANNEL_ID,
message: DISCORD_AWARD_POINTS_MESSAGE(user.name ?? prAuthorUsername, points),
},
});
}
}

Expand Down
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
"@aws-sdk/s3-presigned-post": "^3.577.0",
"@aws-sdk/s3-request-presigner": "^3.577.0",
"@hookform/resolvers": "^3.4.0",
"@aws-sdk/client-s3": "^3.540.0",
"@aws-sdk/s3-presigned-post": "^3.540.0",
"@aws-sdk/s3-request-presigner": "^3.540.0",
"@discordjs/core": "1.2.0",
"@discordjs/rest": "2.3.0",
"@hookform/resolvers": "^3.3.4",
"@next-auth/prisma-adapter": "^1.0.7",
"@octokit/auth-app": "^6.1.1",
"@octokit/rest": "^20.1.1",
Expand Down Expand Up @@ -99,7 +105,12 @@
"typescript": "^5.4.5"
},
"packageManager": "[email protected]",
"pnpm": {
"overrides": {
"undici": "5.27.0"
}
},
"trigger.dev": {
"endpointId": "testing-bHhV"
"endpointId": "oss-gg-dev-ND8k"
}
}
}
116 changes: 112 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions trigger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TriggerClient } from "@trigger.dev/sdk";
import { TRIGGER_API_KEY, TRIGGER_API_URL } from "@/lib/constants";
import { TriggerClient } from "@trigger.dev/sdk";

export const triggerDotDevClient = new TriggerClient({
id: "testing-bHhV",
id: "oss-gg-dev-ND8k",
apiKey: TRIGGER_API_KEY,
apiUrl: TRIGGER_API_URL,
});

0 comments on commit ba50d99

Please sign in to comment.