python - Discord Currency Bot discord.py 需要帮助
问题描述
我使用 discord.py API 用 python 制作了一个不和谐的货币机器人。但问题是当您执行 ?give {user} 时。它会起作用,但会在聊天中弹出,但在 .json 文件中,即使您执行 ?save 手动保存,金额也不会改变。我会发送代码,但我现在不在我的电脑上。因此,如果您可以解决此问题,请回复,我将向您发送我的不和谐用户。这是代码,因此您可以更好地帮助我。
from discord.ext import commands
import discord
import json
bot = commands.Bot(command_prefix=commands.when_mentioned_or(f"?"), help_command=None)
print("Online")
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, name='bal', help='Shows you how much money you have in the bank.')
async def bal(ctx):
id = str(ctx.message.author.id)
if id in amounts:
await ctx.send("You have {} dollars in the bank".format(amounts[id]))
else:
await ctx.send("You do not have an account yet. Type ?register to register!")
@bot.command(pass_context=True, name='register', help='Register to start using Bot currency!')
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! Remember, type ?save to save!")
_save()
else:
await ctx.send("You already have an account.")
@bot.command(name='save', help='Your currency autosaves, but remember to always save just in case! This saves your currency.')
async def save(ctx):
_save()
await ctx.send("Data saved!")
@bot.command(pass_context=True, name='give', help='Give another member some money!')
async def give(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(invoke_without_command=True)
async def help(ctx):
em = discord.Embed(title = "Bot Help", description = "Remember invite your friends",color = ctx.author.color)
em.add_field(name = "Balance Command", value = "Do ?bal to see your ballance.", inline=False)
em.add_field(name = "Register Command", value = "Do ?register to make a bank account.", inline=False)
em.add_field(name = "Save Command", value = "Do ?save to save your data so it wont reset.", inline=False)
em.add_field(name = "Give Command", value = "Do ?give to give people money they must have an active bank account.", inline=False)
await ctx.send(embed = em)
@bot.command()
async def rob(ctx, member:discord.Member):
await ctx.send(f" You are now attempting to rob {member.mention}")
解决方案
当您保存时,amounts
我相信您正在保存原始变量。考虑更新您的保存功能,使其看起来像这样:
def save_(var_to_save):
with open('amounts.json', 'w+') as f:
json.dump(var_to_save, f)
然后,当调用你的保存函数时
save_(amounts)
这可确保您保存的是更新后的变量,而不是最初打开的数据。
编辑:您还应该更新打开文件的方式。
def open():
with open('amounts.json') as f:
amounts = json.load(f)
return amounts
然后,每当您启动命令/函数并想要访问文件的最新版本时,调用该open
函数以检索该信息。
@bot.command()
async def bal(ctx):
amounts = open()
etc.
推荐阅读
- r - 如何在情节中旋转多类别x轴刻度标签?
- elasticsearch - Kibana 返回 200 代码但浏览器显示未就绪
- javascript - 如何将可编辑表的数据与其他数据一起从表单发送到控制器?
- laravel - Laravel Vapor 部署,然后无法响应请求
- ruby-on-rails - Rails 常量赋值
- c# - 具有可变秒数的 DateTime.ParseExact
- java - 无状态服务器如何在服务器端保持会话状态?
- javascript - 固定页脚重叠内容
- html - 用于在html中模糊图像的算法
- javascript - 通过 Javascript 功能实时编辑 HTML