将 pickle 用于 discord bot 时出错 | discord.py
Error when using pickle for a discord bot | discord.py
当我试图用 pickle 保存我的 reactionrole 对象列表时,它给了我错误 Command raised an exception: TypeError: cannot pickle 'TaskStepMethWrapper' object
。
我没有尝试任何东西,因为我不确定 TaskStepMethWrapper 是什么。这是我的代码:
@client.command()
async def reactionadd(ctx):
# await ctx.send('Please give me the ID of the message you want to have a reactionrole on.')
# msgid_var = await client.wait_for('message')
await ctx.send('Please react with the emoji you want the reactionrole to use.')
emoji_var = await client.wait_for('reaction_add')
# await ctx.send('Please give me the ID of the role you want the reactionrole to give.')
# roleid_var = await client.wait_for('message')
if not os.path.isfile('reactionrole.obj'):
rrf = open('reactionrole.obj', 'xb')
rrf.close()
rrf = open('reactionrole.obj', 'rb+')
if os.stat('reactionrole.obj').st_size == 0:
rrobj = []
else:
rrobj = pickle.load(rrf)
emoji_var = emoji_var[0]
rrobj.append(reactionrole(749316751212150965, emoji_var, 749317419255857232))
pickle.dump(rrobj, rrf)
rrf.close()
class reactionrole:
def __init__(self, msgid, emoji, roleid):
self.msgid = msgid
self.emoji = emoji
self.roleid = roleid
那么,如何解决这个错误?我应该继续 pickle 还是使用其他序列化技术?如果需要,我可以自己写入和解析文本文件。
原来 emoji_var[0] 是一个对象。只需使用 str()
将其转换为字符串即可解决此问题。
当我试图用 pickle 保存我的 reactionrole 对象列表时,它给了我错误 Command raised an exception: TypeError: cannot pickle 'TaskStepMethWrapper' object
。
我没有尝试任何东西,因为我不确定 TaskStepMethWrapper 是什么。这是我的代码:
@client.command()
async def reactionadd(ctx):
# await ctx.send('Please give me the ID of the message you want to have a reactionrole on.')
# msgid_var = await client.wait_for('message')
await ctx.send('Please react with the emoji you want the reactionrole to use.')
emoji_var = await client.wait_for('reaction_add')
# await ctx.send('Please give me the ID of the role you want the reactionrole to give.')
# roleid_var = await client.wait_for('message')
if not os.path.isfile('reactionrole.obj'):
rrf = open('reactionrole.obj', 'xb')
rrf.close()
rrf = open('reactionrole.obj', 'rb+')
if os.stat('reactionrole.obj').st_size == 0:
rrobj = []
else:
rrobj = pickle.load(rrf)
emoji_var = emoji_var[0]
rrobj.append(reactionrole(749316751212150965, emoji_var, 749317419255857232))
pickle.dump(rrobj, rrf)
rrf.close()
class reactionrole:
def __init__(self, msgid, emoji, roleid):
self.msgid = msgid
self.emoji = emoji
self.roleid = roleid
那么,如何解决这个错误?我应该继续 pickle 还是使用其他序列化技术?如果需要,我可以自己写入和解析文本文件。
原来 emoji_var[0] 是一个对象。只需使用 str()
将其转换为字符串即可解决此问题。