我制造了一个不和谐的机器人,但它给了我一个TypeError:难以理解的类型:“列表”错误,我试图用很多方法修复它,但仍然没有工作,你介意帮我吗?
代码:
import discord
from discord.ext import commands
import random
intents = discord.Intents.all()
discord.members = True
intents.members = True
client = commands.Bot(command_prefix="!", intents=intents)
@client.command()
async def highLow(ctx: commands.Context):
AceCard = [1]
TwoCard = [2]
ThreeCard = [3]
FourCard = [4]
FiveCard = [5]
SixCard = [6]
SevenCard = [7]
EightCard = [8]
NineCard = [9]
TenCard = [10]
JackCard = [11]
QueenCard = [12]
KingCard = [13]
randomCard = random.choice({AceCard}, {TwoCard}, {ThreeCard}, {FourCard}, {FiveCard}, {SixCard}, {SevenCard}, {EightCard}, {NineCard}, {TenCard}, {JackCard}, {QueenCard}, {KingCard})
embed = discord.Embed(title="Welcome to HighLow", description=f"You have {randomCard}", colour=0x87CEEB)
embed.set_author(name="Anwais#6857")
embed.add_field(name="Higher", value="React 1 for higher", inline=False)
embed.add_field(name="Lower", value="React 2 for lower", inline=True)
global one
one = client.get_emoji(946853495628238878)
global two
two = client.get_emoji(946853495716327504)
messageBeforeCard1 = await ctx.channel.send(embed=embed)
await messageBeforeCard1.add_reaction(one)
await messageBeforeCard1.add_reaction(two)
print("a")
@client.event
async def on_reaction_add(reaction, user, ctx: commands.Context):
one= 1
HigherLowerId = 0
oneReaction = 0
twoReaction = 0
if reaction.emoji.id == one:
if user.id == HigherLowerId :
await ctx.send(f"You chose {one}")
else:
await reaction.message.channel.send(f"This is not your game, {user.mention}")
elif reaction.emoji.id != oneReaction:
pass 下面是Fulltrace:
PS C:\Users\walid\Downloads\Main> c:; cd 'c:\Users\walid\Downloads\Main'; & 'C:\Users\walid\AppData\Local\Programs\Python\Python39\python.exe' 'c:\Users\walid\.vscode\extensions\ms-python.python-2022.3.10741003\pythonFiles\lib\python\debugpy\launcher' '51596' '--' 'c:\Users\walid\Downloads\Main\HighLowCardGame.py'
Ignoring exception in command highLow:
Traceback (most recent call last):
File "C:\Users\walid\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\walid\Downloads\Main\HighLowCardGame.py", line 25, in highLow
randomCard = random.choice({AceCard}, {TwoCard}, {ThreeCard}, {FourCard}, {FiveCard}, {SixCard}, {SevenCard}, {EightCard}, {NineCard}, {TenCard}, {JackCard}, {QueenCard}, {KingCard})
TypeError: unhashable type: 'list'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\walid\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke await ctx.command.invoke(ctx)
File "C:\Users\walid\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\walid\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: unhashable type: 'list'⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
发布于 2022-03-16 16:24:45
这个错误是因为您不能在一个list中拥有一个set。你在做random.choice({[1]}, {[2]})等等。
此外,您还可以检查文档。random.choice只接收一个序列(一个参数),而不是多个序列。即:random.choice([1, 2, 3, 4])
您应该定义一个列表并将其提供给random.choice,或者更好的选择是使用random.randint(1,13)。这将从1到13随机挑选一个。
https://stackoverflow.com/questions/71498995
复制相似问题