Интеграция кода в Discord bot
Хочу интегрировать рабочий код в Discord бот, но выходят такие ошибки: (Ignoring exception in on_ready Traceback (most recent call last): File "C:\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(args, kwargs) File "C:\Users}{\Desktop\bot\tes2.py", line 93, in on_ready alertList = pickle.load(infile) AttributeError: Can't get attribute 'Alert' on <module 'main' from 'C:\Users}{\Desktop\bot\tes2.py'>).
По советам пробовал добавить from __future__ import annotations не помогло. Буду рад за помощь!
from yahoo_fin import stock_info as si
from time import sleep
from threading import Thread, Lock
import os
import pickle
import discord
from discord.ext import commands
from config import settings
bot = commands.Bot(command_prefix = settings['prefix'], help_command=None)
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('Bot is ready')
print('--------------------')
channel = bot.get_channel(825351852988170254)
await channel.send(f'Ready')
class Alert:
ticker = "NA"
price = float(0)
way = 1
def __init__(self, stock, p, w):
self.ticker = stock
self.price = p
self.way = w
# Stopping the alert after is has hit the target
def stopAlert(self, alert, Alertlist):
Alertlist.remove(alert)
outfile = open(filename, "wb")
pickle.dump(alertList, outfile)
outfile.close()
async def checkPrice(self, alertlist):
while (True and stop):
for alert in alertlist:
if (alert.way == str(1)):
if (float("%.2f" % ((si.get_live_price(alert.ticker))))) >= float(alert.price):
await channel.send('test1')
Alert.stopAlert(Alert, alert, alertlist)
elif (alert.way == str(2)):
if ((float("%.2f" % ((si.get_live_price(alert.ticker))))) <= float(alert.price)):
await channel.send('test2')
Alert.stopAlert(Alert, alert, alertlist)
async def createAlert(self, alertList):
stock = input("\n What is the ticker symbol of the stock?\n :: ")
price = -1.0
while (price < 0.0):
try:
price = float(input(" What is the price you want to hit?\n :: "))
if (price < 0):
await channel.send('Invalid price!')
except ValueError:
await channel.send('Invalid input!')
try:
("%.2f" % ((si.get_live_price(stock))))
alert = Alert(stock, price, way)
alertList.append(alert)
outfile = open(filename, "wb")
pickle.dump(alertList, outfile)
outfile.close()
await channel.send("Alert created successfully!")
except AssertionError:
await channel.send(" " + "-" * 21)
await channel.send(" | Stock doesn't exist |")
await channel.send(" " + "-" * 21)
async def printAlert(self, alert):
await channel.send("Ticker: {} to hit price: {}".format(alert.ticker.upper(), alert.price))
filename = "alerts"
try:
infile = open(filename, "xb")
except FileExistsError:
infile = open(filename, "rb")
try:
infile = open(filename, "rb")
alertList = pickle.load(infile)
infile.close()
except EOFError:
alertList = list()
stop = True
t = Thread(target=Alert.checkPrice, args=(Alert, alertList,))
t.start()
@bot.command()
async def above(ctx, *, arg):
pass
bot.run(settings['token'])