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

2024-06-10 08:07:10 发布

您现在位置:Python中文网/ 问答频道 /正文

主要目标: 每周三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代码的初学者。


Tags: run代码fromimportclientloopasyncioasync
1条回答
网友
1楼 · 发布于 2024-06-10 08:07:10

我不建议使用scheduler,因为discord扩展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()函数,如果您不想等待并在希望运行该函数的当天运行代码,则检查该函数是否适合运行

但是如果您真的想使用schedule模块,请发布我的帮助:Discord.py Time Schedule

相关问题 更多 >