THREAD_CREATE event Listener run method arguments

What I want to do: I want to inspect the id of the parent channel where the thread was created. The code I tried:
import { Listener } from '@sapphire/framework'
import type { ThreadChannel } from 'discord.js'

export class ForumListener extends Listener {
public constructor(context: Listener.Context, options: Listener.Options) {
super(context, {
...options,
emitter: 'ws',
event: 'THREAD_CREATE',
})
}

public run(thread: ThreadChannel, newlyCreated: boolean) {
const string = JSON.stringify(thread, null, 2)
console.log(thread.parentId, newlyCreated)
console.log(string)
}
}
import { Listener } from '@sapphire/framework'
import type { ThreadChannel } from 'discord.js'

export class ForumListener extends Listener {
public constructor(context: Listener.Context, options: Listener.Options) {
super(context, {
...options,
emitter: 'ws',
event: 'THREAD_CREATE',
})
}

public run(thread: ThreadChannel, newlyCreated: boolean) {
const string = JSON.stringify(thread, null, 2)
console.log(thread.parentId, newlyCreated)
console.log(string)
}
}
The result I got:
console.log(thread.parentId, newlyCreated) // undefined 0
console.log(thread.parentId, newlyCreated) // undefined 0
{
"type": 11,
"total_message_sent": 0,
"thread_metadata": {
"locked": false,
"create_timestamp": "2023-03-05T11:39:42.536000+00:00",
"auto_archive_duration": 4320,
"archived": false,
"archive_timestamp": "2023-03-05T11:39:42.536000+00:00"
},
"rate_limit_per_user": 0,
"parent_id": "1081883195349405746",
"owner_id": "xxx",
"newly_created": true,
"name": "test",
"message_count": 0,
"member_count": 1,
"last_message_id": null,
"id": "1081903866334937118",
"guild_id": "xxx",
"flags": 0
}
{
"type": 11,
"total_message_sent": 0,
"thread_metadata": {
"locked": false,
"create_timestamp": "2023-03-05T11:39:42.536000+00:00",
"auto_archive_duration": 4320,
"archived": false,
"archive_timestamp": "2023-03-05T11:39:42.536000+00:00"
},
"rate_limit_per_user": 0,
"parent_id": "1081883195349405746",
"owner_id": "xxx",
"newly_created": true,
"name": "test",
"message_count": 0,
"member_count": 1,
"last_message_id": null,
"id": "1081903866334937118",
"guild_id": "xxx",
"flags": 0
}
stringify on the thread can determine the parent_id, but when accessed as an argument to the run method, it returns undefined. What am I missing? Thanks,
Solution:
Any event emitted by the discordjs websocket, such as THREAD_CREATE, doesn't receive a discordjs class, such as ThreadChannel, as first parameter. It receives the raw discord API data. If you type it as the type from discord-api-types (dunno which) you'll get the right properties in your code. My guess is that it's an APIThreadChannel https://discord-api-types.dev/api/discord-api-types-v10/interface/APIThreadChannel The ws event also receives only ONE argument which is that APIThreadChannel, there is no second argument NewlyCreated. That gets inserted by DiscordJS....
discord-api-types documentation
discord-api-types - Imagine typings
discord-api-types is a simple Node/Deno module that brings up to date typings for Discord's API
Jump to solution
2 Replies
Solution
Favna
Favna2y ago
Any event emitted by the discordjs websocket, such as THREAD_CREATE, doesn't receive a discordjs class, such as ThreadChannel, as first parameter. It receives the raw discord API data. If you type it as the type from discord-api-types (dunno which) you'll get the right properties in your code. My guess is that it's an APIThreadChannel https://discord-api-types.dev/api/discord-api-types-v10/interface/APIThreadChannel The ws event also receives only ONE argument which is that APIThreadChannel, there is no second argument NewlyCreated. That gets inserted by DiscordJS. That said, you're far better off just rewriting it to the DiscordJS client event emitter:
import { Events, Listener } from '@sapphire/framework';
import type { ThreadChannel } from 'discord.js';

export class ForumListener extends Listener<typeof Events.ThreadCreate> {
public constructor(context: Listener.Context, options: Listener.Options) {
super(context, {
...options,
event: Events.ThreadCreate
});
}

public run(thread: ThreadChannel, newlyCreated: boolean) {
console.log(thread.parentId, newlyCreated);
}
}
import { Events, Listener } from '@sapphire/framework';
import type { ThreadChannel } from 'discord.js';

export class ForumListener extends Listener<typeof Events.ThreadCreate> {
public constructor(context: Listener.Context, options: Listener.Options) {
super(context, {
...options,
event: Events.ThreadCreate
});
}

public run(thread: ThreadChannel, newlyCreated: boolean) {
console.log(thread.parentId, newlyCreated);
}
}
discord-api-types documentation
discord-api-types - Imagine typings
discord-api-types is a simple Node/Deno module that brings up to date typings for Discord's API
RYU
RYUOP2y ago
It was a huge help! I verified the normal behavior with the code you wrote. Thank you!
Want results from more Discord servers?
Add your server