Skip to content

Commit f7f09cd

Browse files
committed
Send message to Discord when reaching 50, 100, ..., users
1 parent 501c92c commit f7f09cd

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

backend/api/src/create-user.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {createSupabaseDirectClient} from 'shared/supabase/init'
1313
import {insert} from 'shared/supabase/utils'
1414
import {convertPrivateUser, convertUser} from 'common/supabase/users'
1515
import {getBucket} from "shared/firebase-utils";
16-
import {sendDiscordNewUser} from "common/discord/core";
16+
import {sendDiscordMessage} from "common/discord/core";
1717

1818
export const createUser: APIHandler<'create-user'> = async (
1919
props,
@@ -130,10 +130,36 @@ export const createUser: APIHandler<'create-user'> = async (
130130
console.log('Failed to track create profile', e)
131131
}
132132
try {
133-
await sendDiscordNewUser(user)
133+
await sendDiscordMessage(
134+
`**${user.name}** just created a profile at https://www.compassmeet.com/${user.username}`,
135+
'members',
136+
)
134137
} catch (e) {
135138
console.log('Failed to send discord new user', e)
136139
}
140+
try {
141+
const nProfiles = await pg.one<number>(
142+
`SELECT count(*) FROM users`,
143+
[],
144+
(r) => Number(r.count)
145+
)
146+
147+
const isMilestone = (n: number) => {
148+
return (
149+
[15, 20, 30, 40].includes(n) || // early milestones
150+
n % 50 === 0
151+
)
152+
}
153+
if (isMilestone(nProfiles)) {
154+
await sendDiscordMessage(
155+
`We just reached **${nProfiles}** total profiles! 🎉`,
156+
'general',
157+
)
158+
}
159+
160+
} catch (e) {
161+
console.log('Failed to send discord user milestone', e)
162+
}
137163
}
138164

139165
return {

common/src/discord/core.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import {User} from "common/user";
2-
3-
export const sendDiscordNewUser = async (user: User) => {
4-
const webhookUrl = process.env.DISCORD_WEBHOOK_NEW_USERS
1+
export const sendDiscordMessage = async (content: string, channel: string) => {
2+
const webhookUrl = {
3+
members: process.env.DISCORD_WEBHOOK_MEMBERS,
4+
general: process.env.DISCORD_WEBHOOK_GENERAL,
5+
}[channel]
56

67
if (!webhookUrl) return
78

89
const response = await fetch(webhookUrl!, {
910
method: 'POST',
1011
headers: {'Content-Type': 'application/json'},
11-
body: JSON.stringify({content: `**${user.name}** just created a profile at https://www.compassmeet.com/${user.username}`}),
12+
body: JSON.stringify({content}),
1213
})
1314

1415
if (!response.ok) {

common/src/secrets.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export const secrets = (
1818
'RESEND_KEY',
1919
'COMPASS_API_KEY',
2020
'NEXT_PUBLIC_FIREBASE_API_KEY',
21-
'DISCORD_WEBHOOK_NEW_USERS',
21+
'DISCORD_WEBHOOK_MEMBERS',
22+
'DISCORD_WEBHOOK_GENERAL',
2223
// Some typescript voodoo to keep the string literal types while being not readonly.
2324
] as const
2425
).concat()

0 commit comments

Comments
 (0)