From aaa070e705c708e118deb9163fbbe2d117ba237a Mon Sep 17 00:00:00 2001 From: Nathan Cooper Date: Sat, 4 Apr 2026 19:54:15 -0400 Subject: [PATCH] add members utils --- cordslite/_modidx.py | 1 + cordslite/core.py | 15 ++++ nbs/00_core.ipynb | 171 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 159 insertions(+), 28 deletions(-) diff --git a/cordslite/_modidx.py b/cordslite/_modidx.py index 234a799..d1ab5e4 100644 --- a/cordslite/_modidx.py +++ b/cordslite/_modidx.py @@ -59,6 +59,7 @@ 'cordslite.core.Guild': ('core.html#guild', 'cordslite/core.py'), 'cordslite.core.Guild.__repr__': ('core.html#guild.__repr__', 'cordslite/core.py'), 'cordslite.core.Guild.channels': ('core.html#guild.channels', 'cordslite/core.py'), + 'cordslite.core.Guild.find_member': ('core.html#guild.find_member', 'cordslite/core.py'), 'cordslite.core.Guild.members': ('core.html#guild.members', 'cordslite/core.py'), 'cordslite.core.Guild.search': ('core.html#guild.search', 'cordslite/core.py'), 'cordslite.core.Member': ('core.html#member', 'cordslite/core.py'), diff --git a/cordslite/core.py b/cordslite/core.py index c23fe6a..300f8b4 100644 --- a/cordslite/core.py +++ b/cordslite/core.py @@ -139,6 +139,21 @@ async def search(self:Guild, content=None, author_id=None, channel_id=None, ment r = await self.client._req('GET', f'/guilds/{self.id}/messages/search', params=params) return Messages(Message(m[0], self.client) for m in r.json()['messages']) +# %% ../nbs/00_core.ipynb #44642992 +@patch +async def find_member(self:Guild, name): + "Search guild members by name/nick, return first match's user ID or None" + data = (await self.client._req( 'GET', f'/guilds/{self.id}/members/search', + params={'query': name, 'limit': 1})).json() + return data[0]['user']['id'] if data else None + +# %% ../nbs/00_core.ipynb #eeaf8a6f +@patch(as_prop=True) +async def members(self:Guild): + "List all guild members' display names (nick > global_name > username)" + data = (await self.client._req('GET', f'/guilds/{self.id}/members', params={'limit': 1000})).json() + return [m.get('nick') or m['user'].get('global_name') or m['user']['username'] for m in data] + # %% ../nbs/00_core.ipynb #3fd1132c @patch async def send(self:Channel, content='', files=None): diff --git a/nbs/00_core.ipynb b/nbs/00_core.ipynb index 9b11df0..c28b4be 100644 --- a/nbs/00_core.ipynb +++ b/nbs/00_core.ipynb @@ -225,7 +225,9 @@ ] }, "execution_count": null, - "metadata": {}, + "metadata": { + "__type": "Guild" + }, "output_type": "execute_result" } ], @@ -287,18 +289,21 @@ { "data": { "text/html": [ - "
IDNameType
1327046393453613077Text Channels4
1327046393453613078Voice Channels4
1327046393453613079general0
1327046393453613080General2
1327954661960978512private0
" + "
IDNameType
1327046393453613077Text Channels4
1327046393453613078Voice Channels4
1327046393453613079general0
1327046393453613080General2
1327954661960978512private0
1475600987627458812forwarded0
" ], "text/plain": [ "[Channel(id=1327046393453613077, name='Text Channels', type=4),\n", " Channel(id=1327046393453613078, name='Voice Channels', type=4),\n", " Channel(id=1327046393453613079, name='general', type=0),\n", " Channel(id=1327046393453613080, name='General', type=2),\n", - " Channel(id=1327954661960978512, name='private', type=0)]" + " Channel(id=1327954661960978512, name='private', type=0),\n", + " Channel(id=1475600987627458812, name='forwarded', type=0)]" ] }, "execution_count": null, - "metadata": {}, + "metadata": { + "__type": "Channels" + }, "output_type": "execute_result" } ], @@ -319,7 +324,9 @@ ] }, "execution_count": null, - "metadata": {}, + "metadata": { + "__type": "Channel" + }, "output_type": "execute_result" } ], @@ -355,19 +362,7 @@ "execution_count": null, "id": "461bcb8b", "metadata": {}, - "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" - ] - } - ], + "outputs": [], "source": [ "#| export\n", "class Message(DiscordObject):\n", @@ -422,18 +417,20 @@ { "data": { "text/html": [ - "
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
" + "
IDAuthorContentDate
1473468409684426845DBuddyHi, from Solveit!2026-02-17
1473468411853148376DBuddyHere is a file!2026-02-17
1473720714816393372DBuddyHi, from Solveit!2026-02-18
1473721117276373023nate.dawgg@DBuddy2026-02-18
1490061485593071666DBuddyHi from SolveitAI! 👋2026-04-04
" ], "text/plain": [ - "[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!'),\n", - " Message(id=1473468409684426845, author='DBuddy', content='Hi, from Solveit!'),\n", - " Message(id=1473468411853148376, author='DBuddy', content='Here is a file!')]" + "[Message(id=1473468409684426845, author='DBuddy', content='Hi, from Solveit!'),\n", + " Message(id=1473468411853148376, author='DBuddy', content='Here is a file!'),\n", + " Message(id=1473720714816393372, author='DBuddy', content='Hi, from Solveit!'),\n", + " Message(id=1473721117276373023, author='nate.dawgg', content='@DBuddy'),\n", + " Message(id=1490061485593071666, author='DBuddy', content='Hi from SolveitAI! 👋')]" ] }, "execution_count": null, - "metadata": {}, + "metadata": { + "__type": "Messages" + }, "output_type": "execute_result" } ], @@ -486,10 +483,25 @@ { "data": { "text/html": [ - "
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
" + "
IDAuthorContentDate
1490061485593071666DBuddyHi from SolveitAI! 👋2026-04-04
1483815592933720094nate.dawgg2026-03-18
1483812396307976363nate.dawgg2026-03-18
1481403515963183155nate.dawgg2026-03-11
1481022276089544834nate.dawgg2026-03-10
1478064387796242443nate.dawgg2026-03-02
1477046398754099456nate.dawgg2026-02-27
1476505929750155305nate.dawgg2026-02-26
1476303849030422670nate.dawgg2026-02-25
1476195818473263165nate.dawgg2026-02-25
1475827802887553054nate.dawgg2026-02-24
1475601140518359322nate.dawgg2026-02-23
1475601048742793400nate.dawgg2026-02-23
1473721117276373023nate.dawgg@DBuddy2026-02-18
1473720714816393372DBuddyHi, from Solveit!2026-02-18
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=1473468411853148376, author='DBuddy', content='Here is a file!'),\n", + "[Message(id=1490061485593071666, author='DBuddy', content='Hi from SolveitAI! 👋'),\n", + " Message(id=1483815592933720094, author='nate.dawgg', content=''),\n", + " Message(id=1483812396307976363, author='nate.dawgg', content=''),\n", + " Message(id=1481403515963183155, author='nate.dawgg', content=''),\n", + " Message(id=1481022276089544834, author='nate.dawgg', content=''),\n", + " Message(id=1478064387796242443, author='nate.dawgg', content=''),\n", + " Message(id=1477046398754099456, author='nate.dawgg', content=''),\n", + " Message(id=1476505929750155305, author='nate.dawgg', content=''),\n", + " Message(id=1476303849030422670, author='nate.dawgg', content=''),\n", + " Message(id=1476195818473263165, author='nate.dawgg', content=''),\n", + " Message(id=1475827802887553054, author='nate.dawgg', content=''),\n", + " Message(id=1475601140518359322, author='nate.dawgg', content=''),\n", + " Message(id=1475601048742793400, author='nate.dawgg', content=''),\n", + " Message(id=1473721117276373023, author='nate.dawgg', content='@DBuddy'),\n", + " Message(id=1473720714816393372, author='DBuddy', content='Hi, from Solveit!'),\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", @@ -499,7 +511,9 @@ ] }, "execution_count": null, - "metadata": {}, + "metadata": { + "__type": "Messages" + }, "output_type": "execute_result" } ], @@ -507,6 +521,107 @@ "await gld.search(after='2026-02-16')" ] }, + { + "cell_type": "markdown", + "id": "cdd89759", + "metadata": {}, + "source": [ + "Sometimes you need to search by name rather than snowflake ID. `find_member` searches the guild's members by username, nickname, or display name using Discord's member search endpoint, and returns the first match's user ID. This makes it easy to chain into `search`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "44642992", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "@patch\n", + "async def find_member(self:Guild, name):\n", + " \"Search guild members by name/nick, return first match's user ID or None\"\n", + " data = (await self.client._req( 'GET', f'/guilds/{self.id}/members/search',\n", + " params={'query': name, 'limit': 1})).json()\n", + " return data[0]['user']['id'] if data else None" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aafd6bef", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
IDAuthorContentDate
1483815592933720094nate.dawgg2026-03-18
1483812396307976363nate.dawgg2026-03-18
1481403515963183155nate.dawgg2026-03-11
1481022276089544834nate.dawgg2026-03-10
1478064387796242443nate.dawgg2026-03-02
" + ], + "text/plain": [ + "[Message(id=1483815592933720094, author='nate.dawgg', content=''),\n", + " Message(id=1483812396307976363, author='nate.dawgg', content=''),\n", + " Message(id=1481403515963183155, author='nate.dawgg', content=''),\n", + " Message(id=1481022276089544834, author='nate.dawgg', content=''),\n", + " Message(id=1478064387796242443, author='nate.dawgg', content='')]" + ] + }, + "execution_count": null, + "metadata": { + "__type": "Messages" + }, + "output_type": "execute_result" + } + ], + "source": [ + "uid = await gld.find_member('nate')\n", + "await gld.search(author_id=uid, limit=5)" + ] + }, + { + "cell_type": "markdown", + "id": "8aae2813", + "metadata": {}, + "source": [ + "The `members` property fetches all guild members and returns their display names, preferring server nickname over global display name over username. Note: this requires the **Server Members Intent** enabled in the Developer Portal (Bot → Privileged Gateway Intents)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eeaf8a6f", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "@patch(as_prop=True)\n", + "async def members(self:Guild):\n", + " \"List all guild members' display names (nick > global_name > username)\"\n", + " data = (await self.client._req('GET', f'/guilds/{self.id}/members', params={'limit': 1000})).json()\n", + " return [m.get('nick') or m['user'].get('global_name') or m['user']['username'] for m in data]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cdd2e812", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['nathan', 'SearchBuddy', 'DBuddy', 'Dizcord Util Bot', 'Search Agent']" + ] + }, + "execution_count": null, + "metadata": { + "__type": "list" + }, + "output_type": "execute_result" + } + ], + "source": [ + "await gld.members" + ] + }, { "cell_type": "markdown", "id": "89d2bbdd",