diff --git a/src/gateway/handlers/index.ts b/src/gateway/handlers/index.ts index 595ad28..81c92e2 100644 --- a/src/gateway/handlers/index.ts +++ b/src/gateway/handlers/index.ts @@ -10,6 +10,7 @@ import { guildUpdate } from './guildUpdate.ts' import { guildBanAdd } from './guildBanAdd.ts' import { ready } from './ready.ts' import { guildBanRemove } from './guildBanRemove.ts' +import { messageCreate } from "./messageCreate.ts" export const gatewayHandlers: { [eventCode in GatewayEvents]: GatewayEventHandler | undefined @@ -37,7 +38,7 @@ export const gatewayHandlers: { GUILD_ROLE_DELETE: undefined, INVITE_CREATE: undefined, INVITE_DELETE: undefined, - MESSAGE_CREATE: undefined, + MESSAGE_CREATE: messageCreate, MESSAGE_UPDATE: undefined, MESSAGE_DELETE: undefined, MESSAGE_DELETE_BULK: undefined, diff --git a/src/gateway/handlers/messageCreate.ts b/src/gateway/handlers/messageCreate.ts new file mode 100644 index 0000000..5a5d0eb --- /dev/null +++ b/src/gateway/handlers/messageCreate.ts @@ -0,0 +1,15 @@ +import { Channel } from "../../structures/channel.ts" +import { Message } from "../../structures/message.ts" +import { MessagePayload } from "../../types/channelTypes.ts" +import { Gateway, GatewayEventHandler } from '../index.ts' + +export const messageCreate: GatewayEventHandler = async( + gateway: Gateway, + d: MessagePayload +) => { + let channel = gateway.client.channels.get(d.channel_id) + // Fetch the channel if not cached + if(!channel) channel = (await gateway.client.channels.fetch(d.channel_id) as any) as Channel + let message = new Message(gateway.client, d, channel) + gateway.client.emit('messageCreate', message) +} diff --git a/src/structures/channel.ts b/src/structures/channel.ts index 144b142..8cf18d0 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -15,7 +15,7 @@ export class Channel extends Base { super(client, data) this.type = data.type this.id = data.id - cache.set('channel', this.id, this) + this.client.channels.set(this.id, data) } protected readFromData (data: ChannelPayload): void { diff --git a/src/structures/message.ts b/src/structures/message.ts index 87c1269..d5ec61a 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -15,12 +15,14 @@ import { Member } from './member.ts' import { Embed } from './embed.ts' import { CHANNEL_MESSAGE } from '../types/endpoint.ts' import cache from '../models/cache.ts' +import { Channel } from "./channel.ts" export class Message extends Base { // eslint-disable-next-line @typescript-eslint/prefer-readonly private data: MessagePayload id: string channelID: string + channel: Channel guildID?: string author: User member?: Member @@ -44,7 +46,7 @@ export class Message extends Base { messageReference?: MessageReference flags?: number - constructor (client: Client, data: MessagePayload) { + constructor (client: Client, data: MessagePayload, channel?: Channel, noSave?: boolean) { super(client) this.data = data this.id = data.id @@ -73,7 +75,9 @@ export class Message extends Base { this.application = data.application this.messageReference = data.message_reference this.flags = data.flags - this.client.messages.set(this.id, data) + if(channel) this.channel = channel || this.client.channels.get(this.channelID) + else throw new Error("Message received without Channel (neither in cache)") // unlikely to happen + if(!noSave) this.client.messages.set(this.id, data) } protected readFromData (data: MessagePayload): void { diff --git a/src/test/index.ts b/src/test/index.ts index 1d9c6c4..cfcdbda 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -6,6 +6,7 @@ import { GuildTextChannel } from '../structures/guildTextChannel.ts' import { TextChannel } from '../structures/textChannel.ts' import { Guild } from '../structures/guild.ts' import { User } from '../structures/user.ts' +import { Message } from "../structures/message.ts" const bot = new Client() @@ -61,6 +62,10 @@ bot.on('guildUpdate', (before: Guild, after: Guild) => { console.log('guildUpdate', before.name, after.name) }) +bot.on('messageCreate', (msg: Message) => { + console.log(`${msg.author.tag}: ${msg.content}`) +}) + bot.connect(TOKEN, [ GatewayIntents.GUILD_MEMBERS, GatewayIntents.GUILD_PRESENCES,