Files
igncore/modules/onlinebot/alliance/alliance_relay_controller.py
T
Minidodo 9f1da9a00d Fixed warnings caused by non-existing messagehub channels.
Changed the setting registration, removed the warnings.
Loot roll messages are more obvious now.
Superadmins are meant to stay mostily hidden, but are being exposed in !system again.
2021-08-29 17:54:18 +02:00

111 lines
5.4 KiB
Python

from core.aochat import server_packets, client_packets
from core.conn import Conn
from core.decorators import instance
from core.logger import Logger
from core.lookup.character_service import CharacterService
from core.setting_service import SettingService
from core.setting_types import TextSettingType, BooleanSettingType, ColorSettingType
from core.text import Text
from core.igncore import Tyrbot
@instance("AllianceRelayController")
class AllianceRelayController:
relay_channel_id = None
MESSAGE_SOURCE = "alliance"
def __init__(self):
self.logger = Logger(__name__)
# noinspection DuplicatedCode
def inject(self, registry):
self.bot: Tyrbot = registry.get_instance("bot")
self.setting_service: SettingService = registry.get_instance("setting_service")
self.character_service: CharacterService = registry.get_instance("character_service")
self.message_hub_service = registry.get_instance("message_hub_service")
self.public_channel_service = registry.get_instance("public_channel_service")
self.text: Text = registry.get_instance("text")
def pre_start(self):
self.message_hub_service.register_message_source(self.MESSAGE_SOURCE)
def start(self):
self.setting_service.register(self.module_name, "arelaybot", "",
TextSettingType(allow_empty=True), "Bot for alliance relay")
self.setting_service.register(self.module_name, "arelay_enabled", False,
BooleanSettingType(), "Enable the alliance relay")
self.setting_service.register(self.module_name, "arelay_color", "#C3C3C3",
ColorSettingType(),
"Color of messages from relay")
self.message_hub_service.register_message_destination(self.MESSAGE_SOURCE, self.handle_relay_hub_message, [],
[self.MESSAGE_SOURCE])
self.bot.register_packet_handler(server_packets.PrivateChannelInvited.id, self.handle_private_channel_invite,
100)
self.bot.register_packet_handler(server_packets.PrivateChannelMessage.id, self.handle_private_channel_message)
# noinspection DuplicatedCode
def handle_private_channel_invite(self, conn: Conn, packet: server_packets.PrivateChannelInvited):
if conn.id != "main":
return
if not self.setting_service.get("arelay_enabled").get_value():
return
channel_name = self.character_service.get_char_name(packet.private_channel_id)
if self.setting_service.get_value("arelaybot").lower() == channel_name.lower():
self.bot.send_packet(client_packets.PrivateChannelJoin(packet.private_channel_id))
self.logger.info("Joined private channel {channel}".format(channel=channel_name))
self.relay_channel_id = packet.private_channel_id
# noinspection DuplicatedCode
def handle_private_channel_message(self, conn: Conn, packet: server_packets.PrivateChannelMessage):
if conn.id != "main":
return
if not self.setting_service.get("arelay_enabled").get_value():
return
# ignore packets from the bot's own private channel and from the bot itself
if packet.private_channel_id == self.bot.get_char_id() or packet.char_id == self.bot.get_char_id():
return
message = packet.message.lstrip()
if message[:6] != "!agcr ":
return
message = message[6:]
formatted_message = self.setting_service.get("arelay_color").format_text(message)
sender = None
self.message_hub_service.send_message(self.MESSAGE_SOURCE, sender, message, formatted_message)
def handle_relay_hub_message(self, ctx):
if not self.setting_service.get("arelay_enabled").get_value():
return
plain_msg = ctx.message or ctx.formatted_message
invite = self.text.make_chatcmd("click here", "/tell <myname> discord invite",
style="style='text-decoration:none'")
blob = self.text.format_page('Info',
f"<header>::: Information :::</header><br><br>"
f"This message has been sent to you by:<br><br>"
f"<header2>Igncom</header2><br>"
f"<notice>{ctx.sender[1].name + '#' + ctx.sender[1].discriminator}</notice><br>"
f"<highlight>{ctx.sender[0]}</highlight> on Alliance Discord.<br><br>"
f"To reply, either respond in the relay or "
f"contact them directly at the provided handles.<br><br>"
f"<header2>Have you joined The Alliance Discord yet? "
f"If not <highlight>{invite}</highlight> to receive an invite.</header2>")
self.send_message_to_alliance(plain_msg + f" <yellow>[{blob}]</yellow>")
def send_message_to_alliance(self, msg):
if self.relay_channel_id:
packet = client_packets.PrivateChannelMessage(self.relay_channel_id,
"!agcr " + self.text.format_message(msg, False), "\0")
self.bot.conns["main"].send_packet(packet)