addressing requested changes

This commit is contained in:
InspectorCaracal 2022-11-29 16:33:12 -07:00
parent 583d585d10
commit e803be2773
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
command available: `@discord2chan`. Enter `help discord2chan` for an explanation
command available: `discord2chan`. Enter `help discord2chan` for an explanation
of its options.
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,
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
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
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.
### 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:
@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
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.
"""
if not len(self.account.sessions.all()):
if not self.account.sessions.all():
self.account.start()
def at_repeat(self):
@ -626,21 +626,22 @@ class DiscordBot(Bot):
"""
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
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
Args:
message (str): Incoming text from channel.
channel (Channel): The channel the message is being received from
message (str) - Incoming text from channel.
channel (Channel) - The channel the message is being received from
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"):
@ -661,6 +662,9 @@ class DiscordBot(Bot):
message (str) - Incoming text from Discord.
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
@ -678,7 +682,7 @@ class DiscordBot(Bot):
sender (tuple) - The Discord info for the sender in the form (id, nickname)
from_channel (str) - The Discord channel 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 = ""
@ -701,7 +705,7 @@ class DiscordBot(Bot):
def execute_cmd(
self,
txt=None,
content=None,
session=None,
type=None,
sender=None,
@ -710,6 +714,18 @@ class DiscordBot(Bot):
"""
Take incoming data from protocol and send it to connected channel. This is
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
if type == "channel":
@ -726,12 +742,12 @@ class DiscordBot(Bot):
if not channel:
continue
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
elif type == "direct":
# pass on to the DM hook
self.direct_msg(txt, sender, **kwargs)
self.direct_msg(content, sender, **kwargs)
# guild info update
elif type == "guild":

View file

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

View file

@ -2022,8 +2022,6 @@ class TestDiscord(BaseEvenniaCommandTest):
[
("", "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."),
("/channel", "Relayed messages will include the originating channel."),
]