【问题标题】:Make a cog that sends a message every 10 seconds in a specified chat discord.py制作一个在指定聊天 discord.py 中每 10 秒发送一次消息的 cog
【发布时间】:2021-04-27 15:30:23
【问题描述】:
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))

到目前为止,这是我的代码。当我运行它时,我得到了这个错误:

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'

我做错了什么?

【问题讨论】:

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


    【解决方案1】:
    1. self 总是作为第一个参数,而不是第二个
    async def sendmessage(self, ctx):
    
    1. 你需要在启动任务时传递ctx参数(或者根本不传递,不需要)

    2. 没有self.get_channel 这样的东西(请记住,您不是从discord.Clientcommands.Bot 继承的),而是self.client.get_channel

    channel = self.client.get_channel(...)
    
    1. 您需要在函数或命令中启动任务
    @commands.command()
    async def start(self, ctx):
        self.sendmessage.start() # Pass the `ctx` parameter accordingly
    

    编辑:在__init__ 方法中启动循环

    def __init__(self, client):
        self.client = client
        self.sendmessages.start()
    

    注意:您应该添加等待,直到机器人准备好加载内部缓存,其中一种方法是使用装饰器 {task}.before_loop 并使用 Bot.wait_until_ready

    @sendmessage.before_loop
    async def before_sendmessage(self):
        await self.client.wait_until_ready()
    

    您的完整固定代码

    from discord.ext import commands, tasks
    
    
    class sendmessage10seconds(commands.Cog):
        def __init__(self, client):
            self.client = client
            self.sendmessage.start()
    
    
        @tasks.loop(seconds=10)
        async def sendmessage(self): # You can pass the `ctx` parameter if you want, but there's no point in doing that
            channel = self.client.get_channel(802273252973477960)
            await channel.send("Hi")
    
    
        @sendmessage.before_loop
        async def before_sendmessage(self):
            await self.client.wait_until_ready()
    
    
    def setup(client):
        client.add_cog(sendmessage10seconds(client))
    

    【讨论】:

    • 没有错误但什么也没有发生。没有消息被发送,但是 cog 正在运行(我在 init 中放置了一个打印语句)
    • 好吧,你必须使用start 命令才能启动它
    • 另一种方法是在 __init__ 方法中启动它,但是你应该等到机器人准备好,使用 bot.wait_until_ready 所以缓存已完全加载,让我编辑我的答案
    • 我在 sendmessage 函数之后添加了“sendmessage.start()”,缩进级别与@tasks.loop 相同。它输出了这个错误:```内部后台任务'sendmessage'中的未处理异常。回溯(最后一次调用):文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/tasks/__init__.py”,第 101 行,在 _loop await self.coro(*args , **kwargs) 类型错误:sendmessage() 缺少 1 个必需的位置参数:'self' ```
    • 我也尝试了代码,它完美运行fine
    猜你喜欢
    • 2021-10-04
    • 2020-10-15
    • 2022-01-25
    • 1970-01-01
    • 2021-02-19
    • 2022-06-20
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多