From c1ee7d19aae21103e5dbbc9a2ef70c3cc9b7aa4b Mon Sep 17 00:00:00 2001 From: Azaria <34754130+ItsAzaria@users.noreply.github.com> Date: Thu, 19 Feb 2026 17:56:44 +0000 Subject: [PATCH] handle emoji/emote in messages too --- app/Events/MessageCreate.php | 71 +++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/app/Events/MessageCreate.php b/app/Events/MessageCreate.php index 989b64c..ae6d662 100644 --- a/app/Events/MessageCreate.php +++ b/app/Events/MessageCreate.php @@ -21,6 +21,75 @@ class MessageCreate extends Event */ public function handle(Message $message, Discord $discord) { - $this->console()->log('The Message Create event has fired!'); + if ($message->author->bot) { + return; + } + + $customEmotePattern = '//'; + $unicodeEmojiPattern = '/[\x{1F600}-\x{1F64F}\x{1F300}-\x{1F5FF}\x{1F680}-\x{1F6FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}]/u'; + + $content = $message->content; + $author = $message->author; + $guild = $message->guild_id ? $discord->guilds->get('id', $message->guild_id) : null; + + if ($guild === null) { + return; + } + + preg_match_all($customEmotePattern, $content, $customMatches, PREG_SET_ORDER); + preg_match_all($unicodeEmojiPattern, $content, $unicodeMatches); + + $user = \App\Models\User::firstOrCreate( + ['discord_id' => $author->id], + ['username' => $author->username] + ); + + foreach ($customMatches as $match) { + $emoteName = $match[1]; + $emoteId = $match[2]; + $emote = null; + $type = 'STATIC'; + if ($guild && $guild->emojis) { + $emote = $guild->emojis->get('id', $emoteId); + if ($emote && $emote->animated) { + $type = 'ANIMATED'; + } + } + if ($emote) { + $emoteModel = \App\Models\Emote::firstOrCreate( + ['emote_id' => $emoteId], + [ + 'guild_id' => $guild ? $guild->id : null, + 'emote_name' => $emoteName, + 'type' => $type, + ] + ); + $emoteModel->addReaction( + user: $user, + guildId: $guild ? $guild->id : null, + channelId: $message->channel_id, + messageId: $message->id, + unicodeEmoji: null + ); + } + } + + foreach ($unicodeMatches[0] as $emoji) { + $emoteModel = \App\Models\Emote::firstOrCreate( + ['emote_id' => $emoji], + [ + 'guild_id' => $guild ? $guild->id : null, + 'emote_name' => $emoji, + 'type' => 'UNICODE', + ] + ); + $emoteModel->addReaction( + user: $user, + guildId: $guild ? $guild->id : null, + channelId: $message->channel_id, + messageId: $message->id, + unicodeEmoji: $emoji + ); + } } }