用python写一个不和谐的机器人

时间:2020-04-12 22:26:06

标签: python discord.py

我有一个有效的货币系统,但是我们想要赚钱的方法。所以我想制作迷你游戏,所以我制作了投币游戏,但是我无法正常工作,它告诉我在尝试时找不到命令。我也想制作这样的游戏,因此游戏成本>货币所以您可以赢一点,所以基本上博彩

from discord.ext import commands
import discord
import json
import random
from discord.ext.commands import Bot
from discord import Game
import time
import asyncio
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 {} :Ereb: 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.command() #problem
async def on_message(message):
    if message.content.startswith('$coinflip'):
        randomlist = ["heads","tails",]
        await client.send_message(message.channel,(random.choice(randomlist)))***

bot.run("token")

    ```

1 个答案:

答案 0 :(得分:1)

您需要用on_message而不是bot.event装饰bot.command回调。您还需要添加bot.process_commands行,以便正常调用其他命令。

@bot.event
async def on_message(message):
    if message.content.startswith('$coinflip'):
        randomlist = ["heads","tails",]
        await client.send_message(message.channel,(random.choice(randomlist)))
    else:
        await bot.process_commands(message)