Skip to content

Commit

Permalink
Fix member add handler in discord-ws (#1967)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrrikkotua authored Dec 14, 2023
1 parent 3e54b47 commit 5087654
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
4 changes: 2 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@aws-sdk/util-format-url": "^3.226.0",
"@crowd/alerting": "file:../services/libs/alerting",
"@crowd/common": "file:../services/libs/common",
"@crowd/common_services": "file:../services/libs/common_services",
"@crowd/cubejs": "file:../services/libs/cubejs",
"@crowd/feature-flags": "file:../services/libs/feature-flags",
"@crowd/integrations": "file:../services/libs/integrations",
Expand All @@ -67,7 +68,6 @@
"@crowd/temporal": "file:../services/libs/temporal",
"@crowd/tracing": "file:../services/libs/tracing",
"@crowd/types": "file:../services/libs/types",
"@crowd/common_services": "file:../services/libs/common_services",
"@cubejs-client/core": "^0.30.4",
"@google-cloud/storage": "5.3.0",
"@octokit/auth-app": "^3.6.1",
Expand Down Expand Up @@ -99,7 +99,7 @@
"cron-time-generator": "^1.3.0",
"crowd-sentiment": "^1.1.7",
"crypto-js": "^4.1.1",
"discord.js": "^14.7.1",
"discord.js": "^14.14.1",
"dotenv": "8.2.0",
"dotenv-expand": "^8.0.3",
"emoji-dictionary": "^1.0.11",
Expand Down
21 changes: 10 additions & 11 deletions backend/src/bin/discord-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,24 @@ async function spawnClient(
})

// listen to discord events
client.on(Events.GuildMemberAdd, async (m) => {
const member = m as any
client.on(Events.GuildMemberAdd, async (member) => {
// discord.js is cruel. member object here is typed,
// but it has custom toString and toJSON methods
// and they you print and JSON.stringify it
// the structure turns out to be different
await executeIfNotExists(
`member-${member.userId}`,
`discord-ws-member-${member.user.id}-${member.guild.id}`,
cache,
async () => {
logger.debug(
{
member: member.displayName,
guildId: member.guildId ?? member.guild.id,
userId: member.userId,
guildId: member.guild.id,
userId: member.user.id,
},
'Member joined guild!',
)
await processPayload(
DiscordWebsocketEvent.MEMBER_ADDED,
member,
member.guildId ?? member.guild.id,
)
await processPayload(DiscordWebsocketEvent.MEMBER_ADDED, member, member.guild.id)
},
delayMilliseconds,
)
Expand All @@ -167,7 +166,7 @@ async function spawnClient(
client.on(Events.MessageCreate, async (message) => {
if (message.type === MessageType.Default || message.type === MessageType.Reply) {
await executeIfNotExists(
`msg-${message.id}`,
`discord-ws-msg-${message.id}`,
cache,
async () => {
logger.debug(
Expand Down
29 changes: 22 additions & 7 deletions pnpm-lock.yaml

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

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ export const getMember = async (
token: string,
ctx: IProcessStreamContext,
): Promise<DiscordApiMember> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (typeof guildId !== 'string' || guildId.trim() === '') {
throw new Error('Invalid guildId')
}
if (typeof userId !== 'string' || userId.trim() === '') {
throw new Error('Invalid userId')
}
if (typeof token !== 'string' || token.trim() === '') {
throw new Error('Invalid token')
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const config: AxiosRequestConfig<any> = {
method: 'get',
Expand Down

0 comments on commit 5087654

Please sign in to comment.