因此,此后台任务的目标是等待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
之前打开了一个浏览器
最终的结果是,该函数在机器人准备就绪之前运行,只是把一切都搞糟了
一些旁注:
monitorGame()
实质上是打开一个浏览器并解析与其相关的html李>
您可以简单地检查启动selenium部件需要多长时间,然后测量时间并插入一个
time.sleep('seconds')
直到它启动,这可能是本例中最简单的修复方法相关问题 更多 >
编程相关推荐