后台任务未等待Bot就绪

2024-09-27 00:16:59 发布

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

因此,此后台任务的目标是等待bot就绪,然后执行函数monitorGame()。不幸的是,当我运行bot时,情况并非如此

有问题的代码:

import json
import requests
import discord
from gamestop import monitorGame
from datetime import datetime
from time import sleep
from discord.ext import tasks, commands
from discord.ext.commands import errors
from discord.utils import get
from discord_webhook import DiscordWebhook, DiscordEmbed
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

# Prefix
bot = commands.Bot(command_prefix='/')
bot.remove_command("help")

# Bot Event
@bot.event
async def on_ready():
    print("[+] Bot Is Alive [+]")
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"Game"))

# Start Monitor
@tasks.loop(seconds=1)
async def monitor():

  # Wait Until Bot Is Ready
  await bot.wait_until_ready()

  # Run Function
  monitorGame()

# Start Task
monitor.start()

# Run bot
bot.run(token)

由于函数monitorGame()使用了selenium,所以告诉我该函数在bot准备就绪之前正在运行的是,在bot打印Bot Is Alive之前打开了一个浏览器

最终的结果是,该函数在机器人准备就绪之前运行,只是把一切都搞糟了

一些旁注:

  • 该功能包括selenium并打开浏览器
  • 我正在使用repl运行此bot
  • 函数monitorGame()实质上是打开一个浏览器并解析与其相关的html

Tags: 函数fromimportdatetimeisbotselenium浏览器
1条回答
网友
1楼 · 发布于 2024-09-27 00:16:59

您可以简单地检查启动selenium部件需要多长时间,然后测量时间并插入一个time.sleep('seconds')直到它启动,这可能是本例中最简单的修复方法

相关问题 更多 >

    热门问题