Fixed discord data orders

This commit is contained in:
Sven Heidemann 2023-09-28 07:31:08 +02:00
parent 77e079d91c
commit 0037a30c11

View File

@ -1,3 +1,4 @@
import discord
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List from cpl_query.extension import List
from discord import Guild from discord import Guild
@ -17,22 +18,24 @@ class GuildQuery(QueryABC):
self.set_field("id", lambda g, *_: g.id) self.set_field("id", lambda g, *_: g.id)
self.set_field("name", lambda g, *_: g.name) self.set_field("name", lambda g, *_: g.name)
self.set_field("channels", self._resolve_channels) self.set_field("channels", self._resolve_channels)
self.set_field("roles", lambda g, *_: g.roles) self.set_field("roles", lambda g, *_: g.roles.order_by(lambda x: x.position))
self.set_field("emojis", lambda g, *_: g.emojis) self.set_field(
"emojis", lambda g, *_: List(discord.Emoji, g.emojis).order_by_descending(lambda x: x.created_at)
)
def _resolve_channels(self, g: Guild, *_, filter=None): def _resolve_channels(self, g: Guild, *_, filter=None):
users = List(any).extend(g.channels) channels = List(any).extend(g.channels)
if filter is None: if filter is None:
return users return channels
if "id" in filter: if "id" in filter:
users = users.where(lambda c: c.id == int(filter["id"])) channels = channels.where(lambda c: c.id == int(filter["id"]))
if "name" in filter: if "name" in filter:
users = users.where(lambda c: c.id == filter["name"]) channels = channels.where(lambda c: c.id == filter["name"])
if "type" in filter: if "type" in filter:
users = users.where(lambda c: type(c).__name__ == filter["type"]) channels = channels.where(lambda c: type(c).__name__ == filter["type"])
return users return channels.order_by(lambda x: x.position)