如何解决Discord money机器人将用户ID保留在json文件中Bot重新启动时,会为每个人创建一个新的但相同的ID
发生这种情况是因为JSON对象始终具有用于“键”的字符串。因此json.dump
将整数键转换为字符串。您可以通过在使用用户ID之前将其转换为字符串来完成相同的操作。
from discord.ext import commands
import discord
import json
bot = commands.Bot('!')
amounts = {}
@bot.event
async def on_ready():
global amounts
try:
with open('amounts.json') as f:
amounts = json.load(f)
except FileNotFoundError:
print("Could not load amounts.json")
amounts = {}
@bot.command(pass_context=True)
async def balance(ctx):
id = str(ctx.message.author.id)
if id in amounts:
await ctx.send("You have {} in the bank".format(amounts[id]))
else:
await ctx.send("You do not have an account")
@bot.command(pass_context=True)
async def register(ctx):
id = str(ctx.message.author.id)
if id not in amounts:
amounts[id] = 100
await ctx.send("You are now registered")
_save()
else:
await ctx.send("You already have an account")
@bot.command(pass_context=True)
async def transfer(ctx, amount: int, other: discord.Member):
primary_id = str(ctx.message.author.id)
other_id = str(other.id)
if primary_id not in amounts:
await ctx.send("You do not have an account")
elif other_id not in amounts:
await ctx.send("The other party does not have an account")
elif amounts[primary_id] < amount:
await ctx.send("You cannot afford this transaction")
else:
amounts[primary_id] -= amount
amounts[other_id] += amount
await ctx.send("Transaction complete")
_save()
def _save():
with open('amounts.json', 'w+') as f:
json.dump(amounts, f)
@bot.command()
async def save():
_save()
bot.run("Token")
解决方法
运行此代码时,它可以从不一致的用户ID中获取用户ID,并将其放入json中,其中有100美元。但是,一旦您重新启动bot,就必须重新注册,并在json文件中写入相同的用户ID,并认为这是新用户。它不是。
from discord.ext import commands
import discord
import json
bot = commands.Bot('!')
amounts = {}
@bot.event
async def on_ready():
global amounts
try:
with open('amounts.json') as f:
amounts = json.load(f)
except FileNotFoundError:
print("Could not load amounts.json")
amounts = {}
@bot.command(pass_context=True)
async def balance(ctx):
id = ctx.message.author.id
if id in amounts:
await ctx.send("You have {} in the bank".format(amounts[id]))
else:
await ctx.send("You do not have an account")
@bot.command(pass_context=True)
async def register(ctx):
id = ctx.message.author.id
if id not in amounts:
amounts[id] = 100
await ctx.send("You are now registered")
_save()
else:
await ctx.send("You already have an account")
@bot.command(pass_context=True)
async def transfer(ctx,amount: int,other: discord.Member):
primary_id = ctx.message.author.id
other_id = other.id
if primary_id not in amounts:
await ctx.send("You do not have an account")
elif other_id not in amounts:
await ctx.send("The other party does not have an account")
elif amounts[primary_id] < amount:
await ctx.send("You cannot afford this transaction")
else:
amounts[primary_id] -= amount
amounts[other_id] += amount
await ctx.send("Transaction complete")
_save()
def _save():
with open('amounts.json','w+') as f:
json.dump(amounts,f)
@bot.command()
async def save():
_save()
bot.run("Token")
漫游器关闭后重新打开并注册两次后的JSON(虚假用户ID):
{"56789045678956789": 100,"56789045678956789": 100}
需要它即使在关闭和重新启动漫游器后也能够识别用户ID。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。