Skip to content

Conversation

@yamikaitou
Copy link
Contributor

@yamikaitou yamikaitou commented Sep 18, 2021

Adds some event dispatches to Bank

red_bank_set_global
red_bank_set_balance
red_bank_transfer_credits
red_bank_wipe
red_bank_prune_accounts

Resolves #5141

@github-actions github-actions bot added Category: Core - API - Bank This is related to the core Bank API. Category: Bot Core labels Sep 18, 2021
@Jackenmen Jackenmen added this to the 3.4.x milestone Sep 18, 2021
@github-actions github-actions bot added Category: Docs - Other This is related to documentation that doesn't have its dedicated label. Category: Tests labels May 13, 2022
@yamikaitou yamikaitou marked this pull request as ready for review May 13, 2022 21:44
@github-actions github-actions bot added Category: Cogs - Economy This is related to the Economy cog. Category: Docs - For Developers This is related to developer (cog creator) documentation. Category: Core - Bot Class This is related to the `redbot.core.bot.Red` class. and removed Category: Docs - Other This is related to documentation that doesn't have its dedicated label. labels Mar 25, 2024
self.recipient_new_balance = recipient_new_balance


class BankPruneInformation:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type annotation for pruned_users should be Union[List[int], Dict[str, List[int]]] instead of Union(List[int], Dict[str, List[int]])

@Zephyrkul
Copy link
Contributor

Would it be possible it to make these payload classes into NamedTuples instead? That should be a free performance improvement since these classes don't need to be modified after they're created.

Copy link
Member

@Flame442 Flame442 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial design feedback.

Comment on lines +14 to +16
.. py:method:: red_bank_set_balance(payload: redbot.core.bank.BankSetBalanceInformation)
Dispatched when a user has their balance changed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documented events that dispatch custom objects should link to those objects in some way. It might make sense to use a similar format as the autogenerated API descriptions for methods, which have a "parameters" section describing each of the parameters in detail.

return deco


class BankTransferInformation(NamedTuple):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These objects should be added to __all__, since they are part of the public API. They also need to be documented, which may or may not happen automatically after doing so.

"""

print(f"{guild} | {user_id}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover print?

await group.name.set(member.display_name)

payload = BankSetBalanceInformation(
member.id, (0 if _cache_is_global else guild.id), old_balance, amount
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more typical for guild like attributes to be Optional and use None as the value to represent non-guild actions (IE, message.guild). If it is consistently possible to provide the full user/guild object rather than an ID, I also think that would be preferred.


.. py:method:: red_bank_wipe(guild_id: int)
Dispatched when the Bank gets wiped. :code:`guild_id` will be the ID of the Guild that was wiped, 0 if all Guilds were wiped (Bank is Local), or -1 if all Users were wiped (Bank is Global)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having both 0 and -1 as special values seems a bit smelly. Is there a meaningful difference for the CC to account for that makes differentiating between wiping guilds or users something that needs to happen? If so, I think it would be neater to just have a second argument, whether or not the bank was global at the time of wiping.

I would say the CC can just check if the bank is global or not to get the same value, but that only applies for [p]bankset reset. [p]bankset toggleglobal also implicitly results in this being called, since it clears the bank during the process of swapping scope. Trying to test for it at that time would likely result in an incorrect value due to race conditions.

if user_id in bank_data:
del bank_data[user_id]

payload = BankPruneInformation(scope, tmp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tmp is only defined when user_id is None. This errors if a particular user is pruned.
(I do think the conditionally defined variables in this function are smelly, making this sort of error very easy to make)

@Flame442 Flame442 self-assigned this Mar 28, 2024
@Flame442 Flame442 added the Type: Feature New feature or request. label Mar 28, 2024
Comment on lines +548 to +553
if guild is None and user_id is None:
scope = 1 # prune global
if guild is not None and user_id is None:
scope = 2 # prune server
else:
scope = 3 # prune user
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to hit submit on the comment I was writing here....

Doing it like this loses the ability to know what guild was being pruned. Unless there is some reason to differentiate between specifically pruning a member and a single member happening to be the only invalid member from a bulk prune, the signature of this event object would likely be better as:
guild: Optional[discord.Guild], pruned_user_ids: List[int]

vertyco added a commit to vertyco/Red-DiscordBot that referenced this pull request Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Category: Cogs - Economy This is related to the Economy cog. Category: Core - API - Bank This is related to the core Bank API. Category: Core - Bot Class This is related to the `redbot.core.bot.Red` class. Category: Docs - For Developers This is related to developer (cog creator) documentation. Type: Feature New feature or request.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bank] Add event dispatch on account events

6 participants