使不和谐机器人每隔10秒更改播放状态

2024-09-26 18:09:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使测试不一致机器人的状态每10秒在两条消息之间更改一次。当状态消息更改时,我需要执行脚本的其余部分,但每当我试图使其工作时,就会出现一个错误。我的脚本中有线程,但我不完全确定如何在这种情况下使用它。

@test_bot.event
async def on_ready():
    print('Logged in as')
    print(test_bot.user.name)
    print(test_bot.user.id)
    print('------')
    await change_playing()


@test_bot.event
async def change_playing():
    threading.Timer(10, change_playing).start()
    await test_bot.change_presence(game=discord.Game(name='Currently on ' + str(len(test_bot.servers)) +
                                                          ' servers'))
    threading.Timer(10, change_playing).start()
    await test_bot.change_presence(game=discord.Game(name='Say test.help'))

错误消息如下:

C:\Python\Python36-32\lib\threading.py:1182: RuntimeWarning: coroutine 'change_playing' was never awaited
  self.function(*self.args, **self.kwargs)

Tags: nametestself脚本event消息async状态
4条回答

请参阅:

https://github.com/Rapptz/discord.py/blob/master/examples/background_task.py

import discord
import asyncio
client = discord.Client()
async def my_background_task():
    await client.wait_until_ready()
    counter = 0
    channel = discord.Object(id='channel_id_here')
    while not client.is_closed:
        counter += 1
        await client.send_message(channel, counter)
        await asyncio.sleep(60) # task runs every 60 seconds
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
client.loop.create_task(my_background_task())
client.run('token')

Discord.py具有内置的后台任务功能

请参阅:

https://github.com/Rapptz/discord.py/blob/master/examples/background_task.py

import discord
import asyncio
client = discord.Client()
async def my_background_task():
    await client.wait_until_ready()
    counter = 0
    channel = discord.Object(id='channel_id_here')
    while not client.is_closed:
        counter += 1
        await client.send_message(channel, counter)
        await asyncio.sleep(60) # task runs every 60 seconds
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
client.loop.create_task(my_background_task())
client.run('token')

Discord.py带有内置的后台任务功能

version 1.1.0引入了^{},它旨在使您描述的后台任务更简单,并在存在连接问题时处理重新连接到不一致的潜在复杂逻辑。

下面是使用tasks执行任务的示例:

from discord.ext import commands, tasks
from commands import Bot
from tasks import loop
from asyncio import sleep

bot = Bot("!")

@loop(seconds=10)
async def name_change():
    await bot.change_presence(...)
    await sleep(10)
    await bot.change_presence(...)

name_change.before_loop(bot.wait_until_ready())    
name_change.start()
bot.run("TOKEN")

很不幸,线程和异步不能很好地结合在一起。你需要跳过额外的圈来等待线程内的协程。最简单的解决方案就是不要使用线程。

你要做的是等待一段时间,然后进行一次联程。这可以通过后台任务(example)完成

async def status_task():
    while True:
        await test_bot.change_presence(...)
        await asyncio.sleep(10)
        await test_bot.change_presence(...)
        await asyncio.sleep(10)

@test_bot.event
async def on_ready():
    ...
    bot.loop.create_task(status_task())

不能使用time.sleep(),因为这将阻止机器人的执行。不过,asyncio.sleep()和其他东西一样是一个协程,因此是非阻塞的。

最后,装饰器@client.event应该只用于bot识别为events的函数。例如on_ready和on_消息。

相关问题 更多 >

    热门问题