不和谐的Py任务。不会运行任务

时间:2020-06-27 06:08:14

标签: python discord.py discord.py-rewrite

我需要以下代码的帮助:

import discord
from discord.ext import commands, tasks
import os

TOKEN = "tokenthing"
client = commands.Bot(command_prefix="$$")
@client.event
async def on_ready():
        test.start()
        print("Succesful Login as {0.user}".format(client))
        


@client.command()
async def GiveMoney(ctx, user : discord.User, value : int):
        if value <= 0:
                await ctx.send("Can't send negative money.")
                return
        else:
                pass
        
        sender = str(ctx.author)
        reciever = str(user)

        if os.path.exists(sender+".txt"):
                pass
        else: 
                await ctx.send("You Don't have an account. Type $$CreateAccount to create one.")
                return

        if os.path.exists(reciever+".txt"):
                pass
        else:
                await ctx.send("The Person you are trying to give money doesn't have a bank Account")
                return

        try:
                f= open(sender+".txt", "r")
                balance = f.read()
                f.close()
                balance = int(balance)
                balance = balance - value
                balance = str(balance)
                f = open(sender+".txt", "w")
                f.write(balance)
                f.close()
        except FileNotFoundError:
                await ctx.send("You don't have a Bank account. Type $$CreateAccount to create one.")
                return
        try:
                f = open(reciever+".txt", "r")
                balance = f.read()
                f.close()
                balance = int(balance)
                balance = balance + value
                balance = str(balance)
                f = open(reciever+".txt", "w")
                f.write(balance)
                f.close()
        except FileNotFoundError:
                await ctx.send("The Person You are sending money dosen't have an account")
        print("{0} sent {1} to {2}".format(sender, value, reciever))


@tasks.loop(seconds=10)
async def test(ctx):
        for i in range(10):
                print(i)




client.run(TOKEN)

我似乎一辈子都无法让它在机器人就绪状态下运行任务。 我没有收到任何错误或似乎只是跳过它的任何内容。 我在Windows 10计算机上使用Discord.py-rewrite。 我怀疑我错过了一些命令,但不确定。

谢谢

1 个答案:

答案 0 :(得分:1)

只需在开始循环时添加await。因为您的函数是async。示例:

@client.event
async def on_ready():
    await test.start()