【发布时间】:2020-04-14 01:34:49
【问题描述】:
我正在尝试创建一个简单的 Discord 机器人,它每小时向频道发送一条消息,当我在终端中对其进行测试时,它工作正常,每 2 秒打印一次“测试”。但是当我想添加 'await bot.channel.send('here')' 行时,它只会在终端中打印一次'test',而在不和谐频道中什么也没有
import discord,asyncio,os
from discord.ext import commands, tasks
token = 'xxx'
bot = commands.Bot(command_prefix='.')
@bot.event
async def on_ready():
change_status.start()
print('bot in active')
@tasks.loop(seconds=2)
async def change_status():
channel = bot.get_channel = xxx
await bot.change_presence(activity=discord.Game('online'))
print('test')
await bot.channel.send('here')
bot.run(token)
【问题讨论】:
-
你试过在
@tasks.loop外面跑一次change_status吗? -
是的,它只会打印一次'test',我需要它,因为每小时重复一次这个功能。
-
嗯,也许你错过了
change_status.start()? -
我应该在哪里添加?
-
假设
channel是指你想发送消息的通道,你应该使用那个对象:await channel.send(...)
标签: python discord discord.py discord.py-rewrite