2 Framework Bank
Mrcoolgamer11 edited this page 2026-11-22 16:07:10 +00:00

This page is generated from docs/framework_bank.rst - please edit the source file instead of this page.

Bank

Basic Usage

from redbot.core import bank, commands
import discord

class MyCog(commands.Cog):
    @commands.command()
    async def balance(self, ctx, user: discord.Member = commands.Author):
        bal = await bank.get_balance(user)
        currency = await bank.get_currency_name(ctx.guild)
        await ctx.send(
            "{}'s balance is {} {}".format(
                user.display_name, bal, currency
            )
        )

API Reference

Bank

@redbot.core.bank.cost(amount)

Decorates a coroutine-function or command to have a cost.

If the command raises an exception, the cost will be refunded.

You can intentionally refund by raising AbortPurchase (this error will be consumed and not show to users)

Other exceptions will propagate and will be handled by Red's (and/or any other configured) error handling.

exception redbot.core.bank.AbortPurchase

Bases: Exception

class redbot.core.bank.Account(name, balance, created_at)

Bases: object

A single account.

This class should ONLY be instantiated by the bank itself.

class redbot.core.bank.BankPruneInformation(guild, user_id, pruned_users)

Bases: NamedTuple

guild

Alias for field number 0

pruned_users

Alias for field number 2

user_id

Alias for field number 1

class redbot.core.bank.BankSetBalanceInformation(recipient, guild, recipient_old_balance, recipient_new_balance)

Bases: NamedTuple

guild

Alias for field number 1

recipient

Alias for field number 0

recipient_new_balance

Alias for field number 3

recipient_old_balance

Alias for field number 2

class redbot.core.bank.BankTransferInformation(sender, recipient, guild, transfer_amount, sender_new_balance, recipient_new_balance)

Bases: NamedTuple

guild

Alias for field number 2

recipient

Alias for field number 1

recipient_new_balance

Alias for field number 5

sender

Alias for field number 0

sender_new_balance

Alias for field number 4

transfer_amount

Alias for field number 3

class redbot.core.bank.PaydayClaimInformation(member, channel, message, amount, old_balance, new_balance)

Bases: NamedTuple

amount

Alias for field number 3

channel

Alias for field number 1

member

Alias for field number 0

message

Alias for field number 2

new_balance

Alias for field number 5

old_balance

Alias for field number 4

await redbot.core.bank.bank_prune(bot, guild=None, user_id=None)

Prune bank accounts from the bank.

  • Parameters:
    • bot (Red) -- The bot.
    • guild (discord.Guild) -- The guild to prune. This is required if the bank is set to local.
    • user_id (int) -- The id of the user whose account will be pruned. If supplied this will prune only this user's bank account otherwise it will prune all invalid users from the bank.
  • Raises: BankPruneError -- If guild is None and the bank is Local.

await redbot.core.bank.can_spend(member, amount)

Determine if a member can spend the given amount.

  • Parameters:
  • Raises: TypeError -- If the amount is not an int.
  • Returns: True if the member has a sufficient balance to spend the amount, else False.
  • Return type: bool

await redbot.core.bank.deposit_credits(member, amount)

Add a given amount of credits to an account.

await redbot.core.bank.get_account(member)

Get the appropriate account for the given user or member.

A member is required if the bank is currently guild specific.

await redbot.core.bank.get_balance(member)

Get the current balance of a member.

  • Parameters: member (discord.Member) -- The member whose balance to check.
  • Returns: The member's balance
  • Return type: int

await redbot.core.bank.get_bank_name(guild=None)

Get the current bank name.

  • Parameters: guild (discord.Guild, optional) -- The guild to get the bank name for (required if bank is guild-specific).
  • Returns: The bank's name.
  • Return type: str
  • Raises: RuntimeError -- If the bank is guild-specific and guild was not provided.

await redbot.core.bank.get_currency_name(guild=None)

Get the currency name of the bank.

  • Parameters: guild (discord.Guild, optional) -- The guild to get the currency name for (required if bank is guild-specific).
  • Returns: The currency name.
  • Return type: str
  • Raises: RuntimeError -- If the bank is guild-specific and guild was not provided.

await redbot.core.bank.get_default_balance(guild=None)

Get the current default balance amount.

  • Parameters: guild (discord.Guild, optional) -- The guild to get the default balance for (required if bank is guild-specific).
  • Returns: The bank's default balance.
  • Return type: int
  • Raises: RuntimeError -- If the bank is guild-specific and guild was not provided.

await redbot.core.bank.get_leaderboard(positions=None, guild=None)

Gets the bank's leaderboard

  • Parameters:
    • positions (int) -- The number of positions to get
    • guild (discord.Guild) -- The guild to get the leaderboard of. If the bank is global and this is provided, get only guild members on the leaderboard
  • Returns: The sorted leaderboard in the form of (user_id, raw_account)
  • Return type: list of tuple
  • Raises: TypeError -- If the bank is guild-specific and no guild was specified

await redbot.core.bank.get_leaderboard_position(member, , return_lb_size=False)

Get the leaderboard position for the specified user

  • Parameters:
    • member (discord.User or discord.Member) -- The user to get the leaderboard position of.
    • return_lb_size (bool) -- Whether to return a tuple with the position and size of the leaderboard.
  • Returns: The position of the user on the leaderboard, if return_lb_size is False (default) or a tuple of the position of the user and the total leaderboard size. The position may be None, if the user is not on the leaderboard.
  • Return type: Optional[int] or Tuple[Optional[int], int]
  • Raises: TypeError -- If the bank is currently guild-specific and a discord.User object was passed in

await redbot.core.bank.get_max_balance(guild=None)

Get the max balance for the bank.

  • Parameters: guild (discord.Guild, optional) -- The guild to get the max balance for (required if bank is guild-specific).
  • Returns: The maximum allowed balance.
  • Return type: int
  • Raises: RuntimeError -- If the bank is guild-specific and guild was not provided.

await redbot.core.bank.is_global()

Determine if the bank is currently global.

  • Returns: True if the bank is global, otherwise False.
  • Return type: bool

redbot.core.bank.is_owner_if_bank_global()

Restrict the command to the bot owner if the bank is global, otherwise ensure it's used in guild (WITHOUT checking any user permissions).

When used on the command, this should be combined with permissions check like redbot.core.commands.guildowner_or_permissions().

This is a command check.

Example

@bank.is_owner_if_bank_global()
@commands.guildowner()
@commands.group()
async def bankset(self, ctx: commands.Context):
    """Base command for bank settings."""

If the bank is global, the [p]bankset command can only be used by the bot owners in both guilds and DMs. If the bank is local, the command can only be used in guilds by guild and bot owners.

await redbot.core.bank.set_balance(member, amount)

Set an account balance.

  • Parameters:
  • Returns: New account balance.
  • Return type: int
  • Raises:
    • ValueError -- If attempting to set the balance to a negative number.
    • RuntimeError -- If the bank is guild-specific and a discord.User object is provided.
    • BalanceTooHigh -- If attempting to set the balance to a value greater than bank._MAX_BALANCE.
    • TypeError -- If the amount is not an int.

await redbot.core.bank.set_bank_name(name, guild=None)

Set the bank name.

  • Parameters:
    • name (str) -- The new name for the bank.
    • guild (discord.Guild, optional) -- The guild to set the bank name for (required if bank is guild-specific).
  • Returns: The new name for the bank.
  • Return type: str
  • Raises: RuntimeError -- If the bank is guild-specific and guild was not provided.

await redbot.core.bank.set_currency_name(name, guild=None)

Set the currency name for the bank.

  • Parameters:
    • name (str) -- The new name for the currency.
    • guild (discord.Guild, optional) -- The guild to set the currency name for (required if bank is guild-specific).
  • Returns: The new name for the currency.
  • Return type: str
  • Raises: RuntimeError -- If the bank is guild-specific and guild was not provided.

await redbot.core.bank.set_default_balance(amount, guild=None)

Set the default balance amount.

  • Parameters:
    • amount (int) -- The new default balance.
    • guild (discord.Guild, optional) -- The guild to set the default balance for (required if bank is guild-specific).
  • Returns: The new default balance.
  • Return type: int
  • Raises:
    • RuntimeError -- If the bank is guild-specific and guild was not provided.
    • ValueError -- If the amount is less than 0 or higher than the max allowed balance.
    • TypeError -- If the amount is not an int.

await redbot.core.bank.set_global(global_)

Set global status of the bank.

IMPORTANT

All accounts are reset when you switch!

  • Parameters: global (bool) -- True will set bank to global mode.
  • Returns: New bank mode, True is global.
  • Return type: bool
  • Raises: RuntimeError -- If bank is becoming global and a discord.Member was not provided.

await redbot.core.bank.set_max_balance(amount, guild=None)

Set the maximum balance for the bank.

  • Parameters:
    • amount (int) -- The new maximum balance.
    • guild (discord.Guild, optional) -- The guild to set the max balance for (required if bank is guild-specific).
  • Returns: The new maximum balance.
  • Return type: int
  • Raises:
    • RuntimeError -- If the bank is guild-specific and guild was not provided.
    • ValueError -- If the amount is less than 0 or higher than 2 ** 63 - 1.
    • TypeError -- If the amount is not an int.

await redbot.core.bank.transfer_credits(from_, to, amount)

Transfer a given amount of credits from one account to another.

  • Parameters:
  • Returns: The new balance of the member gaining credits.
  • Return type: int
  • Raises:
    • ValueError -- If the amount is invalid or if from_ has insufficient funds.
    • TypeError -- If the amount is not an int.
    • RuntimeError -- If the bank is guild-specific and a discord.User object is provided.
    • BalanceTooHigh -- If the balance after the transfer would be greater than bank._MAX_BALANCE.

await redbot.core.bank.wipe_bank(guild=None)

Delete all accounts from the bank.

  • Parameters: guild (discord.Guild) -- The guild to clear accounts for. If unsupplied and the bank is per-server, all accounts in every guild will be wiped.

await redbot.core.bank.withdraw_credits(member, amount)

Remove a certain amount of credits from an account.

  • Parameters:
  • Returns: New account balance.
  • Return type: int
  • Raises:
    • ValueError -- If the withdrawal amount is invalid or if the account has insufficient funds.
    • TypeError -- If the withdrawal amount is not an int.