Send message to Discord when reaching 50, 100, ..., users

This commit is contained in:
MartinBraquet
2025-09-28 21:09:11 +02:00
parent 501c92c350
commit f7f09cd9e5
3 changed files with 36 additions and 8 deletions

View File

@@ -13,7 +13,7 @@ import {createSupabaseDirectClient} from 'shared/supabase/init'
import {insert} from 'shared/supabase/utils'
import {convertPrivateUser, convertUser} from 'common/supabase/users'
import {getBucket} from "shared/firebase-utils";
import {sendDiscordNewUser} from "common/discord/core";
import {sendDiscordMessage} from "common/discord/core";
export const createUser: APIHandler<'create-user'> = async (
props,
@@ -130,10 +130,36 @@ export const createUser: APIHandler<'create-user'> = async (
console.log('Failed to track create profile', e)
}
try {
await sendDiscordNewUser(user)
await sendDiscordMessage(
`**${user.name}** just created a profile at https://www.compassmeet.com/${user.username}`,
'members',
)
} catch (e) {
console.log('Failed to send discord new user', e)
}
try {
const nProfiles = await pg.one<number>(
`SELECT count(*) FROM users`,
[],
(r) => Number(r.count)
)
const isMilestone = (n: number) => {
return (
[15, 20, 30, 40].includes(n) || // early milestones
n % 50 === 0
)
}
if (isMilestone(nProfiles)) {
await sendDiscordMessage(
`We just reached **${nProfiles}** total profiles! 🎉`,
'general',
)
}
} catch (e) {
console.log('Failed to send discord user milestone', e)
}
}
return {

View File

@@ -1,14 +1,15 @@
import {User} from "common/user";
export const sendDiscordNewUser = async (user: User) => {
const webhookUrl = process.env.DISCORD_WEBHOOK_NEW_USERS
export const sendDiscordMessage = async (content: string, channel: string) => {
const webhookUrl = {
members: process.env.DISCORD_WEBHOOK_MEMBERS,
general: process.env.DISCORD_WEBHOOK_GENERAL,
}[channel]
if (!webhookUrl) return
const response = await fetch(webhookUrl!, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({content: `**${user.name}** just created a profile at https://www.compassmeet.com/${user.username}`}),
body: JSON.stringify({content}),
})
if (!response.ok) {

View File

@@ -18,7 +18,8 @@ export const secrets = (
'RESEND_KEY',
'COMPASS_API_KEY',
'NEXT_PUBLIC_FIREBASE_API_KEY',
'DISCORD_WEBHOOK_NEW_USERS',
'DISCORD_WEBHOOK_MEMBERS',
'DISCORD_WEBHOOK_GENERAL',
// Some typescript voodoo to keep the string literal types while being not readonly.
] as const
).concat()