Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Alias] Aliased commands retain newlines #4656

Merged
merged 6 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading