Skip to content

Commit

Permalink
[Alias] Aliased commands retain newlines (#4656)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Oliveira <[email protected]>
  • Loading branch information
goettner and Flame442 authored Jan 9, 2024
1 parent a393a10 commit 569840e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
12 changes: 11 additions & 1 deletion redbot/cogs/alias/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ async def get_prefix(self, message: discord.Message) -> str:
raise ValueError("No prefix found.")

async def call_alias(self, message: discord.Message, prefix: str, alias: AliasEntry):
new_message = self.translate_alias_message(message, prefix, alias)
await self.bot.process_commands(new_message)

def translate_alias_message(self, message: discord.Message, prefix: str, alias: AliasEntry):
"""
Translates a discord message using an alias
for a command to a discord message using the
alias' base command.
"""
new_message = copy(message)
try:
args = alias.get_extra_args_from_alias(message, prefix)
Expand All @@ -163,7 +172,8 @@ async def call_alias(self, message: discord.Message, prefix: str, alias: AliasEn
new_message.content = "{}{} {}".format(
prefix, command, " ".join(args[trackform.max + 1 :])
).strip()
await self.bot.process_commands(new_message)

return new_message

async def paginate_alias_list(
self, ctx: commands.Context, alias_list: List[AliasEntry]
Expand Down
3 changes: 1 addition & 2 deletions redbot/cogs/alias/alias_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def get_extra_args_from_alias(self, message: discord.Message, prefix: str) -> st
word = view.get_quoted_word()
if len(word) < view.index - prev:
word = "".join((view.buffer[prev], word, view.buffer[view.index - 1]))
extra.append(word)
view.skip_ws()
extra.append(word.strip(" "))
return extra

def to_json(self) -> dict:
Expand Down
14 changes: 14 additions & 0 deletions redbot/pytest/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"empty_role",
"empty_user",
"member_factory",
"newline_message",
"user_factory",
"prefix",
"ctx",
]

Expand Down Expand Up @@ -142,6 +144,18 @@ def empty_message():
return mock_msg("No content.")


@pytest.fixture(scope="module")
def newline_message():
mock_msg = type("", (), {})()
mock_msg.content = "!test a\nb\nc"
return mock_msg


@pytest.fixture(scope="module")
def prefix():
return "!"


@pytest.fixture()
def ctx(empty_member, empty_channel, red):
mock_ctx = namedtuple("Context", "author guild channel message bot")
Expand Down
14 changes: 14 additions & 0 deletions tests/cogs/test_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ async def test_add_guild_alias(alias, ctx):
assert alias_obj.name == "test"


async def test_translate_alias_message(alias, ctx, newline_message, prefix):
await create_test_guild_alias(alias, ctx)
alias_obj = await alias._aliases.get_alias(ctx.guild, "test")

translated_message = alias.translate_alias_message(newline_message, prefix, alias_obj)

original_content = newline_message.content.split(" ", 1)[1]
original_content = original_content.replace(" ", "")
new_content = translated_message.content.split(" ", 1)[1]
new_content = new_content.replace(" ", "")

assert new_content == original_content


async def test_delete_guild_alias(alias, ctx):
await create_test_guild_alias(alias, ctx)
alias_obj = await alias._aliases.get_alias(ctx.guild, "test")
Expand Down

0 comments on commit 569840e

Please sign in to comment.