所以我试着做一个经济机器人,用户可以先注册,这样他们就可以成为经济系统的一部分,现在我试着让机器人给发送命令的用户随机的收入,然后我遇到了一个错误,赚钱命令没有像预期的那样工作,这里是完整的代码,问题出在(getprimo)上:
@client.command(pass_context=True)
async def wallet(ctx):
id = str(ctx.message.author.id)
if id in amounts:
em1 = discord.Embed(title = f'Wallet', description = "You have {} <:primogem:853895082646044672> in paimon bank <:_Paimon6:827074349450133524>.".format(amounts[id]), color= ctx.author.color)
await ctx.send(embed = em1)
else:
em2 = discord.Embed(title = f'Account Not Registered', description = f'You do not have an account <:_pBaffled:827075083670650950>.\nUse %register to make an account.', color= ctx.author.color)
await ctx.send(embed = em2)
@client.command(pass_context=True)
async def register(ctx):
id = str(ctx.message.author.id)
if id not in amounts:
amounts[id] = 100
em3 = discord.Embed(title = f'Account Successfully Registered', description = f'You are now registered <:_Paimon6:827074349450133524>.', color= ctx.author.color)
await ctx.send(embed = em3)
_save()
else:
em4 = discord.Embed(title = f'Account Already Registered', description = f'You already have an account <:_pBaffled:827075083670650950>.', color= ctx.author.color)
await ctx.send(embed = em4)
@client.command(pass_context=True)
async def send(ctx, other: discord.Member, amount: int):
primary_id = str(ctx.message.author.id)
other_id = str(other.id)
if primary_id not in amounts:
em5 = discord.Embed(title = f'Account Not Registered', description = f'You do not have an account <:_pBaffled:827075083670650950>.\nUse %register to make an account.', color= ctx.author.color)
await ctx.send(embed = em5)
elif other_id not in amounts:
em6 = discord.Embed(title = f'Account Not Registered', description = f'You cant send <:primogem:853895082646044672> to someone that does not have an account.\nTell the person to use %register first in order to send the <:primogem:853895082646044672>.', color= ctx.author.color)
await ctx.send(embed = em6)
elif amounts[primary_id] < amount:
em7 = discord.Embed(title = f'Not Enough Primogems', description = f'Insufficient <:primogem:853895082646044672>.\nCant send <:primogem:853895082646044672> to {other.mention} <:_pHug:827086739435683840>.', color= ctx.author.color)
await ctx.send(embed = em7)
else:
amounts[primary_id] -= amount
amounts[other_id] += amount
em8 = discord.Embed(title = f'Send Successful', description = f'Done sending {amount} <:primogem:853895082646044672> to {other.mention} <:_Paimon6:827074349450133524>.', color= ctx.author.color)
await ctx.send(embed = em8)
_save()
@client.command(pass_context=True)
@commands.cooldown(1, 300, commands.BucketType.user)
async def getprimo(ctx):
id = str(ctx.message.author.id)
amount = random.randrange(0, 100)
amounts[id] += amount
em9 = discord.Embed(title = f'Primogems Earned', description = f'You get {amount} <:primogem:853895082646044672>.\nPlease wait 5 minutes to get more primo <:_Paimon6:827074349450133524>.', color= ctx.author.color)
await ctx.send(embed = em9)
_save()
def _save():
with open('amounts.json', 'w+') as f:
json.dump(amounts, f)
@client.command()
async def save():
_save()
任何帮助都将不胜感激,谢谢。
发布于 2021-07-08 16:55:13
好了,我终于找到了答案,很抱歉之前问了这个问题。如果你想知道,这是我之前遇到的问题的修复代码。我更改了“数量”和"id“,因为这只是一个测试。
@client.command(pass_context=True)
@commands.cooldown(1, 300, commands.BucketType.user)
async def getprimo(ctx):
primaryid = str(ctx.message.author.id)
earning = random.randint(0, 100)
amounts[primaryid] += earning
em9 = discord.Embed(title = f'Primogems Earned', description = f'You get {earning} <:primogem:853895082646044672>.\nPlease wait 5 minutes to get more primo <:_Paimon6:827074349450133524>.', color= ctx.author.color)
await ctx.send(embed = em9)
_save()我之前使用的是random.randrange,而不是random.randint,这就是该命令不起作用的原因。
https://stackoverflow.com/questions/68296931
复制相似问题