Merge pull request #3711 from InspectorCaracal/discord-bot-updates

Minor usability improvements for the Discord integration
This commit is contained in:
Griatch 2025-01-18 12:12:10 +01:00 committed by GitHub
commit 0a12882eae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 31 deletions

View file

@ -555,24 +555,28 @@ class DiscordBot(Bot):
""" """
factory_path = "evennia.server.portal.discord.DiscordWebsocketServerFactory" factory_path = "evennia.server.portal.discord.DiscordWebsocketServerFactory"
def _load_channels(self):
self.ndb.ev_channels = {}
if channel_links := self.db.channels:
# this attribute contains a list of evennia<->discord links in the form
# of ("evennia_channel", "discord_chan_id")
# grab Evennia channels, cache and connect
channel_set = {evchan for evchan, dcid in channel_links}
for channel_name in list(channel_set):
channel = search.search_channel(channel_name)
if not channel:
logger.log_err(f"Evennia Channel {channel_name} not found; skipping.")
continue
channel = channel[0]
self.ndb.ev_channels[channel_name] = channel
def at_init(self): def at_init(self):
""" """
Load required channels back into memory Load required channels back into memory
""" """
if channel_links := self.db.channels: self._load_channels()
# this attribute contains a list of evennia<->discord links in the form
# of ("evennia_channel", "discord_chan_id")
# grab Evennia channels, cache and connect
channel_set = {evchan for evchan, dcid in channel_links}
self.ndb.ev_channels = {}
for channel_name in list(channel_set):
channel = search.search_channel(channel_name)
if not channel:
raise RuntimeError(f"Evennia Channel {channel_name} not found.")
channel = channel[0]
self.ndb.ev_channels[channel_name] = channel
def start(self): def start(self):
""" """
@ -583,27 +587,18 @@ class DiscordBot(Bot):
self.delete() self.delete()
return return
if self.ndb.ev_channels: if not self.ndb.ev_channels:
for channel in self.ndb.ev_channels.values(): self._load_channels()
channel.connect(self)
elif channel_links := self.db.channels: for channel in self.ndb.ev_channels.values():
# this attribute contains a list of evennia<->discord links in the form if not channel.connect(self):
# of ("evennia_channel", "discord_chan_id") logger.log_warn(f"{self} could not connect to Evennia channel {channel}.")
# grab Evennia channels, cache and connect if not channel.access(self, "send"):
channel_set = {evchan for evchan, dcid in channel_links} logger.log_warn(f"{self} doesn't have permission to send messages to Evennia channel {channel}.")
self.ndb.ev_channels = {}
for channel_name in list(channel_set):
channel = search.search_channel(channel_name)
if not channel:
raise RuntimeError(f"Evennia Channel {channel_name} not found.")
channel = channel[0]
self.ndb.ev_channels[channel_name] = channel
channel.connect(self)
# connect
# these will be made available as properties on the protocol factory # these will be made available as properties on the protocol factory
configdict = {"uid": self.dbid} configdict = {"uid": self.dbid}
# finally, connect
evennia.SESSION_HANDLER.start_bot_session(self.factory_path, configdict) evennia.SESSION_HANDLER.start_bot_session(self.factory_path, configdict)
def at_pre_channel_msg(self, message, channel, senders=None, **kwargs): def at_pre_channel_msg(self, message, channel, senders=None, **kwargs):

View file

@ -2030,7 +2030,7 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
# show all connections # show all connections
if channel_list := discord_bot.db.channels: if channel_list := discord_bot.db.channels:
table = self.styled_table( table = self.styled_table(
"|wLink ID|n", "|wLink Index|n",
"|wEvennia|n", "|wEvennia|n",
"|wDiscord|n", "|wDiscord|n",
border="cells", border="cells",
@ -2076,7 +2076,7 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
# show all discord channels linked to self.lhs # show all discord channels linked to self.lhs
if channel_list := discord_bot.db.channels: if channel_list := discord_bot.db.channels:
table = self.styled_table( table = self.styled_table(
"|wLink ID|n", "|wLink Index|n",
"|wEvennia|n", "|wEvennia|n",
"|wDiscord|n", "|wDiscord|n",
border="cells", border="cells",