diff --git a/cordslite/_modidx.py b/cordslite/_modidx.py index 42e1067..234a799 100644 --- a/cordslite/_modidx.py +++ b/cordslite/_modidx.py @@ -66,6 +66,7 @@ 'cordslite.core.Members': ('core.html#members', 'cordslite/core.py'), 'cordslite.core.Members._repr_html_': ('core.html#members._repr_html_', 'cordslite/core.py'), 'cordslite.core.Message': ('core.html#message', 'cordslite/core.py'), + 'cordslite.core.Message.__init__': ('core.html#message.__init__', 'cordslite/core.py'), 'cordslite.core.Message.__repr__': ('core.html#message.__repr__', 'cordslite/core.py'), 'cordslite.core.Message.attachments': ('core.html#message.attachments', 'cordslite/core.py'), 'cordslite.core.Message.channel': ('core.html#message.channel', 'cordslite/core.py'), diff --git a/cordslite/core.py b/cordslite/core.py index 3421f02..c23fe6a 100644 --- a/cordslite/core.py +++ b/cordslite/core.py @@ -13,7 +13,7 @@ from fastcore.utils import * from nacl.bindings import crypto_aead_xchacha20poly1305_ietf_decrypt as xchacha_decrypt -import asyncio,ffmpeg,httpx,json,opuslib_next,os,random,socket,time +import asyncio,ffmpeg,httpx,json,opuslib_next,os,random,re,socket,time import websockets.asyncio.client # %% ../nbs/00_core.ipynb #d9e0f6cf @@ -86,6 +86,14 @@ async def get_channel(self:DiscordClient, channel_id): # %% ../nbs/00_core.ipynb #461bcb8b class Message(DiscordObject): + def __init__(self, data, client): + super().__init__(data, client) + self.raw_content = self.content + mentions = {m['id']: m['username'] for m in data.get('mentions', [])} + self.data['content'] = re.sub( r'<@!?(\d+)>', + lambda m: f"@{mentions.get(m.group(1), 'unknown')}", + self.content) + def __repr__(self): author = self.author['username'] preview = self.content[:30] + '...' if len(self.content) > 30 else self.content diff --git a/nbs/00_core.ipynb b/nbs/00_core.ipynb index 6534618..9b11df0 100644 --- a/nbs/00_core.ipynb +++ b/nbs/00_core.ipynb @@ -33,7 +33,7 @@ "from fastcore.utils import *\n", "from nacl.bindings import crypto_aead_xchacha20poly1305_ietf_decrypt as xchacha_decrypt\n", "\n", - "import asyncio,ffmpeg,httpx,json,opuslib_next,os,random,socket,time\n", + "import asyncio,ffmpeg,httpx,json,opuslib_next,os,random,re,socket,time\n", "import websockets.asyncio.client" ] }, @@ -355,10 +355,30 @@ "execution_count": null, "id": "461bcb8b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndentationError", + "evalue": "unexpected indent (2441870440.py, line 6)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[6]\u001b[39m\u001b[32m, line 6\u001b[39m", + "\u001b[31m \u001b[39m\u001b[31mself.data['content'] = re.sub( r'<@!?(\\d+)>',\u001b[39m", + " ^", + "\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m unexpected indent" + ] + } + ], "source": [ "#| export\n", "class Message(DiscordObject):\n", + " def __init__(self, data, client):\n", + " super().__init__(data, client)\n", + " self.raw_content = self.content\n", + " mentions = {m['id']: m['username'] for m in data.get('mentions', [])}\n", + " self.data['content'] = re.sub( r'<@!?(\\d+)>',\n", + " lambda m: f\"@{mentions.get(m.group(1), 'unknown')}\",\n", + " self.content)\n", + "\n", " def __repr__(self): \n", " author = self.author['username']\n", " preview = self.content[:30] + '...' if len(self.content) > 30 else self.content\n", @@ -402,14 +422,14 @@ { "data": { "text/html": [ - "
IDAuthorContentDate
1473452262922911961nate.dawgghowdy!2026-02-17
1473455165708435556DBuddyHi, from Solveit!2026-02-17
1473455208284684320DBuddyHere is a file!2026-02-17
1473468026882887823DBuddyHi, from Solveit!2026-02-17
1473468028950675577DBuddyHere is a file!2026-02-17
" + "
IDAuthorContentDate
1473455208284684320DBuddyHere is a file!2026-02-17
1473468026882887823DBuddyHi, from Solveit!2026-02-17
1473468028950675577DBuddyHere is a file!2026-02-17
1473468409684426845DBuddyHi, from Solveit!2026-02-17
1473468411853148376DBuddyHere is a file!2026-02-17
" ], "text/plain": [ - "[Message(id=1473452262922911961, author='nate.dawgg', content='howdy!'),\n", - " Message(id=1473455165708435556, author='DBuddy', content='Hi, from Solveit!'),\n", - " Message(id=1473455208284684320, author='DBuddy', content='Here is a file!'),\n", + "[Message(id=1473455208284684320, author='DBuddy', content='Here is a file!'),\n", " Message(id=1473468026882887823, author='DBuddy', content='Hi, from Solveit!'),\n", - " Message(id=1473468028950675577, author='DBuddy', content='Here is a file!')]" + " Message(id=1473468028950675577, author='DBuddy', content='Here is a file!'),\n", + " Message(id=1473468409684426845, author='DBuddy', content='Hi, from Solveit!'),\n", + " Message(id=1473468411853148376, author='DBuddy', content='Here is a file!')]" ] }, "execution_count": null, @@ -466,10 +486,12 @@ { "data": { "text/html": [ - "
IDAuthorContentDate
1473468028950675577DBuddyHere is a file!2026-02-17
1473468026882887823DBuddyHi, from Solveit!2026-02-17
1473455208284684320DBuddyHere is a file!2026-02-17
1473455165708435556DBuddyHi, from Solveit!2026-02-17
1473452262922911961nate.dawgghowdy!2026-02-17
" + "
IDAuthorContentDate
1473468411853148376DBuddyHere is a file!2026-02-17
1473468409684426845DBuddyHi, from Solveit!2026-02-17
1473468028950675577DBuddyHere is a file!2026-02-17
1473468026882887823DBuddyHi, from Solveit!2026-02-17
1473455208284684320DBuddyHere is a file!2026-02-17
1473455165708435556DBuddyHi, from Solveit!2026-02-17
1473452262922911961nate.dawgghowdy!2026-02-17
" ], "text/plain": [ - "[Message(id=1473468028950675577, author='DBuddy', content='Here is a file!'),\n", + "[Message(id=1473468411853148376, author='DBuddy', content='Here is a file!'),\n", + " Message(id=1473468409684426845, author='DBuddy', content='Hi, from Solveit!'),\n", + " Message(id=1473468028950675577, author='DBuddy', content='Here is a file!'),\n", " Message(id=1473468026882887823, author='DBuddy', content='Hi, from Solveit!'),\n", " Message(id=1473455208284684320, author='DBuddy', content='Here is a file!'),\n", " Message(id=1473455165708435556, author='DBuddy', content='Hi, from Solveit!'),\n", @@ -523,7 +545,7 @@ { "data": { "text/plain": [ - "Message(id=1473468409684426845, author='DBuddy', content='Hi, from Solveit!')" + "Message(id=1473720714816393372, author='DBuddy', content='Hi, from Solveit!')" ] }, "execution_count": null,