add tests for new command
This commit is contained in:
parent
4e7222ea7f
commit
9a225c7ad4
2 changed files with 68 additions and 12 deletions
|
|
@ -1962,7 +1962,7 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
|
||||||
|
|
||||||
discord_bot = bots.DiscordBot.objects.filter_family()
|
discord_bot = bots.DiscordBot.objects.filter_family()
|
||||||
if not discord_bot:
|
if not discord_bot:
|
||||||
if "name" in self.switches:
|
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(self.lhs, None, None, typeclass=bot_class)
|
||||||
|
|
@ -1975,11 +1975,14 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
|
||||||
discord_bot = discord_bot[0]
|
discord_bot = discord_bot[0]
|
||||||
|
|
||||||
if "name" in self.switches:
|
if "name" in self.switches:
|
||||||
new_name = self.args.strip()
|
if self.args:
|
||||||
if bots.DiscordBot.validate_username(new_name):
|
new_name = self.args.strip()
|
||||||
discord_bot.name = new_name
|
if bots.DiscordBot.validate_username(new_name):
|
||||||
self.msg(f"The Discord relay account is now named {new_name} in-game.")
|
discord_bot.name = new_name
|
||||||
return
|
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
|
||||||
|
|
@ -2008,7 +2011,7 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
|
||||||
# load in the pretty names for the discord channels from cache
|
# load in the pretty names for the discord channels from cache
|
||||||
dc_chan_names = discord_bot.attributes.get("discord_channels", {})
|
dc_chan_names = discord_bot.attributes.get("discord_channels", {})
|
||||||
for i, (evchan, dcchan) in enumerate(channel_list):
|
for i, (evchan, dcchan) in enumerate(channel_list):
|
||||||
dc_info = dc_chan_names.get(dcchan, {"name": "unknown", "guild": "unknown"})
|
dc_info = dc_chan_names.get(dcchan, {"name": dcchan, "guild": "unknown"})
|
||||||
table.add_row(
|
table.add_row(
|
||||||
i, evchan, f"#{dc_info.get('name','?')}@{dc_info.get('guild','?')}"
|
i, evchan, f"#{dc_info.get('name','?')}@{dc_info.get('guild','?')}"
|
||||||
)
|
)
|
||||||
|
|
@ -2055,10 +2058,8 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
|
||||||
results = False
|
results = False
|
||||||
for i, (evchan, dcchan) in enumerate(channel_list):
|
for i, (evchan, dcchan) in enumerate(channel_list):
|
||||||
if evchan.lower() == ev_channel.lower():
|
if evchan.lower() == ev_channel.lower():
|
||||||
dc_info = dc_chan_names.get(dcchan, {"name": "unknown", "guild": "unknown"})
|
dc_info = dc_chan_names.get(dcchan, {"name": dcchan, "guild": "unknown"})
|
||||||
table.add_row(
|
table.add_row(i, evchan, f"#{dc_info['name']}@{dc_info['guild']}")
|
||||||
i, evchan, f"#{dc_info.get('name','?')}@{dc_info.get('guild','?')}"
|
|
||||||
)
|
|
||||||
results = True
|
results = True
|
||||||
if results:
|
if results:
|
||||||
self.msg(table)
|
self.msg(table)
|
||||||
|
|
@ -2071,7 +2072,7 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
|
||||||
# check if link already exists
|
# check if link already exists
|
||||||
if channel_list := discord_bot.db.channels:
|
if channel_list := discord_bot.db.channels:
|
||||||
if (ev_channel, dc_channel) in channel_list:
|
if (ev_channel, dc_channel) in channel_list:
|
||||||
self.msg(f"Those channels are already linked.")
|
self.msg("Those channels are already linked.")
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
discord_bot.db.channels = []
|
discord_bot.db.channels = []
|
||||||
|
|
@ -2082,6 +2083,7 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
|
||||||
return
|
return
|
||||||
channel_obj = channel_obj[0]
|
channel_obj = channel_obj[0]
|
||||||
discord_bot.db.channels.append((channel_obj.name, dc_channel))
|
discord_bot.db.channels.append((channel_obj.name, dc_channel))
|
||||||
|
channel_obj.connect(discord_bot)
|
||||||
if dc_chans := discord_bot.db.discord_channels:
|
if dc_chans := discord_bot.db.discord_channels:
|
||||||
dc_channel_name = dc_chans.get(dc_channel, {}).get("name", dc_channel)
|
dc_channel_name = dc_chans.get(dc_channel, {}).get("name", dc_channel)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -2002,6 +2002,60 @@ class TestComms(BaseEvenniaCommandTest):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(DISCORD_BOT_TOKEN="notarealtoken", DISCORD_ENABLED=True)
|
||||||
|
class TestDiscord(BaseEvenniaCommandTest):
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
self.channel = create.create_channel(key="testchannel", desc="A test channel")
|
||||||
|
self.cmddiscord = cmd_comms.CmdDiscord2Chan
|
||||||
|
self.cmddiscord.account_caller = False
|
||||||
|
# create bot manually so it doesn't get started
|
||||||
|
self.discordbot = create.create_account(
|
||||||
|
"DiscordTest", None, None, typeclass="evennia.accounts.bots.DiscordBot"
|
||||||
|
)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
if self.channel.pk:
|
||||||
|
self.channel.delete()
|
||||||
|
|
||||||
|
@parameterized.expand(
|
||||||
|
[
|
||||||
|
("", "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."),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_discord__switches(self, cmd_args, expected):
|
||||||
|
self.call(self.cmddiscord(), cmd_args, expected)
|
||||||
|
|
||||||
|
def test_discord__linking(self):
|
||||||
|
self.call(
|
||||||
|
self.cmddiscord(), "nosuchchannel = 5555555", "There is no channel 'nosuchchannel'"
|
||||||
|
)
|
||||||
|
self.call(
|
||||||
|
self.cmddiscord(),
|
||||||
|
"testchannel = 5555555",
|
||||||
|
"Discord connection created: testchannel <-> #5555555",
|
||||||
|
)
|
||||||
|
self.assertTrue(self.discordbot in self.channel.subscriptions.all())
|
||||||
|
self.assertTrue(("testchannel", "5555555") in self.discordbot.db.channels)
|
||||||
|
self.call(self.cmddiscord(), "testchannel = 5555555", "Those channels are already linked.")
|
||||||
|
|
||||||
|
def test_discord__list(self):
|
||||||
|
self.discordbot.db.channels = [("testchannel", "5555555")]
|
||||||
|
cmdobj = self.cmddiscord()
|
||||||
|
cmdobj.msg = lambda text, **kwargs: setattr(self, "out", str(text))
|
||||||
|
self.call(cmdobj, "", None)
|
||||||
|
self.assertIn("testchannel", self.out)
|
||||||
|
self.assertIn("5555555", self.out)
|
||||||
|
self.call(cmdobj, "testchannel", None)
|
||||||
|
self.assertIn("testchannel", self.out)
|
||||||
|
self.assertIn("5555555", self.out)
|
||||||
|
|
||||||
|
|
||||||
class TestBatchProcess(BaseEvenniaCommandTest):
|
class TestBatchProcess(BaseEvenniaCommandTest):
|
||||||
"""
|
"""
|
||||||
Test the batch processor.
|
Test the batch processor.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue