【问题标题】:cog task for discord.pydiscord.py 的 cog 任务
【发布时间】:2020-08-30 22:57:36
【问题描述】:

这应该每 15 分钟在频道中发送一条消息。 由于某种原因,它不起作用。它也没有显示任何错误。有人可以帮我解决这个问题吗?

import time

from discord.ext import commands

message = 'choose roles from <#728984187041742888>'
channel_id = 742227160944869437  # the channel id (right click on the channel to get it)
time_spacing = 15*60  # s : 15min


class auto(commands.Cog):
    def __init__(self, bot):
        self.bot = bot


    @commands.Cog.listener()
    async def spamm(self, ctx):
        while True:
            time.sleep(time_spacing)
            await ctx.get_channel(channel_id).send(message)


    @commands.Cog.listener()
    async def on_ready(self):
        print(f'Sp4mm3r {self.bot.user} has connected to Discord!')
        print('Sending message :', message, 'every', time_spacing, 'seconds')


def setup(bot):
    bot.add_cog(auto(bot))

---------修正版------------ 认为问题是我没有开始任务,并且 time.sleep 不应该在那里使用。

from discord.ext import tasks
from discord.ext import commands


time = 30*60


class Automessager(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.message = 'Choose roles from <#728984187041742888>'
        self.channel1 = 725550664561983519
        self.text.start()
  

    @tasks.loop(seconds=time)
    async def text(self):
        try:
            channel = self.bot.get_channel(self.channel1)
            await channel.send(self.message)
        except Exception as e:
            print(e)


    @commands.Cog.listener()
    async def on_ready(self):
        print(f'{self.bot.user} has connected to Discord!')
        print(f'Sending message: {self.message} every {time} seconds')


def setup(bot):
    bot.add_cog(Automessager(bot))

【问题讨论】:

    标签: python discord bots discord.py


    【解决方案1】:

    您的代码中有两个错误:

    • get_channel()commands.Bot 方法,而不是 discord.Context 方法。
    • 如果您使用time.sleep() 等待 15 分钟,它将冻结您的整个 cog。

    要制作循环,您可以使用 task.loop(),而不是使用 cog 侦听器:

    from discord.ext import task
    from discord.ext import commands
    
    
    class auto(commands.Cog):
        def __init__(self, bot):
            self.bot = bot
            self.message = 'Choose roles from <#728984187041742888>'
            self.channel_id = 742227160944869437
            self.minutes = 15
    
        @task.loop(minutes=self.minutes)
        async def spamm(self, ctx):
             channel = self.bot.get_channel(self.channel_id)
             await channel.send(self.message)
    
        @commands.Cog.listener()
        async def on_ready(self):
            print(f'Sp4mm3r {self.bot.user} has connected to Discord!')
            print(f'Sending message: {self.message} every {self.minutes} minutes')
    
    
    def setup(bot):
        bot.add_cog(auto(bot))
    

    PS:我已将channel_idmessageminutes 设置为类变量。你也不需要使用time.sleep()

    【讨论】:

    • 谢谢老兄!我不知道 time.sleep 的正确用法。它仍然无法正常工作。问题是我忘记开始任务了
    猜你喜欢
    • 2021-03-03
    • 2021-04-10
    • 2018-06-10
    • 2021-06-29
    • 2020-07-04
    • 1970-01-01
    • 2021-06-22
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多