discord.py tasks.loop() 没有执行

时间:2021-06-14 14:20:40

标签: discord.py task

当我尝试启动机器人时出现错误:TypeError: generator() missing 1 required positional argument: 'ctx'

async def on_ready():
  activity = discord.Game(name="« g! »", type=3)
  await bot.change_presence(status=discord.Status.online, activity=activity)
  print("Bot is ready!")
  print("---------------------------")
  generator.start()


def pretty(number):
    return ("{:,}".format(number))

def genCode(length):
    code = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits)
    for _ in range(length))
    return code


@tasks.loop(minutes = 30)
async def generator(ctx, amount: typing.Optional[int] = 100):
    codeStr = ''
    if amount > 100000:
        amount = 100000
    for x in range(amount):
        if x == amount - 1:
            codeStr += "discord.gift/" + genCode(16)
        else:
            codeStr += "discord.gift/" + genCode(16) + "\n"
        if x == amount - 1:
            name = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits)
            for _ in range(3)) + "codes.txt"
            f = open(name, "x")
            f.write(codeStr)
            f.close()
            await ctx.channel.send(file=discord.File(r'./' + name))
            os.remove(name)

输出:https://imgur.com/kMamq2O

1 个答案:

答案 0 :(得分:0)

错误说明了一切。您不能在事件中使用 ctx,因为 ctx 表示激发命令的上下文。在此处阅读有关 ctx 的文档:https://discordpy.readthedocs.io/en/stable/ext/commands/api.html

为了向特定频道发送消息,请使用 bot.get_channel(id),它将返回一个 discord.Channel 对象,然后您可以使用 discord.Channel.send() 向该频道发送消息。

示例

channel = bot.get_channel(some_id)
await channel.send("foo")

此类机器人违反 Discord ToS,您可能会面临机器人和帐户被标记的风险。

相关问题