每15分钟发送一次不和谐的消息

时间:2020-10-10 21:52:30

标签: python discord.py

我正在尝试使机器人每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)

1 个答案:

答案 0 :(得分:1)

如果您尝试使用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()