mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-23 11:13:51 -05:00
[V3 Economy] Expand payday output (#1386)
* [V3 Economy] implement suggestions from #1371 * Fix a typo * Add functions for getting leaderboard and leaderboard position * Use the new functions to get leaderboard position and leaderboard (overrides https://github.com/Cog-Creators/Red-DiscordBot/pull/1435) * Actually implement showing only guild members on leaderboard when bank is global * get_leaderboard_position needs to be awaited * For global bank, pass None for guild to get_leaderboard when trying to find position * Remove some unneeded code * Wrong index... * Combine 3 messages into 1 * Fix guild leaderboard while bank is global * add missing parentheses * Modify the leaderboard formatting * More work on leaderboard formatting * no subtraction
This commit is contained in:
@@ -243,7 +243,7 @@ async def deposit_credits(member: discord.Member, amount: int) -> int:
|
||||
|
||||
"""
|
||||
if _invalid_amount(amount):
|
||||
raise ValueError("Invalid withdrawal amount {} <= 0".format(amount))
|
||||
raise ValueError("Invalid deposit amount {} <= 0".format(amount))
|
||||
|
||||
bal = await get_balance(member)
|
||||
return await set_balance(member, amount + bal)
|
||||
@@ -287,62 +287,81 @@ async def wipe_bank():
|
||||
await _conf.clear_all_members()
|
||||
|
||||
|
||||
async def get_guild_accounts(guild: discord.Guild) -> List[Account]:
|
||||
"""Get all account data for the given guild.
|
||||
async def get_leaderboard(positions: int=None, guild: discord.Guild=None) -> List[tuple]:
|
||||
"""
|
||||
Gets the bank's leaderboard
|
||||
|
||||
Parameters
|
||||
----------
|
||||
positions : `int`
|
||||
The number of positions to get
|
||||
guild : discord.Guild
|
||||
The guild to get accounts for.
|
||||
The guild to get the leaderboard of. If the bank is global and this
|
||||
is provided, get only guild members on the leaderboard
|
||||
|
||||
Returns
|
||||
-------
|
||||
`list` of `Account`
|
||||
A list of all guild accounts.
|
||||
`list` of `tuple`
|
||||
The sorted leaderboard in the form of :code:`(user_id, raw_account)`
|
||||
|
||||
Raises
|
||||
------
|
||||
RuntimeError
|
||||
If the bank is currently global.
|
||||
TypeError
|
||||
If the bank is guild-specific and no guild was specified
|
||||
|
||||
"""
|
||||
if await is_global():
|
||||
raise RuntimeError("The bank is currently global.")
|
||||
|
||||
ret = []
|
||||
accs = await _conf.all_members(guild)
|
||||
for user_id, acc in accs.items():
|
||||
acc_data = acc.copy() # There ya go kowlin
|
||||
acc_data['created_at'] = _decode_time(acc_data['created_at'])
|
||||
ret.append(Account(**acc_data))
|
||||
return ret
|
||||
raw_accounts = await _conf.all_users()
|
||||
if guild is not None:
|
||||
tmp = raw_accounts.copy()
|
||||
for acc in tmp:
|
||||
if not guild.get_member(acc):
|
||||
del raw_accounts[acc]
|
||||
else:
|
||||
if guild is None:
|
||||
raise TypeError("Expected a guild, got NoneType object instead!")
|
||||
raw_accounts = await _conf.all_members(guild)
|
||||
sorted_acc = sorted(raw_accounts.items(), key=lambda x: x[1]['balance'], reverse=True)
|
||||
if positions is None:
|
||||
return sorted_acc
|
||||
else:
|
||||
return sorted_acc[:positions]
|
||||
|
||||
|
||||
async def get_global_accounts() -> List[Account]:
|
||||
"""Get all global account data.
|
||||
async def get_leaderboard_position(member: Union[discord.User, discord.Member]) -> Union[int, None]:
|
||||
"""
|
||||
Get the leaderboard position for the specified user
|
||||
|
||||
Parameters
|
||||
----------
|
||||
member : `discord.User` or `discord.Member`
|
||||
The user to get the leaderboard position of
|
||||
|
||||
Returns
|
||||
-------
|
||||
`list` of `Account`
|
||||
A list of all global accounts.
|
||||
`int`
|
||||
The position of the user on the leaderboard
|
||||
|
||||
Raises
|
||||
------
|
||||
RuntimeError
|
||||
If the bank is currently guild specific.
|
||||
TypeError
|
||||
If the bank is currently guild-specific and a `discord.User` object was passed in
|
||||
|
||||
"""
|
||||
if not await is_global():
|
||||
raise RuntimeError("The bank is not currently global.")
|
||||
|
||||
ret = []
|
||||
accs = await _conf.all_users() # this is a dict of user -> acc
|
||||
for user_id, acc in accs.items():
|
||||
acc_data = acc.copy()
|
||||
acc_data['created_at'] = _decode_time(acc_data['created_at'])
|
||||
ret.append(Account(**acc_data))
|
||||
|
||||
return ret
|
||||
if await is_global():
|
||||
guild = None
|
||||
else:
|
||||
guild = member.guild if hasattr(member, "guild") else None
|
||||
try:
|
||||
leaderboard = await get_leaderboard(None, guild)
|
||||
except TypeError:
|
||||
raise
|
||||
else:
|
||||
pos = discord.utils.find(lambda x: x[1][0] == member.id, enumerate(leaderboard, 1))
|
||||
if pos is None:
|
||||
return None
|
||||
else:
|
||||
return pos[0]
|
||||
|
||||
|
||||
async def get_account(member: Union[discord.Member, discord.User]) -> Account:
|
||||
|
||||
Reference in New Issue
Block a user