Merge pull request #68 from Helloyunho/reaction
Add `addReaction` and `removeReaction` feature, Handle null-id emojis
This commit is contained in:
		
						commit
						8d693157da
					
				
					 11 changed files with 115 additions and 22 deletions
				
			
		|  | @ -17,23 +17,29 @@ export const guildEmojiUpdate: GatewayEventHandler = async ( | |||
|     const _updated: EmojiPayload[] = [] | ||||
| 
 | ||||
|     for (const raw of d.emojis) { | ||||
|       const has = emojis.get(raw.id) | ||||
|       const emojiID = raw.id !== null ? raw.id : raw.name | ||||
|       const has = emojis.get(emojiID) | ||||
|       if (has === undefined) { | ||||
|         await guild.emojis.set(raw.id, raw) | ||||
|         const emoji = (await guild.emojis.get(raw.id)) as Emoji | ||||
|         await guild.emojis.set(emojiID, raw) | ||||
|         const emoji = (await guild.emojis.get(emojiID)) as Emoji | ||||
|         added.push(emoji) | ||||
|       } else _updated.push(raw) | ||||
|     } | ||||
| 
 | ||||
|     for (const emoji of emojis.values()) { | ||||
|       const find = _updated.find((e) => emoji.id === e.id) | ||||
|       const emojiID = emoji.id !== null ? emoji.id : emoji.name | ||||
|       const find = _updated.find((e) => { | ||||
|         const eID = e.id !== null ? e.id : e.name | ||||
|         return emojiID === eID | ||||
|       }) | ||||
|       if (find === undefined) { | ||||
|         await guild.emojis.delete(emoji.id) | ||||
|         await guild.emojis.delete(emojiID) | ||||
|         deleted.push(emoji) | ||||
|       } else { | ||||
|         const before = (await guild.emojis.get(find.id)) as Emoji | ||||
|         await guild.emojis.set(find.id, find) | ||||
|         const after = (await guild.emojis.get(find.id)) as Emoji | ||||
|         const foundID = find.id !== null ? find.id : find.name | ||||
|         const before = (await guild.emojis.get(foundID)) as Emoji | ||||
|         await guild.emojis.set(foundID, find) | ||||
|         const after = (await guild.emojis.get(foundID)) as Emoji | ||||
|         updated.push({ before, after }) | ||||
|       } | ||||
|     } | ||||
|  |  | |||
|  | @ -29,15 +29,16 @@ export const messageReactionAdd: GatewayEventHandler = async ( | |||
|     } else return | ||||
|   } | ||||
| 
 | ||||
|   let reaction = await message.reactions.get(d.emoji.id) | ||||
|   const emojiID = d.emoji.id !== null ? d.emoji.id : d.emoji.name | ||||
|   let reaction = await message.reactions.get(emojiID) | ||||
|   if (reaction === undefined) { | ||||
|     await message.reactions.set(d.emoji.id, { | ||||
|     await message.reactions.set(emojiID, { | ||||
|       count: 1, | ||||
|       emoji: d.emoji, | ||||
|       me: d.user_id === gateway.client.user?.id | ||||
|     }) | ||||
|     reaction = ((await message.reactions.get( | ||||
|       d.emoji.id | ||||
|       emojiID | ||||
|     )) as unknown) as MessageReaction | ||||
|   } | ||||
| 
 | ||||
|  |  | |||
|  | @ -27,7 +27,8 @@ export const messageReactionRemove: GatewayEventHandler = async ( | |||
|     } else return | ||||
|   } | ||||
| 
 | ||||
|   const reaction = await message.reactions.get(d.emoji.id) | ||||
|   const emojiID = d.emoji.id !== null ? d.emoji.id : d.emoji.name | ||||
|   const reaction = await message.reactions.get(emojiID) | ||||
|   if (reaction === undefined) return | ||||
| 
 | ||||
|   reaction.users.delete(d.user_id) | ||||
|  |  | |||
|  | @ -19,7 +19,8 @@ export const messageReactionRemoveEmoji: GatewayEventHandler = async ( | |||
|     } else return | ||||
|   } | ||||
| 
 | ||||
|   const reaction = await message.reactions.get(d.emoji.id) | ||||
|   const emojiID = d.emoji.id !== null ? d.emoji.id : d.emoji.name | ||||
|   const reaction = await message.reactions.get(emojiID) | ||||
|   if (reaction === undefined) return | ||||
| 
 | ||||
|   await reaction.users.flush() | ||||
|  |  | |||
|  | @ -87,7 +87,8 @@ export class GuildEmojisManager extends BaseChildManager<EmojiPayload, Emoji> { | |||
|   async flush(): Promise<boolean> { | ||||
|     const arr = await this.array() | ||||
|     for (const elem of arr) { | ||||
|       this.parent.delete(elem.id) | ||||
|       const emojiID = elem.id !== null ? elem.id : elem.name | ||||
|       this.parent.delete(emojiID) | ||||
|     } | ||||
|     return true | ||||
|   } | ||||
|  |  | |||
|  | @ -21,7 +21,9 @@ export class MessageReactionsManager extends BaseManager< | |||
|     const raw = await this._get(id) | ||||
|     if (raw === undefined) return | ||||
| 
 | ||||
|     let emoji = await this.client.emojis.get(raw.emoji.id) | ||||
|     const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name | ||||
| 
 | ||||
|     let emoji = await this.client.emojis.get(emojiID) | ||||
|     if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji) | ||||
| 
 | ||||
|     const reaction = new MessageReaction(this.client, raw, this.message, emoji) | ||||
|  | @ -43,7 +45,8 @@ export class MessageReactionsManager extends BaseManager< | |||
| 
 | ||||
|     return await Promise.all( | ||||
|       arr.map(async (raw) => { | ||||
|         let emoji = await this.client.emojis.get(raw.emoji.id) | ||||
|         const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name | ||||
|         let emoji = await this.client.emojis.get(emojiID) | ||||
|         if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji) | ||||
| 
 | ||||
|         return new MessageReaction(this.client, raw, this.message, emoji) | ||||
|  |  | |||
|  | @ -5,7 +5,7 @@ import { Guild } from './guild.ts' | |||
| import { User } from './user.ts' | ||||
| 
 | ||||
| export class Emoji extends Base { | ||||
|   id: string | ||||
|   id: string | null | ||||
|   name: string | ||||
|   roles?: string[] | ||||
|   user?: User | ||||
|  | @ -16,10 +16,14 @@ export class Emoji extends Base { | |||
|   available?: boolean | ||||
| 
 | ||||
|   get getEmojiString(): string { | ||||
|     if (this.id === null) { | ||||
|       return this.name | ||||
|     } else { | ||||
|       if (this.animated === false) { | ||||
|         return `<:${this.name}:${this.id}>` | ||||
|       } else return `<a:${this.name}:${this.id}>` | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   toString(): string { | ||||
|     return this.getEmojiString | ||||
|  |  | |||
|  | @ -17,6 +17,7 @@ import { TextChannel } from './textChannel.ts' | |||
| import { Guild } from './guild.ts' | ||||
| import { MessageReactionsManager } from '../managers/messageReactions.ts' | ||||
| import { MessageSticker } from './messageSticker.ts' | ||||
| import { Emoji } from './emoji.ts' | ||||
| 
 | ||||
| type AllMessageOptions = MessageOption | Embed | ||||
| 
 | ||||
|  | @ -149,4 +150,15 @@ export class Message extends Base { | |||
|   async delete(): Promise<void> { | ||||
|     return this.client.rest.delete(CHANNEL_MESSAGE(this.channelID, this.id)) | ||||
|   } | ||||
| 
 | ||||
|   async addReaction(emoji: string | Emoji): Promise<void> { | ||||
|     return this.channel.addReaction(this, emoji) | ||||
|   } | ||||
| 
 | ||||
|   async removeReaction( | ||||
|     emoji: string | Emoji, | ||||
|     user?: User | Member | string | ||||
|   ): Promise<void> { | ||||
|     return this.channel.removeReaction(this, emoji, user) | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -13,13 +13,18 @@ import { | |||
| import { | ||||
|   CHANNEL, | ||||
|   CHANNEL_MESSAGE, | ||||
|   CHANNEL_MESSAGES | ||||
|   CHANNEL_MESSAGES, | ||||
|   MESSAGE_REACTION_ME, | ||||
|   MESSAGE_REACTION_USER | ||||
| } from '../types/endpoint.ts' | ||||
| import { Collection } from '../utils/collection.ts' | ||||
| import { Channel } from './channel.ts' | ||||
| import { Embed } from './embed.ts' | ||||
| import { Emoji } from './emoji.ts' | ||||
| import { Guild } from './guild.ts' | ||||
| import { Member } from './member.ts' | ||||
| import { Message } from './message.ts' | ||||
| import { User } from './user.ts' | ||||
| 
 | ||||
| export type AllMessageOptions = MessageOption | Embed | ||||
| 
 | ||||
|  | @ -128,6 +133,54 @@ export class TextChannel extends Channel { | |||
|     return res | ||||
|   } | ||||
| 
 | ||||
|   async addReaction( | ||||
|     message: Message | string, | ||||
|     emoji: Emoji | string | ||||
|   ): Promise<void> { | ||||
|     if (emoji instanceof Emoji) { | ||||
|       emoji = emoji.getEmojiString | ||||
|     } | ||||
|     if (message instanceof Message) { | ||||
|       message = message.id | ||||
|     } | ||||
| 
 | ||||
|     const encodedEmoji = encodeURI(emoji) | ||||
| 
 | ||||
|     await this.client.rest.put( | ||||
|       MESSAGE_REACTION_ME(this.id, message, encodedEmoji) | ||||
|     ) | ||||
|   } | ||||
| 
 | ||||
|   async removeReaction( | ||||
|     message: Message | string, | ||||
|     emoji: Emoji | string, | ||||
|     user?: User | Member | string | ||||
|   ): Promise<void> { | ||||
|     if (emoji instanceof Emoji) { | ||||
|       emoji = emoji.getEmojiString | ||||
|     } | ||||
|     if (message instanceof Message) { | ||||
|       message = message.id | ||||
|     } | ||||
|     if (user !== undefined) { | ||||
|       if (typeof user !== 'string') { | ||||
|         user = user.id | ||||
|       } | ||||
|     } | ||||
| 
 | ||||
|     const encodedEmoji = encodeURI(emoji) | ||||
| 
 | ||||
|     if (user === undefined) { | ||||
|       await this.client.rest.delete( | ||||
|         MESSAGE_REACTION_ME(this.id, message, encodedEmoji) | ||||
|       ) | ||||
|     } else { | ||||
|       await this.client.rest.delete( | ||||
|         MESSAGE_REACTION_USER(this.id, message, encodedEmoji, user) | ||||
|       ) | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * Fetch Messages of a Channel | ||||
|    * @param options Options to configure fetching Messages | ||||
|  |  | |||
|  | @ -112,6 +112,17 @@ client.on('messageCreate', async (msg: Message) => { | |||
|     } else { | ||||
|       msg.channel.send('Failed...') | ||||
|     } | ||||
|   } else if (msg.content === '!react') { | ||||
|     msg.addReaction('🤔') | ||||
|   } | ||||
| }) | ||||
| 
 | ||||
| client.on('messageReactionRemove', (reaction, user) => { | ||||
|   const msg = reaction.message | ||||
| 
 | ||||
|   // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
 | ||||
|   if (reaction.me && reaction.emoji.getEmojiString === '🤔') { | ||||
|     msg.removeReaction(reaction.emoji) | ||||
|   } | ||||
| }) | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,7 +1,7 @@ | |||
| import { UserPayload } from './user.ts' | ||||
| 
 | ||||
| export interface EmojiPayload { | ||||
|   id: string | ||||
|   id: string | null | ||||
|   name: string | ||||
|   roles?: string[] | ||||
|   user?: UserPayload | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue