addressing requested changes

This commit is contained in:
InspectorCaracal 2022-11-29 16:33:12 -07:00
parent 87867509cc
commit e8d0754d7d
4 changed files with 42 additions and 38 deletions

View file

@ -27,12 +27,12 @@ DISCORD_ENABLED = True
``` ```
Start/reload Evennia and log in as a privileged user. You should now have a new Start/reload Evennia and log in as a privileged user. You should now have a new
command available: `@discord2chan`. Enter `help discord2chan` for an explanation command available: `discord2chan`. Enter `help discord2chan` for an explanation
of its options. of its options.
Adding a new channel link is done with the following command: Adding a new channel link is done with the following command:
@discord2chan <evennia_channel> = <discord_channel_id> discord2chan <evennia_channel> = <discord_channel_id>
The `evennia_channel` argument must be the name of an existing Evennia channel, The `evennia_channel` argument must be the name of an existing Evennia channel,
and `discord_channel_id` is the full numeric ID of the Discord channel. and `discord_channel_id` is the full numeric ID of the Discord channel.
@ -122,12 +122,12 @@ DISCORD_ENABLED = True
Start or reload your game to apply the changed settings, then log in as an account Start or reload your game to apply the changed settings, then log in as an account
with at least `Developer` permissions and initialize the bot account on Evennia: with at least `Developer` permissions and initialize the bot account on Evennia:
@discord2chan/name <your bot name> discord2chan/name <your bot name>
The name you assign it can be anything; it will show up in the `who` list for your The name you assign it can be anything; it will show up in the `who` list for your
game and your game's channels, but is otherwise unused. game and your game's channels, but is otherwise unused.
Lastly, confirm that it's fully enabled by entering `@discord2chan` on its own. Lastly, confirm that it's fully enabled by entering `discord2chan` on its own.
You should receive a message that there are no active connections to Discord. You should receive a message that there are no active connections to Discord.
### Connecting an Evennia channel to a Discord channel ### Connecting an Evennia channel to a Discord channel
@ -140,7 +140,7 @@ then your channel ID is `12345678901234567890`
Link the two channels with the following command: Link the two channels with the following command:
@discord2chan <evennia channel> = <discord channel id> discord2chan <evennia channel> = <discord channel id>
The two channels should now relay to each other. Confirm this works by posting a The two channels should now relay to each other. Confirm this works by posting a
message on the evennia channel, and another on the Discord channel - they should message on the evennia channel, and another on the Discord channel - they should

View file

