有没有办法将消息发送到公会的系统通道,并在计划任务中使用它?

问题描述

async def rules_reminder(self):
    channel = self.bot.guild.system_channel
    await channel.send("Remember to adhere to the rules!")
    
self.scheduler.add_job(self.rules_reminder,crontrigger(second="0,15,30,45"))

解决方法

您可以为此使用 discord.ext.tasks。 discord.ext.tasks 是一个 discord.py 扩展,允许用户轻松创建重复任务。对于您的代码,这将是 discord.ext.tasks 版本:

from discord.ext import tasks # Somewhere...
# --------------------------------------------
# Preferably somewhere in your cog's __init__...
self.rules_reminder.start()

@tasks.loop(seconds=15)
async def rules_reminder(self):
    channel = self.bot.guild.system_channel
    await channel.send("Remember to adhere to the rules!")

最后一件事,只是为了确保您知道:commands.Bot 或 discord.Client 都没有 guild 属性。我假设你自己设置了公会。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...