我的代码:
from discord.ext import commands
import json
import discord
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 bot.say("You have {} beans in the bank".format(amounts[ctx.message.author.id]))
else:
await bot.say("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 bot.say("You are now registered")
else:
await bot.say("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 bot.say("You do not have an account")
elif other_id not in amounts:
await bot.say("The other party does not have an account")
elif amounts[primary_id] < amount:
await bot.say("You cannot afford this transaction")
else:
amounts[primary_id] -= amount
amounts[other_id] += amount
await bot.say("Transaction complete")
@bot.command()
async def save():
with open('amounts.json', 'w+') as f:
json.dump(amounts, f)
@bot.command()
async def ping(ctx):
await ctx.send('Pong! {0}'.format(round(bot.latency, 1)))
#kick command
@bot.command()
async def kick(ctx, member : discord.Member, *, reason=None):
await member.kick(reason=reason)
#ban command
@bot.command()
async def ban(ctx, member : discord.Member, *, reason=None):
await member.ban(reason=reason)
@bot.command()
async def daily(ctx):
await ctx.send('You earned 200 Beans!')
bot.run('XXX')
我的JSON文件名为“ amounts.json”只有“ {}”
这是在不和谐服务器上输入“ .register”时出现的错误:
Ignoring exception in command register:
Traceback (most recent call last):
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/ext/commands/core.py", line79, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 32, in register
await bot.say("You are now registered")
AttributeError: 'Bot' object has no attribute 'say'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/ext/commands/core.py", line728, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/ext/commands/core.py", line88, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'say'
基本上,我正在尝试制作一个discord机器人,该机器人将允许我使用特定命令在discord服务器上存储和保存玩家数量。后来我想添加一个我不知道怎么做的coinflip和赌博系统。因此,如果有人可以帮助我解决这个问题,将不胜感激,因为我已经在这个想法上停留了一段时间。
我将repl.it用作在线IDE。