@ -49,7 +49,7 @@ class BotStarter(DefaultScript):
Kick bot into gear. Kick bot into gear.
""" """
if not len(self.account.sessions.all()): if not self.account.sessions.all():
self.account.start() self.account.start()
def at_repeat(self): def at_repeat(self):
@ -626,21 +626,22 @@ class DiscordBot(Bot):
""" """
Called by the Channel just before passing a message into `channel_msg`. Called by the Channel just before passing a message into `channel_msg`.
We overload this to force off the channel tag prefix. We overload this to set the channel tag prefix.
""" """
kwargs["no_prefix"] = not self.db.tag_channel kwargs["no_prefix"] = not self.db.tag_channel
return super().at_pre_channel_msg(message, channel, senders=senders, **kwargs) return super().at_pre_channel_msg(message, channel, senders=senders, **kwargs)
def channel_msg(self, message, channel, senders=None, **kwargs): def channel_msg(self, message, channel, senders=None, relayed=False, **kwargs):
""" """
Passes channel messages received on to discord Passes channel messages received on to discord
Args: Args:
message (str): Incoming text from channel. message (str) - Incoming text from channel.
channel (Channel): The channel the message is being received from channel (Channel) - The channel the message is being received from
Keyword Args: Keyword Args:
senders (list or None): Object(s) sending the message senders (list or None) - Object(s) sending the message
relayed (bool) - A flag identifying whether the message was relayed by the bot.
""" """
if kwargs.get("relayed"): if kwargs.get("relayed"):
@ -661,6 +662,9 @@ class DiscordBot(Bot):
message (str) - Incoming text from Discord. message (str) - Incoming text from Discord.
sender (tuple) - The Discord info for the sender in the form (id, nickname) sender (tuple) - The Discord info for the sender in the form (id, nickname)
Keyword args:
kwargs (optional) - Unused by default, but can carry additional data from the protocol.
""" """
pass pass
@ -678,7 +682,7 @@ class DiscordBot(Bot):
sender (tuple) - The Discord info for the sender in the form (id, nickname) sender (tuple) - The Discord info for the sender in the form (id, nickname)
from_channel (str) - The Discord channel name from_channel (str) - The Discord channel name
from_server (str) - The Discord server name from_server (str) - The Discord server name
kwargs - Any additional keywords. Unused by default, but available for adding additional flags or parameters.
""" """
tag_str = "" tag_str = ""
@ -701,7 +705,7 @@ class DiscordBot(Bot):
def execute_cmd( def execute_cmd(
self, self,
txt=None, content=None,
session=None, session=None,
type=None, type=None,
sender=None, sender=None,
@ -710,6 +714,18 @@ class DiscordBot(Bot):
""" """
Take incoming data from protocol and send it to connected channel. This is Take incoming data from protocol and send it to connected channel. This is
triggered by the bot_data_in Inputfunc. triggered by the bot_data_in Inputfunc.
Keyword args:
content (str) - The content of the message from Discord.
session (Session) - The protocol session this command came from.
type (str, optional) - Indicates the type of activity from Discord, if
the protocol pre-processed it.
sender (tuple) - Identifies the author of the Discord activity in a tuple of two
strings, in the form of (id, nickname)
kwargs - Any additional data specific to a particular type of actions. The data for
any Discord actions not pre-processed by the protocol will also be passed via kwargs.
""" """
# normal channel message # normal channel message
if type == "channel": if type == "channel":
@ -726,12 +742,12 @@ class DiscordBot(Bot):
if not channel: if not channel:
continue continue
channel = channel[0] channel = channel[0]
self.relay_to_channel(txt, channel, sender, channel_name, guild) self.relay_to_channel(content, channel, sender, channel_name, guild)
# direct message # direct message
elif type == "direct": elif type == "direct":
# pass on to the DM hook # pass on to the DM hook
self.direct_msg(txt, sender, **kwargs) self.direct_msg(content, sender, **kwargs)
# guild info update # guild info update
elif type == "guild": elif type == "guild":

View file

@ -1921,7 +1921,6 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
discord2chan/name <bot_name> discord2chan/name <bot_name>
Switches: Switches:
/name - Assign a name for the Discord bot to use on Evennia channels
/list - (or no switch) show existing Evennia <-> Discord links /list - (or no switch) show existing Evennia <-> Discord links
/remove - remove an existing link by link ID /remove - remove an existing link by link ID
/delete - alias to remove /delete - alias to remove
@ -1940,12 +1939,10 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
key = "discord2chan" key = "discord2chan"
aliases = ("discord",) aliases = ("discord",)
switch_options = ( switch_options = (
"available",
"channel", "channel",
"delete", "delete",
"guild", "guild",
"list", "list",
"name",
"remove", "remove",
) )
locks = "cmd:serversetting(DISCORD_ENABLED) and pperm(Developer)" locks = "cmd:serversetting(DISCORD_ENABLED) and pperm(Developer)"
@ -1960,29 +1957,22 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
) )
return return
discord_bot = bots.DiscordBot.objects.filter_family() discord_bot = [
bot for bot in AccountDB.objects.filter(db_is_bot=True, username="DiscordBot")
]
if not discord_bot: if not discord_bot:
if "name" in self.switches and self.args:
# create a new discord bot # create a new discord bot
bot_class = class_from_module(settings.DISCORD_BOT_CLASS, fallback=bots.DiscordBot) bot_class = class_from_module(settings.DISCORD_BOT_CLASS, fallback=bots.DiscordBot)
discord_bot = create.create_account(self.lhs, None, None, typeclass=bot_class) discord_bot = create.create_account("DiscordBot", None, None, typeclass=bot_class)
discord_bot.start() discord_bot.start()
else:
self.msg("Please set up your Discord bot first: discord2chan/name <bot_name>")
return
else: else:
discord_bot = discord_bot[0] discord_bot = discord_bot[0]
if "name" in self.switches: if not discord_bot.is_typeclass(settings.DISCORD_BOT_CLASS, exact=True):
if self.args: self.msg(
new_name = self.args.strip() f"WARNING: The Discord bot's typeclass is '{discord_bot.typeclass_path}'. This does not match {settings.DISCORD_BOT_CLASS} in settings!"
if bots.DiscordBot.validate_username(new_name): )
discord_bot.name = new_name
self.msg(f"The Discord relay bot is now named {new_name} in-game.")
else:
self.msg("Please enter a name for your Discord relay bot.")
return
if "guild" in self.switches: if "guild" in self.switches:
discord_bot.db.tag_guild = not discord_bot.db.tag_guild discord_bot.db.tag_guild = not discord_bot.db.tag_guild

View file

@ -2022,8 +2022,6 @@ class TestDiscord(BaseEvenniaCommandTest):
[ [
("", "No Discord connections found."), ("", "No Discord connections found."),
("/list", "No Discord connections found."), ("/list", "No Discord connections found."),
("/name", "Please enter a name for your Discord relay bot."),
("/name DiscordBot", "The Discord relay bot is now named DiscordBot in-game."),
("/guild", "Messages to Evennia will include the Discord server."), ("/guild", "Messages to Evennia will include the Discord server."),
("/channel", "Relayed messages will include the originating channel."), ("/channel", "Relayed messages will include the originating channel."),
] ]