Skip to content

Commit

Permalink
Member cahcing + get channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Shell1010 committed Jan 22, 2024
1 parent 6f67c34 commit 0a27e86
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
8 changes: 4 additions & 4 deletions selfcord/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions selfcord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion selfcord/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
Messageable
)
from .message import Message, MessageAck
from .event_models import PresenceUpdate, MessageAddReaction
from .event_models import PresenceUpdate, MessageAddReaction, MemberListUpdate
58 changes: 55 additions & 3 deletions selfcord/models/event_models.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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


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")

2 changes: 1 addition & 1 deletion selfcord/models/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions selfcord/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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']
Expand Down

0 comments on commit 0a27e86

Please sign in to comment.