如何为不和谐机器人中的每个用户创建一个变量。 discord.py

How can I make a variable for every user in discord bot. discord.py

我有一个机器人,想为 discord.py 中的用户创建单独的变量。 例)如果一个人是 Bob,另一个是 Ted,Bob 可能有 100 个硬币,但 ted 只有 5 个。这也适用于加入

的任何额外用户

您可能想要为用户创建一个 class,然后列出用户实例:

# definition of the User class
class User():
    def __init__(self, name: str, coins: int):
        self.name = name
        self.coins = coins
    
    def addCoins(self, amount: int):
        self.coins += amount
    
# You could also put them into a dictionary - it's up to you how you want to store them.
# In this case I'm just using a list for simplicity.

users = []
users.append(User("Bob", 100))
users.append(User("Ted", 5))
    
# print Bob's coins
print(users[0].coins)
    
# 100
    
# print Ted's coins
print(users[1].coins)
    
# 5
    
# add coins to Ted's account
users[1].addCoins(10)
    
# print Ted's coins again
print(users[1].coins)
    
# 15

如果您还没有研究过列表、字典和循环,我建议您研究一下。