python - Discord.py 时间表
问题描述
这就是我到目前为止所拥有的......对于我想要的延迟秒数确实有效,但是如何添加时间模块或shedule模块以使其工作..以防万一我希望机器人每次发送消息24 小时
import discord
import asyncio
from discord.ext import commands
import schedule
import time
TOKEN = 'xxxxx'
client = commands.Bot(command_prefix = '.')
channel_id = '515994xxxxx5036697'
@client.event
async def on_ready():
print('Bot Online.')
async def alarm_message():
await client.wait_until_ready()
while not client.is_closed:
channel = client.get_channel(channel_id)
messages = ('test')
await client.send_message(channel, messages)
await asyncio.sleep(5) #runs every 5 seconds
client.loop.create_task(alarm_message())
client.run(TOKEN)
解决方案
您可以使用discord.ext.tasks
.
import discord
import asyncio
from discord.ext import commands
from discord.ext import tasks
import time
TOKEN = 'xxxxx'
client = commands.Bot(command_prefix = '.')
channel_id = '515994xxxxx5036697'
@client.event
async def on_ready():
print('Bot Online.')
@tasks.loop(days=1)
async def alarm_message():
await client.wait_until_ready()
channel = client.get_channel(channel_id)
message = 'test'
await channel.send(message)
alarm_message.start()
client.run(TOKEN)
推荐阅读
- c++ - 如何在没有 MATLAB API 帮助的情况下在 C++ 中读取 mat 文件
- javascript - Material UI 手风琴在折叠或展开时会导致页面闪烁
- spring-boot - 单元测试时如何模拟 ElasticsearchOperations (spring-data-elasticsearch v.4.0.3)
- django - 如何将 django 中的变量导入 vue.js 实例
- javascript - Javascript 日期在使用数字日期时返回指定日期前一个月
- c# - 在没有实现 INotifyPropertyChanged 的情况下更新绑定?
- java - Android 将 AndroidX 库添加到非 android 项目
- python - 在 python 上将 select psql 语句存储到 csv 文件时出错
- python - 如何从另一个脚本获取返回输出?
- autodesk-forge - 如何在 Autodesk Forge Viewer 中更改 ViewCubeUI 的文本?