Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 制作一个cog,在指定的chat discord.py中每10秒发送一条消息_Python_Python 3.x_Discord.py_Discord.py Rewrite_Python 3.8 - Fatal编程技术网

Python 制作一个cog,在指定的chat discord.py中每10秒发送一条消息

Python 制作一个cog,在指定的chat discord.py中每10秒发送一条消息,python,python-3.x,discord.py,discord.py-rewrite,python-3.8,Python,Python 3.x,Discord.py,Discord.py Rewrite,Python 3.8,这是到目前为止我的代码。当我运行它时,会出现以下错误: from discord.ext import commands, tasks class sendmessage10seconds(commands.Cog): def __init__(self, client): self.client = client print("Cog is running") @tasks.loop(seconds=10)

这是到目前为止我的代码。当我运行它时,会出现以下错误:

from discord.ext import commands, tasks



class sendmessage10seconds(commands.Cog):

    def __init__(self, client):
        self.client = client
        print("Cog is running")


    @tasks.loop(seconds=10)
    async def sendmessage(ctx, self):
        channel = self.get_channel(802273252973477960)
        await channel.send("Hi")
    sendmessage.start()

def setup(client):
    client.add_cog(sendmessage10seconds(client))
我做错了什么

  • self
    始终作为第一个参数,而不是第二个参数
  • async def sendmessage(self,ctx):
    
  • 启动任务时,您需要传递
    ctx
    参数(或者根本不传递,不需要传递)

  • 没有所谓的
    self.get_channel
    (请记住,您不是继承自
    discord.Client
    commands.Bot
    ),而是
    self.Client.get_channel

  • channel=self.client.get\u通道(…)
    
  • 您需要在函数或命令中启动任务
  • @commands.command()
    异步def启动(自身、ctx):
    self.sendmessage.start()#相应地传递'ctx'参数
    
    编辑:在
    方法中启动循环

    def\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
    self.client=client
    self.sendmages.start()
    
    注意:您应该添加等待,直到bot准备好加载内部缓存,一种方法是使用decorator
    {task}.before\u loop
    和使用
    bot.wait\u直到\u ready

    @sendmessage.before\u循环
    发送消息之前的异步定义(自):
    等待self.client。等待,直到准备就绪()
    
    您的完整固定代码

    从discord.ext导入命令、任务
    类SendMessage10s(commands.Cog):
    定义初始化(自我,客户机):
    self.client=client
    self.sendmessage.start()
    @tasks.loop(秒=10)
    async def sendmessage(self):#如果需要,可以传递'ctx'参数,但这样做没有意义
    通道=self.client.get_通道(80227322973477960)
    等待频道。发送(“Hi”)
    @sendmessage.before\u循环
    发送消息之前的异步定义(自):
    等待self.client。等待,直到准备就绪()
    def设置(客户端):
    client.add_cog(发送消息10秒(客户端))
    
  • self
    始终作为第一个参数,而不是第二个参数
  • async def sendmessage(self,ctx):
    
  • 启动任务时,您需要传递
    ctx
    参数(或者根本不传递,不需要传递)

  • 没有所谓的
    self.get_channel
    (请记住,您不是继承自
    discord.Client
    commands.Bot
    ),而是
    self.Client.get_channel

  • channel=self.client.get\u通道(…)
    
  • 您需要在函数或命令中启动任务
  • @commands.command()
    异步def启动(自身、ctx):
    self.sendmessage.start()#相应地传递'ctx'参数
    
    编辑:在
    方法中启动循环

    def\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
    self.client=client
    self.sendmages.start()
    
    注意:您应该添加等待,直到bot准备好加载内部缓存,一种方法是使用decorator
    {task}.before\u loop
    和使用
    bot.wait\u直到\u ready

    @sendmessage.before\u循环
    发送消息之前的异步定义(自):
    等待self.client。等待,直到准备就绪()
    
    您的完整固定代码

    从discord.ext导入命令、任务
    类SendMessage10s(commands.Cog):
    定义初始化(自我,客户机):
    self.client=client
    self.sendmessage.start()
    @tasks.loop(秒=10)
    async def sendmessage(self):#如果需要,可以传递'ctx'参数,但这样做没有意义
    通道=self.client.get_通道(80227322973477960)
    等待频道。发送(“Hi”)
    @sendmessage.before\u循环
    发送消息之前的异步定义(自):
    等待self.client。等待,直到准备就绪()
    def设置(客户端):
    client.add_cog(发送消息10秒(客户端))
    
    没有错误,但什么也没有发生。没有消息被发送,cog正在运行(我在init中放了一个print语句)好吧,你必须使用
    start
    命令来启动它,另一种方法是在
    \uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
    方法中启动它,但是你应该等待bot准备好,然后使用
    bot。等待,直到\uu ready
    完成缓存的加载,让我编辑我的答案,我在sendmessage函数之后添加了“sendmessage.start()”,其缩进级别与@tasks.loop相同。它输出了以下错误:``内部后台任务'sendmessage'中未处理的异常。回溯(最近一次调用):文件“/opt/virtualenvs/python3/lib/python3.8/site packages/discord/ext/tasks/u_init___;.py”,第101行,在循环等待self.coro(*args,**kwargs)类型错误:sendmessage()缺少一个必需的位置参数:“self”``我也尝试了该代码,它工作得很好,没有错误,但什么都没有发生。没有消息被发送,cog正在运行(我在init中放了一个print语句)好吧,你必须使用
    start
    命令来启动它,另一种方法是在
    \uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
    方法中启动它,但是你应该等待bot准备好,然后使用
    bot。等待,直到\uu ready
    完成缓存的加载,让我编辑我的答案,我在sendmessage函数之后添加了“sendmessage.start()”,其缩进级别与@tasks.loop相同。它输出了以下错误:``内部后台任务'sendmessage'中未处理的异常。回溯(最近一次调用):文件“/opt/virtualenvs/python3/lib/python3.8/site packages/discord/ext/tasks/_init__.py”,第101行,在_loopwait self.coro(*args,**kwargs)类型错误:sendmessage()缺少一个必需的位置参数:“self”``我也尝试了这段代码,它工作得很好
    Traceback (most recent call last):
      File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/tasks/__init__.py", line 101, in _loop
        await self.coro(*args, **kwargs)
    TypeError: sendmessage() missing 2 required positional arguments: 'ctx' and 'self'