From 0a27e8683bd68cdcb9f63c65c4a8a922f842a33a Mon Sep 17 00:00:00 2001 From: Shell1010 Date: Mon, 22 Jan 2024 03:41:25 +0000 Subject: [PATCH] Member cahcing + get channel --- selfcord/api/events.py | 8 ++--- selfcord/bot.py | 4 +-- selfcord/models/__init__.py | 2 +- selfcord/models/event_models.py | 58 +++++++++++++++++++++++++++++++-- selfcord/models/guild.py | 2 +- selfcord/models/message.py | 4 +-- 6 files changed, 65 insertions(+), 13 deletions(-) diff --git a/selfcord/api/events.py b/selfcord/api/events.py index 7fc7bac..c9aacd5 100644 --- a/selfcord/api/events.py +++ b/selfcord/api/events.py @@ -2,7 +2,7 @@ from time import perf_counter from aioconsole import aprint import asyncio -from ..models import Guild, Convert, User, Message, Member, MessageAck, PresenceUpdate, DMChannel, MessageAddReaction +from ..models import Guild, Convert, User, Message, Member, MessageAck, PresenceUpdate, DMChannel, MessageAddReaction, MemberListUpdate import ujson @@ -240,7 +240,7 @@ async def handle_channel_create(self, data: dict): await self.bot.emit("channel_create", channel) async def handle_channel_delete(self, data: dict): - deleted_channel = self.bot.fetch_channel(data['id']) + deleted_channel = self.bot.get_channel(data['id']) await self.bot.emit("channel_delete", deleted_channel) del deleted_channel @@ -261,8 +261,8 @@ async def handle_guild_delete(self, data: dict): del guild async def handle_guild_member_list_update(self, data: dict): - # for op in data['ops']: - # print(len(data['ops']), op, len(data['ops'])) + MemberListUpdate(data, self.bot) + pass async def handle_presence_update(self, data: dict): diff --git a/selfcord/bot.py b/selfcord/bot.py index 871ad11..b02d600 100644 --- a/selfcord/bot.py +++ b/selfcord/bot.py @@ -12,6 +12,7 @@ import sys import time import urllib + from collections import defaultdict from traceback import format_exception from typing import TYPE_CHECKING, Optional, Literal @@ -366,7 +367,6 @@ async def help(ctx): except: pass - print("here running") for token, bot in zip(tokens, self.bots): asyncio.create_task(bot.runner(token, True)) @@ -517,7 +517,7 @@ def fetch_user(self, user_id: str) -> Optional[User]: return return self.cached_users.get(user_id) - def fetch_channel(self, channel_id: str) -> Optional[Messageable]: + def get_channel(self, channel_id: str) -> Optional[Messageable]: if channel_id is None: return return self.cached_channels.get(channel_id) diff --git a/selfcord/models/__init__.py b/selfcord/models/__init__.py index c933ca1..886f8ad 100644 --- a/selfcord/models/__init__.py +++ b/selfcord/models/__init__.py @@ -20,4 +20,4 @@ Messageable ) from .message import Message, MessageAck -from .event_models import PresenceUpdate, MessageAddReaction \ No newline at end of file +from .event_models import PresenceUpdate, MessageAddReaction, MemberListUpdate \ No newline at end of file diff --git a/selfcord/models/event_models.py b/selfcord/models/event_models.py index c407d48..c5d780c 100644 --- a/selfcord/models/event_models.py +++ b/selfcord/models/event_models.py @@ -1,7 +1,8 @@ from __future__ import annotations +import itertools from typing import Optional -from .users import User, Status +from .users import User, Status, Member from .guild import Emoji class PresenceUpdate: @@ -34,7 +35,7 @@ def update(self, payload): self.burst = payload.get("burst", False) self.message_id = payload.get("message_id") self.channel_id = payload.get("channel_id") - self.channel = self.bot.fetch_channel(self.channel_id) + self.channel = self.bot.get_channel(self.channel_id) self.message = self.bot.fetch_message(self.message_id) self.message_author_id = payload.get("message_author_id") self.message_author = self.bot.fetch_user(self.message_author_id) @@ -43,4 +44,55 @@ def update(self, payload): self.type = payload.get("type") self.emoji = Emoji(payload['emoji'], self.bot) if payload.get("emoji") is not None else None - \ No newline at end of file +class MemberListUpdate: + def __init__(self, payload: dict, bot) -> None: + self.bot = bot + self.http = bot.http + self.update(payload) + + def update(self, payload): + self.member_count = payload.get("member_count") + self.id = payload.get("id") + self.guild_id = payload.get("guild_id") + + self.groups = [] + self.ops = [] + for group, op in itertools.zip_longest(payload.get("groups", []), payload.get("ops", [])): + if group is not None: + group.update({"guild_id": self.guild_id}) + self.groups.append(group) + + if op is not None: + op.update({"guild_id": self.guild_id}) + self.ops.append(op) + +class MemberOp(): + def __init__(self, payload: dict, bot) -> None: + self.bot = bot + self.http = bot.http + self.update(payload) + + def update(self, payload): + self.op = payload.get("op") + self.range = payload.get("range") + self.items = payload.get("items", []) + self.index = payload.get("index") + self.guild_id = payload.get("guild_id") + for item in self.items: + member = item.get("member") + if member is not None: + guild = self.bot.fetch_guild(self.guild_id) + member = Member(member, self.bot) + guild.members.append(member) + + +class Group: + def __init__(self, payload: dict, bot) -> None: + self.bot = bot + self.http = bot.http + self.update(payload) + + def update(self, payload): + self.group_id = payload.get("id") + self.count = payload.get("count") + diff --git a/selfcord/models/guild.py b/selfcord/models/guild.py index 252a870..7e7ed68 100644 --- a/selfcord/models/guild.py +++ b/selfcord/models/guild.py @@ -266,7 +266,7 @@ async def search( async def get_members(self): # Doesn't work yet I'm gonna fix - await self.bot.gateway.chunk_members(self) + pass async def delete(self): await self.http.request( diff --git a/selfcord/models/message.py b/selfcord/models/message.py index 6deea38..a98a106 100644 --- a/selfcord/models/message.py +++ b/selfcord/models/message.py @@ -27,7 +27,7 @@ def guild(self): @property def channel(self): - return self.bot.fetch_channel(self.channel_id) + return self.bot.get_channel(self.channel_id) def update(self, payload: dict): self.id: Optional[str] = payload.get("id") @@ -103,7 +103,7 @@ def __init__(self, payload: dict, bot) -> None: def update(self, payload: dict): self.channel_id: str = payload['channel_id'] - self.channel = self.bot.fetch_channel(self.channel_id) + self.channel = self.bot.get_channel(self.channel_id) self.flags: Optional[int] = payload.get("flags") self.last_viewed = payload['last_viewed'] self.message_id = payload['message_id']