Используя Discord.py-rewrite, Как мы можем диагностировать my_background_task
, чтобы найти причину, по которой его оператор печати не печатает каждые 3 секунды?
подробности:
Проблема, которую я наблюдаю, состоит в том, что «1008 *» напечатано один раз в моих журналах, но не ожидаемое «каждые три секунды». Может ли быть где-то исключение, которое я не ловлю?
Примечание: я делаю см. print(f'Logged in as {bot.user.name} - {bot.user.id}')
в журналах, поэтому on_ready
, кажется, работает, так что метод не может быть виноват.
Я попробовал следующий пример: https://github.com/Rapptz/discord.py/blob/async/examples/background_task.py
однако я не использовал его оператор client = discord.Client()
, потому что я думаю, что могу добиться того же, используя "бот", аналогичный описанному здесь https://stackoverflow.com/a/53136140/6200445
import asyncio
import discord
from discord.ext import commands
token = open("token.txt", "r").read()
def get_prefix(client, message):
prefixes = ['=', '==']
if not message.guild:
prefixes = ['=='] # Only allow '==' as a prefix when in DMs, this is optional
# Allow users to @mention the bot instead of using a prefix when using a command. Also optional
# Do `return prefixes` if u don't want to allow mentions instead of prefix.
return commands.when_mentioned_or(*prefixes)(client, message)
bot = commands.Bot( # Create a new bot
command_prefix=get_prefix, # Set the prefix
description='A bot for doing cool things. Commands list:', # description for the bot
case_insensitive=True # Make the commands case insensitive
)
# case_insensitive=True is used as the commands are case sensitive by default
cogs = ['cogs.basic','cogs.embed']
@bot.event
async def on_ready(): # Do this when the bot is logged in
print(f'Logged in as {bot.user.name} - {bot.user.id}') # Print the name and ID of the bot logged in.
for cog in cogs:
bot.load_extension(cog)
return
async def my_background_task():
await bot.wait_until_ready()
print('inside loop') # This prints one time. How to make it print every 3 seconds?
counter = 0
while not bot.is_closed:
counter += 1
await bot.send_message(channel, counter)
await channel.send(counter)
await asyncio.sleep(3) # task runs every 3 seconds
bot.loop.create_task(my_background_task())
bot.run(token)
[]