【问题标题】:How am I supposed to make the loop/task begin?我应该如何开始循环/任务?
【发布时间】:2020-10-07 12:48:51
【问题描述】:

所以,我正在尝试使用 Python 3.9.0 和 Discord.py 编写 Discord Bot。我已经开始编写一些代码,以及简单的命令,例如在机器人被召唤的频道中发送消息。

现在,我想运行一个每 10 秒发生一次的循环(此处为示例),但我无法对其进行初始化。我该怎么做?

我已经尝试输入:vote_auto.start(),但它说它缺少 1 个必需的位置参数:“ctx”。

import discord
from discord.ext import commands, tasks
from itertools import cycle

bot = commands.Bot(command_prefix = '.')

@bot.event
async def on_ready():
     print("Gol.D.Roger Bot est prêt.")
     await vote_auto(ctx).start()

@bot.command()
async def votez(ctx):
     await ctx.send("N’oubliez pas d’aller VOTER ! C'est un nouveau mois qui 
     s'annonce et qui dit nouveau cycle dit RESET TOP-SITE ! Faites de votre 
     mieux pour nous donner la force. C'est aussi pour vous quon veut rester 
     au TOP ! :star_struck:  ET SURTOUT ! Cette semaine compte TRIPLE !!! 
     :fire: :cent: :clap:")
await ctx.send('https://onepieceanarchy.forumactif.com/t479-10-votes-et-recompenses')
await ctx.send('https://media1.tenor.com/images/4f02345231c710ae0a96098086a46c4f/tenor.gif?itemid=14816222')

 @tasks.loop(seconds=10)
  async def vote_auto(ctx):
      print('Loop Vote Begun')
      await ctx.send("N’oubliez pas d’aller VOTER ! C'est un nouveau mois qui s'annonce et qui dit nouveau cycle dit RESET TOP-SITE ! Faites de votre mieux pour nous donner la force. C'est aussi pour vous quon veut rester au TOP ! :star_struck:  ET SURTOUT ! Cette semaine compte TRIPLE !!! :fire: :cent: :clap:")
      await ctx.send('https://onepieceanarchy.forumactif.com/t479-10-votes-et-recompenses')
      await ctx.send('https://media1.tenor.com/images/4f02345231c710ae0a96098086a46c4f/tenor.gif?itemid=14816222')

     bot.run('NzYzMTIwNzI2MjI3MTU3MDEz.X3zFjw.butp19UlqX2h1Om9B6tG3Hr7riQ')

如果有人可以帮助我,请。

【问题讨论】:

  • 在此处粘贴您的代码。没有人会从照片中输入您的代码。 vote_auto 需要一个参数,而你没有发送任何可能是原因
  • 是的,对不起,我对堆栈溢出很陌生,所以我只是为代码做了一个屏幕截图!我应该如何添加这个参数?我已经尝试了一些东西,但它不起作用。 @bot.event async def on_ready(): print("Gol.D.Roger Bot est prêt.") await vote_auto.start()
  • 您已在问题中包含您的机器人令牌。现在任何人都可以劫持你的机器人。您需要在 discord 开发者网站上重新生成令牌,而不是在此处使用更新的令牌。您的任务将无法执行,因为ctx 无法通过。您需要确定要将消息发送到哪个频道/用户,并使用 await channel.send 之类的东西而不是 await ctx.send
  • 啊,是的,我忘了!我刚改了,谢谢你让我注意到!

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


【解决方案1】:

Docs for discord.ext.tasks

task 没有ctx 参数,ctxcommand invocation Context。想想看,这个 ctx 参数是从哪里来的呢?

async def vote_auto(ctx): 应该只是 async def vote_auto():

task.start() 不是协程,您只需使用vote_auto.start() 来开始您的任务。如 cmets 中所述,您应该在任务中使用 get_channel,然后使用 channel.send 而不是 ctx.send

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多