Added minecraft register commands #181

This commit is contained in:
Sven Heidemann 2023-02-12 17:22:58 +01:00
parent 2ef4d079ed
commit aa16847fbf
5 changed files with 164 additions and 0 deletions

View File

@ -200,6 +200,13 @@
"error": { "error": {
"atr_not_found": "Das Attribut {} konnte nicht gefunden werden :(" "atr_not_found": "Das Attribut {} konnte nicht gefunden werden :("
} }
},
"register": {
"success": "Spieler wurde mit dem Mitglied verlinkt :D",
"not_found": "Benutzer nicht gefunden!"
},
"unregister": {
"success": "Verlinkung wurde entfernt :D"
} }
}, },
"boot_log": { "boot_log": {

View File

@ -48,6 +48,10 @@ class User(TableABC):
def minecraft_id(self) -> Optional[str]: def minecraft_id(self) -> Optional[str]:
return self._minecraft_id return self._minecraft_id
@minecraft_id.setter
def minecraft_id(self, value: str):
self._minecraft_id = value
@property @property
def server(self) -> Optional[Server]: def server(self) -> Optional[Server]:
return self._server return self._server

View File

@ -14,6 +14,8 @@ from modules.base.command.mass_move_command import MassMoveCommand
from modules.base.command.ping_command import PingCommand from modules.base.command.ping_command import PingCommand
from modules.base.command.presence_command import PresenceCommand from modules.base.command.presence_command import PresenceCommand
from modules.base.command.purge_command import PurgeCommand from modules.base.command.purge_command import PurgeCommand
from modules.base.command.register_group import RegisterGroup
from modules.base.command.unregister_group import UnregisterGroup
from modules.base.command.user_group import UserGroup from modules.base.command.user_group import UserGroup
from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent
from modules.base.events.base_on_command_event import BaseOnCommandEvent from modules.base.events.base_on_command_event import BaseOnCommandEvent
@ -53,6 +55,8 @@ class BaseModule(ModuleABC):
self._dc.add_command(PurgeCommand) self._dc.add_command(PurgeCommand)
self._dc.add_command(UserGroup) self._dc.add_command(UserGroup)
self._dc.add_command(RegisterGroup)
self._dc.add_command(UnregisterGroup)
# events # events
self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent) self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent)
self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent) self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent)

View File

@ -0,0 +1,85 @@
import discord
import requests
from cpl_core.database.context import DatabaseContextABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
from discord.ext import commands
from discord.ext.commands import Context
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.helper.command_checks import CommandChecks
from bot_core.logging.command_logger import CommandLogger
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
class RegisterGroup(DiscordCommandABC):
def __init__(
self,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsABC,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
db: DatabaseContextABC,
t: TranslatePipe,
):
DiscordCommandABC.__init__(self)
self._logger = logger
self._message_service = message_service
self._bot = bot
self._client_utils = client_utils
self._servers = servers
self._users = users
self._db = db
self._t = t
self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}")
@commands.hybrid_group()
@commands.guild_only()
async def register(self, ctx: Context):
pass
@register.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator()
async def minecraft(self, ctx: Context, member: discord.Member, name: str):
self._logger.debug(__name__, f"Received command register minecraft {ctx}")
minecraft_id = None
try:
self._logger.debug(__name__, f"Try to get minecraft id for {name}")
response = requests.get(url=f"https://api.mojang.com/users/profiles/minecraft/{name}")
if len(response.content) == 0:
await self._message_service.send_interaction_msg(
ctx.interaction, self._t.transform("modules.base.register.not_found")
)
return
minecraft_id = response.json()["id"]
except Exception as e:
self._logger.error(__name__, f"Get minecraft id for {name} failed", e)
await self._message_service.send_interaction_msg(
ctx.interaction, self._t.transform("modules.base.register.not_found")
)
if minecraft_id is None:
return
server = self._servers.get_server_by_discord_id(ctx.guild.id)
user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id)
user.minecraft_id = minecraft_id
self._users.update_user(user)
self._db.save_changes()
await self._message_service.send_interaction_msg(
ctx.interaction, self._t.transform("modules.base.register.success")
)
self._logger.trace(__name__, f"Finished register minecraft command")

View File

@ -0,0 +1,64 @@
import discord
from cpl_core.database.context import DatabaseContextABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
from discord.ext import commands
from discord.ext.commands import Context
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.helper.command_checks import CommandChecks
from bot_core.logging.command_logger import CommandLogger
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
class UnregisterGroup(DiscordCommandABC):
def __init__(
self,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsABC,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
db: DatabaseContextABC,
t: TranslatePipe,
):
DiscordCommandABC.__init__(self)
self._logger = logger
self._message_service = message_service
self._bot = bot
self._client_utils = client_utils
self._servers = servers
self._users = users
self._db = db
self._t = t
self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}")
@commands.hybrid_group()
@commands.guild_only()
async def unregister(self, ctx: Context):
pass
@unregister.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator()
async def minecraft(self, ctx: Context, member: discord.Member):
self._logger.debug(__name__, f"Received command register minecraft {ctx}")
server = self._servers.get_server_by_discord_id(ctx.guild.id)
user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id)
user.minecraft_id = None
self._users.update_user(user)
self._db.save_changes()
await self._message_service.send_interaction_msg(
ctx.interaction, self._t.transform("modules.base.unregister.success")
)
self._logger.trace(__name__, f"Finished register minecraft command")