Discord.py Giveaway赢家提到不工作



我正在使用齿轮(我是新的齿轮)来创建一个赠品系统。当然,赠品系统需要机器人创建一个反应的帖子,然后它可以根据那些谁点击的反应来确定赢家。一旦赠品结束,我收到一个错误。

这是我使用的代码:

@commands.command()
@commands.has_permissions(administrator=True)
async def giveaway(self, ctx: commands.Context):
await ctx.send("Let's start with this giveaway! Answer these questions within 15 seconds!")
questions = ["Which channel should it be hosted in?", 
"What should be the duration of the giveaway? (s|m|h|d)",
"What is the prize of the giveaway?"]
answers = []
def check(m):
return m.author == ctx.author and m.channel == ctx.channel 
for i in questions:
await ctx.send(i)
try:
msg = await self.bot.wait_for('message', timeout=15.0, check=check)
except asyncio.TimeoutError:
await ctx.send('You didn't answer in time, please be quicker next time!')
return
else:
answers.append(msg.content)
try:
c_id = int(answers[0][2:-1])
except:
await ctx.send(f"You didn't mention a channel properly. Do it like this {ctx.channel.mention} next time.")
return
channel = self.bot.get_channel(c_id)
time = convert(answers[1])
if time == -1:
await ctx.send(f"You didn't answer the time with a proper unit. Use (s|m|h|d) next time!")
return
elif time == -2:
await ctx.send(f"The time must be an integer. Please enter an integer next time")
return            
prize = answers[2]
await ctx.send(f"The Giveaway will be in {channel.mention} and will last {answers[1]}!")
embed = discord.Embed(title = "Giveaway!", description = f"{prize}", color = ctx.author.color)
embed.add_field(name = "Hosted by:", value = ctx.author.mention)
embed.set_footer(text = f"Ends {answers[1]} from now!")
my_msg = await channel.send(embed = embed)
await my_msg.add_reaction("🎉")
await asyncio.sleep(time)
new_msg = await channel.fetch_message(my_msg.id)
users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(self.bot.user))
winner = random.choice(users)
await channel.send(f"Congratulations! {winner.mention} won {prize}!")
def convert(time):
pos = ["s","m","h","d"]
time_dict = {"s" : 1, "m" : 60, "h" : 3600 , "d" : 3600*24}
unit = time[-1]
if unit not in pos:
return -1
try:
val = int(time[:-1])
except:
return -2

return val * time_dict[unit]

我得到的错误是:

Traceback (most recent call last):
File "C:UsersHumanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdiscordextcommandscore.py", line 229, in wrapped
ret = await coro(*args, **kwargs)
File "c:UsersHumanDocumentsTestBotcogsgiveaways.py", line 78, in giveaway
users = await new_msg.reactions[0].users().flatten()
AttributeError: 'async_generator' object has no attribute 'flatten'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersHumanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdiscordextcommandsbot.py", line 1349, in invoke
await ctx.command.invoke(ctx)
File "C:UsersHumanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdiscordextcommandscore.py", line 1023, in invoke
await injected(*ctx.args, **ctx.kwargs)  # type: ignore
File "C:UsersHumanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdiscordextcommandscore.py", line 238, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'async_generator' object has no attribute 'flatten'
坦率地说,我不知道发生了什么事。它应该提到赢得赠品的用户。希望有人能弄清楚为什么我得到一个错误。谢谢。

您可能使用的是与旧版本在某些方面工作方式不同的Discord.py 2.0+。文档中有一些示例,说明迁移时需要做哪些更改- https://discordpy.readthedocs.io/en/stable/migrating.html#moving-away-from-custom-asynciterator

特别地,AsyncIterator被改变了,所以,正如它在文档中所说,没有更多的flatten()属性。

# before
users = await reaction.users().flatten()
# after
users = [user async for user in reaction.users()]

在你的例子中,你有这样的旧代码:

users = await new_msg.reactions[0].users().flatten()
在上面的示例之后,它将被更改为:
users = [user async for user in new_msg.reactions[0].users()]

最新更新