簡體   English   中英

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

[英]How do I use schedule py and Discord py together?

主要目標:每周三 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 的初學者。

我不建議使用調度程序,因為 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() function 以檢查如果您不想等待並在希望運行的那一天運行代碼,是否是運行的正確時間。

但是如果你真的想使用日程安排模塊,這篇文章我幫你: Discord.py 時間表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM