猿问

每 15 分钟发送一条关于 Discord 的消息

我正在尝试制作一个每 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)


慕妹3242003
浏览 75回答 1
1回答

Qyouu

如果您尝试使用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()
随时随地看视频慕课网APP

相关分类

Python
我要回答