Added messageCreate event, will work on Asynchronous Caching
This commit is contained in:
		
							parent
							
								
									e8347891d7
								
							
						
					
					
						commit
						be3bec017c
					
				
					 5 changed files with 29 additions and 4 deletions
				
			
		|  | @ -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, | ||||
|  |  | |||
							
								
								
									
										15
									
								
								src/gateway/handlers/messageCreate.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/gateway/handlers/messageCreate.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -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) | ||||
| } | ||||
|  | @ -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 { | ||||
|  |  | |||
|  | @ -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 { | ||||
|  |  | |||
|  | @ -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, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue