每15分钟发送一条关于不和的信息



我正在尝试制作一个机器人,每15分钟发送一条随机消息。机器人没有加载任何问题,但没有发送消息。我是不是错过了什么?

import discord
import asyncio
import random
from discord.ext import commands, tasks
client = discord.Client()
token = 'xxx'
@tasks.loop(seconds=5)
async def background_loop():
await client.wait_until_ready()
while not client.is_closed:
channel = client.get_channel(xxx)
messages = ["Hello!", "How are you doing?", "Howdy!"]
await channel.send(random.choice(messages))
background_loop.start()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(token)

如果您试图使用tasks,那么您使用它有点错误。以下是关于如何使用tasks的文档。此外,没有什么能比得上client.send_message。你可以只做channel.send(message)

@tasks.loop(minutes=15.0)
async def background_loop():
await client.wait_until_ready()
while not client.is_closed:
channel = client.get_channel(id)
messages = ["Hello!", "How are you doing?", "Howdy!"]
await channel.send(random.choice(messages))
background_loop.start()

最新更新