From f27b69176b08276ba9ef4ad2031e7af889f1139f Mon Sep 17 00:00:00 2001 From: Owen Goettner Date: Tue, 1 Dec 2020 10:38:03 -0800 Subject: [PATCH 1/5] Added discord.Object support to bank.get_balance and relevant test. --- redbot/core/bank.py | 4 ++-- redbot/pytest/core.py | 26 ++++++++++++++++++++++++++ tests/cogs/test_economy.py | 8 ++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/redbot/core/bank.py b/redbot/core/bank.py index 0d8f4133061..17952d13a2c 100644 --- a/redbot/core/bank.py +++ b/redbot/core/bank.py @@ -203,7 +203,7 @@ def _decode_time(time: int) -> datetime: return datetime.utcfromtimestamp(time) -async def get_balance(member: discord.Member) -> int: +async def get_balance(member: Union[discord.Member, discord.User, discord.Object]) -> int: """Get the current balance of a member. Parameters @@ -599,7 +599,7 @@ async def get_leaderboard_position( return pos[0] -async def get_account(member: Union[discord.Member, discord.User]) -> Account: +async def get_account(member: Union[discord.Member, discord.User, discord.Object]) -> Account: """Get the appropriate account for the given user or member. A member is required if the bank is currently guild specific. diff --git a/redbot/pytest/core.py b/redbot/pytest/core.py index ee41822ccb4..98ee0d86d51 100644 --- a/redbot/pytest/core.py +++ b/redbot/pytest/core.py @@ -22,8 +22,11 @@ "empty_message", "empty_role", "empty_user", + "object_as_member_factory", "member_factory", + "newline_message", "user_factory", + "prefix", "ctx", ] @@ -136,12 +139,35 @@ def empty_user(user_factory): return user_factory.get() +@pytest.fixture() +def object_as_member_factory(guild_factory): + mock_member = namedtuple("Object", "id guild display_name") + + class ObjectAsMemberFactory: + def get(self): + return mock_member(random.randint(1, 999999999), guild_factory.get(), "Testing_Name") + + return ObjectAsMemberFactory() + + @pytest.fixture(scope="module") def empty_message(): mock_msg = namedtuple("Message", "content") return mock_msg("No content.") +@pytest.fixture(scope="module") +def newline_message(): + mock_msg = type("", (), {})() + mock_msg.content = "!test a\nb\nc\n" + 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") diff --git a/tests/cogs/test_economy.py b/tests/cogs/test_economy.py index ec7190339f3..1f0b47bc9c7 100644 --- a/tests/cogs/test_economy.py +++ b/tests/cogs/test_economy.py @@ -28,6 +28,14 @@ async def test_bank_transfer(bank, member_factory): assert bal2 + 50 == newbal2 +@pytest.mark.asyncio +async def test_bank_get_from_object(bank, object_as_member_factory): + mbr = object_as_member_factory.get() + await bank.set_balance(mbr, 250) + acc = await bank.get_account(mbr) + assert acc.balance == 250 + + @pytest.mark.asyncio async def test_bank_set(bank, member_factory): mbr = member_factory.get() From bfa84f99df0dcbd1e18b5876fb03affda94f4ecc Mon Sep 17 00:00:00 2001 From: Owen Goettner Date: Fri, 4 Dec 2020 13:37:58 -0800 Subject: [PATCH 2/5] Added changes to allow newlines to be retained when alias is used, and a test case --- redbot/cogs/alias/alias.py | 12 +++++++++++- redbot/cogs/alias/alias_entry.py | 3 ++- redbot/pytest/core.py | 12 ------------ tests/cogs/test_alias.py | 15 +++++++++++++++ tests/cogs/test_economy.py | 8 -------- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/redbot/cogs/alias/alias.py b/redbot/cogs/alias/alias.py index 1c355b82e55..74d6dc773e9 100644 --- a/redbot/cogs/alias/alias.py +++ b/redbot/cogs/alias/alias.py @@ -173,6 +173,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) @@ -186,7 +195,8 @@ async def call_alias(self, message: discord.Message, prefix: str, alias: AliasEn new_message.content = "{}{} {}".format( prefix, command, " ".join(args[trackform.max + 1 :]) ) - await self.bot.process_commands(new_message) + + return new_message async def paginate_alias_list( self, ctx: commands.Context, alias_list: List[AliasEntry] diff --git a/redbot/cogs/alias/alias_entry.py b/redbot/cogs/alias/alias_entry.py index bc6a12ad9bc..72bec60d91b 100644 --- a/redbot/cogs/alias/alias_entry.py +++ b/redbot/cogs/alias/alias_entry.py @@ -63,7 +63,8 @@ def get_extra_args_from_alias(self, message: discord.Message, prefix: str) -> st if len(word) < view.index - prev: word = "".join((view.buffer[prev], word, view.buffer[view.index - 1])) extra.append(word) - view.skip_ws() + extra[0] = extra[0].strip(" ") + extra[len(extra) - 1] = extra[len(extra) - 1].strip(" ") return extra def to_json(self) -> dict: diff --git a/redbot/pytest/core.py b/redbot/pytest/core.py index 98ee0d86d51..c639ac5e3db 100644 --- a/redbot/pytest/core.py +++ b/redbot/pytest/core.py @@ -22,7 +22,6 @@ "empty_message", "empty_role", "empty_user", - "object_as_member_factory", "member_factory", "newline_message", "user_factory", @@ -139,17 +138,6 @@ def empty_user(user_factory): return user_factory.get() -@pytest.fixture() -def object_as_member_factory(guild_factory): - mock_member = namedtuple("Object", "id guild display_name") - - class ObjectAsMemberFactory: - def get(self): - return mock_member(random.randint(1, 999999999), guild_factory.get(), "Testing_Name") - - return ObjectAsMemberFactory() - - @pytest.fixture(scope="module") def empty_message(): mock_msg = namedtuple("Message", "content") diff --git a/tests/cogs/test_alias.py b/tests/cogs/test_alias.py index 27fdc98333d..ddbe4eaabe3 100644 --- a/tests/cogs/test_alias.py +++ b/tests/cogs/test_alias.py @@ -33,6 +33,21 @@ async def test_add_guild_alias(alias, ctx): assert alias_obj.name == "test" +@pytest.mark.asyncio +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 + + @pytest.mark.asyncio async def test_delete_guild_alias(alias, ctx): await create_test_guild_alias(alias, ctx) diff --git a/tests/cogs/test_economy.py b/tests/cogs/test_economy.py index 1f0b47bc9c7..ec7190339f3 100644 --- a/tests/cogs/test_economy.py +++ b/tests/cogs/test_economy.py @@ -28,14 +28,6 @@ async def test_bank_transfer(bank, member_factory): assert bal2 + 50 == newbal2 -@pytest.mark.asyncio -async def test_bank_get_from_object(bank, object_as_member_factory): - mbr = object_as_member_factory.get() - await bank.set_balance(mbr, 250) - acc = await bank.get_account(mbr) - assert acc.balance == 250 - - @pytest.mark.asyncio async def test_bank_set(bank, member_factory): mbr = member_factory.get() From 3038b47e1e41538098adef3d403448db9e687e48 Mon Sep 17 00:00:00 2001 From: Owen Goettner Date: Fri, 4 Dec 2020 13:40:03 -0800 Subject: [PATCH 3/5] Corrected accidental edit --- redbot/core/bank.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redbot/core/bank.py b/redbot/core/bank.py index 17952d13a2c..0d8f4133061 100644 --- a/redbot/core/bank.py +++ b/redbot/core/bank.py @@ -203,7 +203,7 @@ def _decode_time(time: int) -> datetime: return datetime.utcfromtimestamp(time) -async def get_balance(member: Union[discord.Member, discord.User, discord.Object]) -> int: +async def get_balance(member: discord.Member) -> int: """Get the current balance of a member. Parameters @@ -599,7 +599,7 @@ async def get_leaderboard_position( return pos[0] -async def get_account(member: Union[discord.Member, discord.User, discord.Object]) -> Account: +async def get_account(member: Union[discord.Member, discord.User]) -> Account: """Get the appropriate account for the given user or member. A member is required if the bank is currently guild specific. From 692f1d5c3f999e77a037afd87b3d143d05aa9eb7 Mon Sep 17 00:00:00 2001 From: Michael Oliveira <34169552+Flame442@users.noreply.github.com> Date: Mon, 8 Jan 2024 17:21:54 -0500 Subject: [PATCH 4/5] trailing newlines are now stripped, updated test to expect that --- redbot/pytest/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redbot/pytest/core.py b/redbot/pytest/core.py index 56cd52c8034..57450f8e41c 100644 --- a/redbot/pytest/core.py +++ b/redbot/pytest/core.py @@ -147,7 +147,7 @@ def empty_message(): @pytest.fixture(scope="module") def newline_message(): mock_msg = type("", (), {})() - mock_msg.content = "!test a\nb\nc\n" + mock_msg.content = "!test a\nb\nc" return mock_msg From 23b9b6b1a4766477cae37ba43a4ce6ad6f601bb7 Mon Sep 17 00:00:00 2001 From: Michael Oliveira <34169552+Flame442@users.noreply.github.com> Date: Mon, 8 Jan 2024 19:04:19 -0500 Subject: [PATCH 5/5] slight optimization --- redbot/cogs/alias/alias_entry.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/redbot/cogs/alias/alias_entry.py b/redbot/cogs/alias/alias_entry.py index d2a607bf229..159e68fa4ab 100644 --- a/redbot/cogs/alias/alias_entry.py +++ b/redbot/cogs/alias/alias_entry.py @@ -62,9 +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) - extra[0] = extra[0].strip(" ") - extra[len(extra) - 1] = extra[len(extra) - 1].strip(" ") + extra.append(word.strip(" ")) return extra def to_json(self) -> dict: