Update pollReaction system to handle 3rd party emojis
This commit is contained in:
		
							parent
							
								
									31629186c0
								
							
						
					
					
						commit
						63e4ba7bc3
					
				|  | @ -1,6 +1,6 @@ | |||
| export const config = { | ||||
| 	'name': 'Sweeper Bot', // Name of the bot
 | ||||
| 	'version': '0.2.2', // Version of the bot
 | ||||
| 	'version': '0.2.3', // Version of the bot
 | ||||
| 	'token': 'the_bot_token', // Discord API Token for this bot
 | ||||
| 	'localtoken': 'local_testing_token', // Discord API Token for a secondary OPTIONAL testing bot, THIS MUST BE DIFFERENT FROM "token"
 | ||||
| 	'prefix': 's!', // Prefix for all commands
 | ||||
|  |  | |||
							
								
								
									
										2
									
								
								deps.ts
								
								
								
								
							
							
						
						
									
										2
									
								
								deps.ts
								
								
								
								
							|  | @ -6,7 +6,7 @@ export const botId = getBotIdFromToken(LOCALMODE ? config.localtoken : config.to | |||
| 
 | ||||
| export { ActivityTypes, createBot, editBotNickname, editBotStatus, Intents, sendMessage, startBot } from 'https://deno.land/x/discordeno@13.0.0/mod.ts'; | ||||
| 
 | ||||
| export type { Bot, CreateMessage, EventHandlers, Guild, Message } from 'https://deno.land/x/discordeno@13.0.0/mod.ts'; | ||||
| export type { Bot, CreateMessage, Emoji, EventHandlers, Guild, Member, Message, User } from 'https://deno.land/x/discordeno@13.0.0/mod.ts'; | ||||
| 
 | ||||
| export { Client } from 'https://deno.land/x/mysql@v2.10.2/mod.ts'; | ||||
| 
 | ||||
|  |  | |||
|  | @ -12,6 +12,7 @@ events.guildCreate = eventHandlers.guildCreate; | |||
| events.guildDelete = eventHandlers.guildDelete; | ||||
| events.messageCreate = eventHandlers.messageCreate; | ||||
| events.messageUpdate = eventHandlers.messageUpdate; | ||||
| events.reactionAdd = eventHandlers.reactionAdd; | ||||
| 
 | ||||
| if (DEVMODE) { | ||||
| 	events.debug = eventHandlers.debug; | ||||
|  |  | |||
|  | @ -4,6 +4,7 @@ import { guildDelete } from './guildDelete.ts'; | |||
| import { debug } from './debug.ts'; | ||||
| import { messageCreate } from './messageCreate.ts'; | ||||
| import { messageUpdate } from './messageUpdate.ts'; | ||||
| import { reactionAdd } from './reactionAdd.ts'; | ||||
| 
 | ||||
| export default { | ||||
| 	ready, | ||||
|  | @ -12,4 +13,5 @@ export default { | |||
| 	debug, | ||||
| 	messageCreate, | ||||
| 	messageUpdate, | ||||
| 	reactionAdd, | ||||
| }; | ||||
|  |  | |||
|  | @ -10,10 +10,11 @@ import { | |||
| 	Message, | ||||
| } from '../../deps.ts'; | ||||
| import commands from '../commands/_index.ts'; | ||||
| import { pollReactions } from '../functions/pollReactions.ts'; | ||||
| import functions from '../functions/_index.ts'; | ||||
| import utils from '../utils.ts'; | ||||
| 
 | ||||
| export const messageCreate = (bot: Bot, message: Message) => { | ||||
| export const messageCreate = async (bot: Bot, message: Message) => { | ||||
| 	// Ignore all other bots
 | ||||
| 	if (message.isFromBot) return; | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1,12 @@ | |||
| import config from '../../config.ts'; | ||||
| import { | ||||
| 	// Discordeno deps
 | ||||
| 	Bot, | ||||
| } from '../../deps.ts'; | ||||
| import { ReactionAdd } from '../types/eventTypes.ts'; | ||||
| 
 | ||||
| export const reactionAdd = (bot: Bot, payload: ReactionAdd) => { | ||||
| 	if (config.pollChannels.includes(payload.channelId)) { | ||||
| 		console.log(payload); | ||||
| 	} | ||||
| }; | ||||
|  | @ -1,4 +1,4 @@ | |||
| import { pollReactions } from "./pollReactions.ts"; | ||||
| import { pollReactions } from './pollReactions.ts'; | ||||
| 
 | ||||
| export default { | ||||
| 	pollReactions, | ||||
|  |  | |||
|  | @ -1,3 +1,4 @@ | |||
| import config from '../../config.ts'; | ||||
| import { | ||||
| 	// Discordeno deps
 | ||||
| 	Bot, | ||||
|  | @ -11,7 +12,7 @@ export const pollReactions = async (bot: Bot, message: Message, update = false) | |||
| 		// Emoji RegExp
 | ||||
| 		const unicodeEmojis = '(\\p{Emoji_Presentation}|\\p{Extended_Pictographic})'; | ||||
| 		const unicodeEmojiRX = `(${unicodeEmojis}(\u200d${unicodeEmojis})*)`; | ||||
| 		const discordEmojiRX = '(:[a-zA-Z\\d_]+:\\d+)'; | ||||
| 		const discordEmojiRX = '(a?:[a-zA-Z\\d_]+:\\d+)'; | ||||
| 		const allEmojiRX = new RegExp(`${unicodeEmojiRX}|${discordEmojiRX}`, 'gu'); | ||||
| 
 | ||||
| 		// Get list of emojis in message
 | ||||
|  | @ -28,15 +29,24 @@ export const pollReactions = async (bot: Bot, message: Message, update = false) | |||
| 					if (reaction.emoji.name) { | ||||
| 						// Make emoji name that matches our allEmojis array format
 | ||||
| 						const emojiName = reaction.emoji.id ? `:${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name; | ||||
| 						if (!allEmojis.includes(emojiName)) { | ||||
| 							bot.helpers.deleteReaction(message.channelId, message.id, emojiName).catch((e: Error) => utils.commonLoggers.reactionDeleteError('pollReactions.ts:32', message, e, emojiName)); | ||||
| 						} | ||||
| 						await bot.helpers.deleteReaction(message.channelId, message.id, emojiName).catch((e: Error) => utils.commonLoggers.reactionDeleteError('pollReactions.ts:32', message, e, emojiName)); | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		// Finally, add all reactions to the message
 | ||||
| 		bot.helpers.addReactions(message.channelId, message.id, allEmojis, true).catch((e: Error) => utils.commonLoggers.reactionAddError('pollReactions.ts:40', message, e, allEmojis.toString())); | ||||
| 		for (const emoji of allEmojis) { | ||||
| 			await bot.helpers.addReaction(message.channelId, message.id, emoji).catch(async (_err) => { | ||||
| 				try { | ||||
| 					const [animated, emojiName, emojiId] = emoji.split(':'); | ||||
| 					const newEmoji = await bot.helpers.createEmoji(config.devServer, {name: emojiName, image: `https://cdn.discordapp.com/emojis/${emojiId}.${animated ? 'gif' : 'webp'}`}) | ||||
| 					await bot.helpers.addReaction(message.channelId, message.id, `:${newEmoji.name}:${newEmoji.id}`); | ||||
| 					await bot.helpers.deleteEmoji(config.devServer, newEmoji.id || 0n); | ||||
| 				} catch (e) { | ||||
| 					utils.commonLoggers.reactionAddError('pollReactions.ts:45', message, e, emoji); | ||||
| 				} | ||||
| 			}); | ||||
| 		} | ||||
| 	} | ||||
| }; | ||||
|  |  | |||
|  | @ -0,0 +1,11 @@ | |||
| import { Emoji, Member, User } from '../../deps.ts'; | ||||
| 
 | ||||
| export type ReactionAdd = { | ||||
| 	userId: bigint; | ||||
| 	channelId: bigint; | ||||
| 	messageId: bigint; | ||||
| 	guildId?: bigint; | ||||
| 	member?: Member; | ||||
| 	user?: User; | ||||
| 	emoji: Emoji; | ||||
| }; | ||||
		Loading…
	
		Reference in New Issue