[英]Python pickle discord.py
我想将代码集成到机器人的 discord 中。 该代码有效,但是当我制作机器人时,会出现此类错误(忽略 on_ready Traceback 中的异常(最后一次调用):文件“C:\Python39\lib\site-packages\discord\client.py”,第 343 行,在 _run_event 等待 coro( args, kwargs) 文件“C:\Users}{\Desktop\bot\tes2.py”,第 93 行,on_ready alertList = pickle.load(infile) AttributeError: Can't get attribute 'Alert ' 在 <module ' main ' 上来自 'C:\Users\}{\Desktop\bot\tes2.py'> )。 请告诉我如何修复它们?
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'])
该错误是因为在定义 class 时,它的名称仍然不可用。 有两种可能的解决方案:
使用self.anything
代替 class 名称
在代码开头粘贴from __future__ import annotations
。 它应该允许你做你已经在做的事情而不会引发任何错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.