微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何同时使用 schedule py 和 Discord py?

如何解决如何同时使用 schedule py 和 Discord py?

主要目标: 每个星期三 08:00

向频道发送消息

这是我当前的代码

import schedule
import time
import discord
import asyncio
from discord.ext import commands,tasks
from discord.ext.tasks import loop

client = discord.Client()

# IMPORTANT
channel = 
bottoken = ""

async def sendloop():
  while True:
    schedule.run_pending()
    await asyncio.sleep(1)

@client.event
async def on_ready():
        general_channel = client.get_channel(channel)
        print('ready')
        schedule.every(2).seconds.do(lambda:loop.create_task(general_channel.send('TEST')))

client.run(bottoken)

到目前为止,没有错误,只是停留在“就绪”状态。我是使用 VS Code 的初学者。

解决方法

我不建议使用调度程序,因为不协调扩展 task,它可以与您尝试在代码中实现的功能相同:

from discord.ext import commands,tasks

ch = id_channel
client = commands.Bot(command_prefix='.')
TOKEN = "Token"

#every week at this time,you can change this to seconds=2 
#to replicate what you have been testing
@tasks.loop(hours=168) 
async def every_wednesday():
    message_channel = client.get_channel(ch)
    await message_channel.send("Your message")

@every_wednesday.before_loop
async def before():
    await client.wait_until_ready()

every_wednesday.start()

client.run(TOKEN)

如果您在希望重复的时间运行此代码,它将完成您想要的操作。另一种方法是检查 before() 函数以检查是否是运行的正确时间,如果您不想等待并在您希望它运行的那一天运行代码。

但如果你真的想使用日程模块,这篇文章我可以帮助你:Discord.py Time Schedule

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。