GraphQL Part1 (gql endpoint only) #162 #192
@ -3,11 +3,13 @@ from typing import Optional
|
||||
|
||||
from cpl_core.database import TableABC
|
||||
|
||||
from bot_data.model.server import Server
|
||||
|
||||
|
||||
class AutoRole(TableABC):
|
||||
def __init__(
|
||||
self,
|
||||
server_id: int,
|
||||
server: Optional[Server],
|
||||
dc_channel_id: int,
|
||||
dc_message_id: int,
|
||||
created_at: datetime = None,
|
||||
@ -15,7 +17,7 @@ class AutoRole(TableABC):
|
||||
id=0,
|
||||
):
|
||||
self._auto_role_id = id
|
||||
self._server_id = server_id
|
||||
self._server = server
|
||||
self._discord_channel_id = dc_channel_id
|
||||
self._discord_message_id = dc_message_id
|
||||
|
||||
@ -28,8 +30,8 @@ class AutoRole(TableABC):
|
||||
return self._auto_role_id
|
||||
|
||||
@property
|
||||
def server_id(self) -> int:
|
||||
return self._server_id
|
||||
def server(self) -> Server:
|
||||
return self._server
|
||||
|
||||
@property
|
||||
def discord_channel_id(self) -> int:
|
||||
@ -81,7 +83,7 @@ class AutoRole(TableABC):
|
||||
INSERT INTO `AutoRoles` (
|
||||
`ServerId`, `DiscordChannelId`, `DiscordMessageId`, `CreatedAt`, `LastModifiedAt`
|
||||
) VALUES (
|
||||
{self._server_id},
|
||||
{self._server.server_id},
|
||||
{self._discord_channel_id},
|
||||
{self._discord_message_id},
|
||||
'{self._created_at}',
|
||||
@ -95,7 +97,7 @@ class AutoRole(TableABC):
|
||||
return str(
|
||||
f"""
|
||||
UPDATE `AutoRoles`
|
||||
SET `ServerId` = {self._server_id},
|
||||
SET `ServerId` = {self._server.server_id},
|
||||
`DiscordChannelId` = {self._discord_channel_id},
|
||||
`DiscordMessageId` = {self._discord_message_id},
|
||||
`LastModifiedAt` = '{self._modified_at}'
|
||||
|
@ -5,14 +5,16 @@ from cpl_query.extension import List
|
||||
|
||||
from bot_core.logging.database_logger import DatabaseLogger
|
||||
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
|
||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
from bot_data.model.auto_role import AutoRole
|
||||
from bot_data.model.auto_role_rule import AutoRoleRule
|
||||
|
||||
|
||||
class AutoRoleRepositoryService(AutoRoleRepositoryABC):
|
||||
def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC):
|
||||
def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC, servers: ServerRepositoryABC):
|
||||
self._logger = logger
|
||||
self._context = db_context
|
||||
self._servers = servers
|
||||
|
||||
AutoRoleRepositoryABC.__init__(self)
|
||||
|
||||
@ -21,14 +23,20 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC):
|
||||
self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_all_string()}")
|
||||
results = self._context.select(AutoRole.get_select_all_string())
|
||||
for result in results:
|
||||
auto_roles.append(AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0]))
|
||||
auto_roles.append(
|
||||
AutoRole(
|
||||
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
|
||||
)
|
||||
)
|
||||
|
||||
return auto_roles
|
||||
|
||||
def get_auto_role_by_id(self, id: int) -> AutoRole:
|
||||
self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_by_id_string(id)}")
|
||||
result = self._context.select(AutoRole.get_select_by_id_string(id))[0]
|
||||
return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])
|
||||
return AutoRole(
|
||||
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
|
||||
)
|
||||
|
||||
def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]:
|
||||
self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_by_id_string(id)}")
|
||||
@ -38,14 +46,20 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC):
|
||||
|
||||
result = result[0]
|
||||
|
||||
return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])
|
||||
return AutoRole(
|
||||
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
|
||||
)
|
||||
|
||||
def get_auto_roles_by_server_id(self, id: int) -> List[AutoRole]:
|
||||
self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_by_server_id_string(id)}")
|
||||
auto_roles = List(AutoRole)
|
||||
results = self._context.select(AutoRole.get_select_by_server_id_string(id))
|
||||
for result in results:
|
||||
auto_roles.append(AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0]))
|
||||
auto_roles.append(
|
||||
AutoRole(
|
||||
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
|
||||
)
|
||||
)
|
||||
|
||||
return auto_roles
|
||||
|
||||
@ -55,7 +69,9 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC):
|
||||
f"Send SQL command: {AutoRole.get_select_by_message_id_string(id)}",
|
||||
)
|
||||
result = self._context.select(AutoRole.get_select_by_message_id_string(id))[0]
|
||||
return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])
|
||||
return AutoRole(
|
||||
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
|
||||
)
|
||||
|
||||
def find_auto_role_by_message_id(self, id: int) -> Optional[AutoRole]:
|
||||
self._logger.trace(
|
||||
@ -68,7 +84,9 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC):
|
||||
|
||||
result = result[0]
|
||||
|
||||
return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])
|
||||
return AutoRole(
|
||||
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
|
||||
)
|
||||
|
||||
def add_auto_role(self, auto_role: AutoRole):
|
||||
self._logger.trace(__name__, f"Send SQL command: {auto_role.insert_string}")
|
||||
|
@ -14,7 +14,7 @@ class QueryABC(ObjectType):
|
||||
def __init__(self, name: str):
|
||||
ObjectType.__init__(self, name)
|
||||
|
||||
def _add_collection(self, name: str, get_collection: Callable, filter_type: type = None):
|
||||
def add_collection(self, name: str, get_collection: Callable, filter_type: type = None):
|
||||
def wrapper(*args, **kwargs):
|
||||
if filter_type is not None and "filter" in kwargs:
|
||||
kwargs["filter"] = FilterABC.get_collection_filter(filter_type, kwargs["filter"])
|
||||
|
@ -1,3 +1,4 @@
|
||||
from cpl_discord.service import DiscordBotServiceABC
|
||||
from cpl_query.extension import List
|
||||
|
||||
from bot_data.model.auto_role import AutoRole
|
||||
@ -5,22 +6,39 @@ from bot_graphql.abc.filter_abc import FilterABC
|
||||
|
||||
|
||||
class AutoRoleFilter(FilterABC):
|
||||
def __init__(self):
|
||||
def __init__(
|
||||
self,
|
||||
bot: DiscordBotServiceABC,
|
||||
):
|
||||
FilterABC.__init__(self)
|
||||
|
||||
self._bot = bot
|
||||
|
||||
self._id = None
|
||||
self._channel_id = None
|
||||
self._channel_name = None
|
||||
self._message_id = None
|
||||
self._server = None
|
||||
|
||||
def from_dict(self, values: dict):
|
||||
if "id" in values:
|
||||
self._id = values["id"]
|
||||
self._id = int(values["id"])
|
||||
|
||||
if "channel_id" in values:
|
||||
self._channel_id = values["channel_id"]
|
||||
self._channel_id = int(values["channel_id"])
|
||||
|
||||
if "channel_name" in values:
|
||||
self._channel_name = values["channel_name"]
|
||||
|
||||
if "message_id" in values:
|
||||
self._message_id = values["message_id"]
|
||||
self._message_id = int(values["message_id"])
|
||||
|
||||
if "server" in values:
|
||||
from bot_graphql.filter.server_filter import ServerFilter
|
||||
|
||||
server = ServerFilter()
|
||||
server.from_dict(values["server"])
|
||||
self._server = server
|
||||
|
||||
def filter(self, query: List[AutoRole]) -> List[AutoRole]:
|
||||
if self._id is not None:
|
||||
@ -29,10 +47,17 @@ class AutoRoleFilter(FilterABC):
|
||||
if self._channel_id is not None:
|
||||
query = query.where(lambda x: x.discord_channel_id == self._channel_id)
|
||||
|
||||
if self._channel_name is not None and self._channel_id is not None:
|
||||
query = query.where(
|
||||
lambda x: self._bot.get_channel(x.discord_channel_id).name == self._channel_name
|
||||
or self._channel_name in self._bot.get_channel(x.discord_channel_id).name
|
||||
)
|
||||
|
||||
if self._message_id is not None:
|
||||
query = query.where(lambda x: x.discord_message_id == self._message_id)
|
||||
|
||||
# if self._server_id is not None:
|
||||
# query = query.where(lambda x: x.server.server_id == self._server_id)
|
||||
if self._server is not None:
|
||||
servers = self._server.filter(query.select(lambda x: x.server)).select(lambda x: x.server_id)
|
||||
query = query.where(lambda x: x.server.server_id in servers)
|
||||
|
||||
return query
|
||||
|
@ -15,16 +15,16 @@ class AutoRoleRuleFilter(FilterABC):
|
||||
|
||||
def from_dict(self, values: dict):
|
||||
if "id" in values:
|
||||
self._id = values["id"]
|
||||
self._id = int(values["id"])
|
||||
|
||||
if "emoji_name" in values:
|
||||
self._emoji_name = values["emoji_name"]
|
||||
|
||||
if "role_id" in values:
|
||||
self._role_id = values["role_id"]
|
||||
self._role_id = int(values["role_id"])
|
||||
|
||||
if "auto_role_id" in values:
|
||||
self._auto_role_id = values["auto_role_id"]
|
||||
self._auto_role_id = int(values["auto_role_id"])
|
||||
|
||||
def filter(self, query: List[AutoRoleRule]) -> List[AutoRoleRule]:
|
||||
if self._id is not None:
|
||||
|
@ -14,7 +14,7 @@ class LevelFilter(FilterABC):
|
||||
|
||||
def from_dict(self, values: dict):
|
||||
if "id" in values:
|
||||
self._id = values["id"]
|
||||
self._id = int(values["id"])
|
||||
|
||||
if "name" in values:
|
||||
self._name = values["name"]
|
||||
|
@ -11,10 +11,10 @@ class Page(FilterABC):
|
||||
|
||||
def from_dict(self, values: dict):
|
||||
if "page_index" in values:
|
||||
self._page_index = values["page_index"]
|
||||
self._page_index = int(values["page_index"])
|
||||
|
||||
if "page_size" in values:
|
||||
self._page_size = values["page_size"]
|
||||
self._page_size = int(values["page_size"])
|
||||
|
||||
def filter(self, query: List, *args) -> List:
|
||||
if self._page_size is not None and self._page_index is not None:
|
||||
|
@ -20,7 +20,7 @@ class ServerFilter(FilterABC):
|
||||
self._id = int(values["id"])
|
||||
|
||||
if "discord_id" in values:
|
||||
self._discord_id = values["discord_id"]
|
||||
self._discord_id = int(values["discord_id"])
|
||||
|
||||
if "name" in values:
|
||||
self._name = values["name"]
|
||||
|
@ -35,15 +35,15 @@ class UserFilter(FilterABC):
|
||||
|
||||
def from_dict(self, values: dict):
|
||||
if "id" in values:
|
||||
self._id = values["id"]
|
||||
self._id = int(values["id"])
|
||||
if "discord_id" in values:
|
||||
self._discord_id = values["discord_id"]
|
||||
self._discord_id = int(values["discord_id"])
|
||||
if "name" in values:
|
||||
self._name = values["name"]
|
||||
if "xp" in values:
|
||||
self._xp = values["xp"]
|
||||
self._xp = int(values["xp"])
|
||||
if "ontime" in values:
|
||||
self._ontime = values["ontime"]
|
||||
self._ontime = int(values["ontime"])
|
||||
if "level" in values:
|
||||
self._level: LevelFilter = self._services.get_service(LevelFilter)
|
||||
self._level.from_dict(values["level"])
|
||||
|
@ -1,6 +1,7 @@
|
||||
type AutoRole implements TableQuery {
|
||||
id: ID
|
||||
channel_id: String
|
||||
channel_name: String
|
||||
message_id: String
|
||||
|
||||
server: Server
|
||||
@ -15,6 +16,7 @@ type AutoRole implements TableQuery {
|
||||
input AutoRoleFilter {
|
||||
id: ID
|
||||
channel_id: String
|
||||
channel_name: String
|
||||
message_id: String
|
||||
server: ServerFilter
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
from cpl_discord.service import DiscordBotServiceABC
|
||||
|
||||
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
|
||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
from bot_data.model.auto_role import AutoRole
|
||||
@ -8,19 +10,22 @@ from bot_graphql.filter.server_filter import ServerFilter
|
||||
class AutoRoleQuery(DataQueryABC):
|
||||
def __init__(
|
||||
self,
|
||||
bot: DiscordBotServiceABC,
|
||||
auto_role_rules: AutoRoleRepositoryABC,
|
||||
servers: ServerRepositoryABC,
|
||||
):
|
||||
DataQueryABC.__init__(self, "AutoRole")
|
||||
|
||||
self._bot = bot
|
||||
self._auto_role_rules = auto_role_rules
|
||||
self._servers = servers
|
||||
|
||||
self.set_field("id", self.resolve_id)
|
||||
self.set_field("channel_id", self.resolve_channel_id)
|
||||
self.set_field("channel_name", self.resolve_channel_name)
|
||||
self.set_field("message_id", self.resolve_message_id)
|
||||
self.set_field("server", self.resolve_server)
|
||||
self._add_collection(
|
||||
self.add_collection(
|
||||
"auto_role_rule", lambda x, *_: self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id)
|
||||
)
|
||||
|
||||
@ -32,12 +37,16 @@ class AutoRoleQuery(DataQueryABC):
|
||||
def resolve_channel_id(x: AutoRole, *_):
|
||||
return x.discord_channel_id
|
||||
|
||||
def resolve_channel_name(self, x: AutoRole, *_):
|
||||
channel = self._bot.get_channel(x.discord_channel_id)
|
||||
return None if channel is None else channel.name
|
||||
|
||||
@staticmethod
|
||||
def resolve_message_id(x: AutoRole, *_):
|
||||
return x.discord_message_id
|
||||
|
||||
def resolve_server(self, x: AutoRole, *_, filter: ServerFilter = None):
|
||||
if filter is not None:
|
||||
return filter.filter(self._servers.get_server_by_id(x.server_id))
|
||||
return filter.filter(self._servers.get_server_by_id(x.server.server_id))
|
||||
|
||||
return self._servers.get_server_by_id(x.server_id)
|
||||
return self._servers.get_server_by_id(x.server.server_id)
|
||||
|
@ -31,14 +31,12 @@ class ServerQuery(DataQueryABC):
|
||||
self.set_field("discord_id", self.resolve_discord_id)
|
||||
self.set_field("name", self.resolve_name)
|
||||
|
||||
self._add_collection(
|
||||
self.add_collection(
|
||||
"auto_role", lambda server, *_: self._auto_roles.get_auto_roles_by_server_id(server.server_id)
|
||||
)
|
||||
self._add_collection("client", lambda server, *_: self._clients.get_clients_by_server_id(server.server_id))
|
||||
self._add_collection(
|
||||
"user", lambda server, *_: self._users.get_users_by_server_id(server.server_id), UserFilter
|
||||
)
|
||||
self._add_collection("level", lambda server, *_: self._levels.get_levels_by_server_id(server.server_id))
|
||||
self.add_collection("client", lambda server, *_: self._clients.get_clients_by_server_id(server.server_id))
|
||||
self.add_collection("user", lambda server, *_: self._users.get_users_by_server_id(server.server_id), UserFilter)
|
||||
self.add_collection("level", lambda server, *_: self._levels.get_levels_by_server_id(server.server_id))
|
||||
|
||||
@staticmethod
|
||||
def resolve_id(server: Server, *_):
|
||||
|
@ -31,10 +31,10 @@ class UserQuery(DataQueryABC):
|
||||
self.set_field("xp", self.resolve_xp)
|
||||
self.set_field("ontime", self.resolve_ontime)
|
||||
self.set_field("level", self.resolve_level)
|
||||
self._add_collection(
|
||||
self.add_collection(
|
||||
"joined_server", lambda user, *_: self._ujs.get_user_joined_servers_by_user_id(user.user_id)
|
||||
)
|
||||
self._add_collection(
|
||||
self.add_collection(
|
||||
"joined_voice_channel", lambda user, *_: self._ujvs.get_user_joined_voice_channels_by_user_id(user.user_id)
|
||||
)
|
||||
self.set_field("server", self.resolve_server)
|
||||
|
@ -36,14 +36,14 @@ class Query(QueryABC):
|
||||
self._user_joined_voice_channels = user_joined_voice_channel
|
||||
self._users = users
|
||||
|
||||
self._add_collection("auto_role", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter)
|
||||
self._add_collection("auto_role_rule", lambda *_: self._auto_roles.get_auto_role_rules(), AutoRoleRuleFilter)
|
||||
self._add_collection("client", lambda *_: self._clients.get_clients())
|
||||
self._add_collection("known_user", lambda *_: self._known_users.get_users())
|
||||
self._add_collection("level", lambda *_: self._levels.get_levels(), LevelFilter)
|
||||
self._add_collection("server", lambda *_: self._servers.get_servers(), ServerFilter)
|
||||
self._add_collection("user_joined_server", lambda *_: self._user_joined_servers.get_user_joined_servers())
|
||||
self._add_collection(
|
||||
self.add_collection("auto_role", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter)
|
||||
self.add_collection("auto_role_rule", lambda *_: self._auto_roles.get_auto_role_rules(), AutoRoleRuleFilter)
|
||||
self.add_collection("client", lambda *_: self._clients.get_clients())
|
||||
self.add_collection("known_user", lambda *_: self._known_users.get_users())
|
||||
self.add_collection("level", lambda *_: self._levels.get_levels(), LevelFilter)
|
||||
self.add_collection("server", lambda *_: self._servers.get_servers(), ServerFilter)
|
||||
self.add_collection("user_joined_server", lambda *_: self._user_joined_servers.get_user_joined_servers())
|
||||
self.add_collection(
|
||||
"user_joined_voice_channel", lambda *_: self._user_joined_voice_channels.get_user_joined_voice_channels()
|
||||
)
|
||||
self._add_collection("user", lambda *_: self._users.get_users(), UserFilter)
|
||||
self.add_collection("user", lambda *_: self._users.get_users(), UserFilter)
|
||||
|
@ -1,9 +1,8 @@
|
||||
from typing import List as TList, Optional, Any
|
||||
from typing import List as TList
|
||||
|
||||
import discord
|
||||
from cpl_core.database.context import DatabaseContextABC
|
||||
from cpl_discord.command import DiscordCommandABC
|
||||
from cpl_discord.container import TextChannel
|
||||
from cpl_discord.service import DiscordBotServiceABC
|
||||
from cpl_query.extension import List
|
||||
from cpl_translation import TranslatePipe
|
||||
@ -138,10 +137,10 @@ class AutoRoleGroup(DiscordCommandABC):
|
||||
self._logger.trace(__name__, f"Finished command auto-role add")
|
||||
return
|
||||
|
||||
server_id = self._servers.get_server_by_discord_id(ctx.guild.id).server_id
|
||||
self._auto_roles.add_auto_role(AutoRole(server_id, int(channel.id), int(message_id)))
|
||||
server = self._servers.get_server_by_discord_id(ctx.guild.id)
|
||||
self._auto_roles.add_auto_role(AutoRole(server, int(channel.id), int(message_id)))
|
||||
self._db_context.save_changes()
|
||||
self._logger.info(__name__, f"Saved auto-role for message {message_id} at server {server_id}")
|
||||
self._logger.info(__name__, f"Saved auto-role for message {message_id} at server {server}")
|
||||
await self._message_service.send_ctx_msg(
|
||||
ctx, self._t.transform("modules.auto_role.add.success").format(message_id)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user