无法从变量中提取嵌入信息
Embed information cannot be pulled out from a variable
我是一名菜鸟程序员学生,正在寻找这段代码的解决方案。
我确定解决方案比我想象的要容易,但似乎我找不到问题所在。
“for name, value in polls: embed.add_field(name=name, value=value)” 这行代码似乎无法取出我的 polls 变量的信息。
如果我取出 name... 代码行,则该功能有效但嵌入缺少信息。
如果我把这个用于名称、值等...代码行,那么 poll 函数甚至不会执行。
感谢您的帮助
此致
查理
@commands.command()
@commands.has_permissions(administrator=True)
async def poll (self, ctx, time: int, vote: int, title, *options):
polls = [('\u200b'), '\n'.join([f'{self.emoji[index]} {options} \n' for index, option in enumerate(options)])]
embed = discord.Embed(title=title,
description=f':stopwatch: Poll will end in **{time} minute**!',
colour=0xF0000)
embed.set_thumbnail(url=f'https://cdn.discordapp.com/icons/{ctx.message.guild.id}/{ctx.message.guild.icon}.png')
for name, value in polls:
embed.add_field(name=name, value=value)
message = await ctx.channel.send(embed=embed)
for item in self.emoji[:len(options)]:
await message.add_reaction(item)
如果您添加错误消息会很有帮助,但我猜您得到的是类似 ValueError: not enough values to unpack (expected 2, got 1)
的内容。理由如下。您的 polls
变量仅包含两个字符串。当你在做类似
的事情时
for a, b in polls:
pass
,在第一次迭代中,python 获取 polls
的第一个元素并尝试将其解包。这是一个有效的示例,因为可以将元组解压缩为两个变量:
polls = [('a', 'b'), ('c', 'd')]
for i, j in polls:
print(i, j)
但在您的情况下,您试图将 '\u200b'
字符串解压缩为两个变量,但这是行不通的。如果你想将 polls
解压为 name
和 value
,你可以这样做:name, value = polls
。但是因为真的没有什么可以循环的,所以我不确定你想做什么。
您的代码可以在某些方面进行调整。
self.emoji[index]
应该给出什么?
我知道你在这里尝试什么,但你必须记住,你想计算你在投票中给出的每一个“选择”。
嵌入的thumbnail
可以更简单的设置。 只需使用以下内容:
embed.set_thumbnail(url=ctx.guild.icon)
现在让我们开始魔术吧。
for name, value in polls:
embed.add_field(name=name, value=value)
如果我正确理解您的代码,就不会带您到任何与 polls = [(...)]
相关的地方。我们可以只使用 time
参数并自己添加一个参数。
看看下面的代码:
def to_emoji(c):
base = 0x1f1e6 # Our base unicode emoji
return chr(base + c)
# Your Class here
@commands.command()
@commands.has_permissions(administrator=True)
async def poll(self, ctx, time: int, *questions_and_choices: str):
embed = discord.Embed(title=questions_and_choices[0],
colour=0xF0000)
# The first argument will be the title
embed.set_thumbnail(url=ctx.guild.icon) # Get the guild icon
if len(questions_and_choices) < 3: # Check that at least two choices are given
return await ctx.send('**Please at least give one question and two choices**')
choices = [(to_emoji(e), v) for e, v in enumerate(questions_and_choices[1:])]
# Get the emojis that need to be added from our 'def'
body = "\n".join(f"{key}: {c}" for key, c in choices) # Get the choices and "join" them into the embed with the specific key
embed.description = f':stopwatch: Poll will end in **{time} minute**!\n\n {body}'
poll = await ctx.send(embed=embed)
for emoji, _ in choices:
await poll.add_reaction(emoji) # Add the reactions
我们做了什么
- 我们构建了一个函数,returns 表情符号和下一个最高的
- 枚举我们给定的选择
- 为我们的选择分配表情符号
示例截图:
如果您想使用数字而不是字符,只需在我们的函数中更改即可。
我是一名菜鸟程序员学生,正在寻找这段代码的解决方案。 我确定解决方案比我想象的要容易,但似乎我找不到问题所在。 “for name, value in polls: embed.add_field(name=name, value=value)” 这行代码似乎无法取出我的 polls 变量的信息。 如果我取出 name... 代码行,则该功能有效但嵌入缺少信息。 如果我把这个用于名称、值等...代码行,那么 poll 函数甚至不会执行。 感谢您的帮助
此致
查理
@commands.command()
@commands.has_permissions(administrator=True)
async def poll (self, ctx, time: int, vote: int, title, *options):
polls = [('\u200b'), '\n'.join([f'{self.emoji[index]} {options} \n' for index, option in enumerate(options)])]
embed = discord.Embed(title=title,
description=f':stopwatch: Poll will end in **{time} minute**!',
colour=0xF0000)
embed.set_thumbnail(url=f'https://cdn.discordapp.com/icons/{ctx.message.guild.id}/{ctx.message.guild.icon}.png')
for name, value in polls:
embed.add_field(name=name, value=value)
message = await ctx.channel.send(embed=embed)
for item in self.emoji[:len(options)]:
await message.add_reaction(item)
如果您添加错误消息会很有帮助,但我猜您得到的是类似 ValueError: not enough values to unpack (expected 2, got 1)
的内容。理由如下。您的 polls
变量仅包含两个字符串。当你在做类似
for a, b in polls:
pass
,在第一次迭代中,python 获取 polls
的第一个元素并尝试将其解包。这是一个有效的示例,因为可以将元组解压缩为两个变量:
polls = [('a', 'b'), ('c', 'd')]
for i, j in polls:
print(i, j)
但在您的情况下,您试图将 '\u200b'
字符串解压缩为两个变量,但这是行不通的。如果你想将 polls
解压为 name
和 value
,你可以这样做:name, value = polls
。但是因为真的没有什么可以循环的,所以我不确定你想做什么。
您的代码可以在某些方面进行调整。
self.emoji[index]
应该给出什么?
我知道你在这里尝试什么,但你必须记住,你想计算你在投票中给出的每一个“选择”。
嵌入的thumbnail
可以更简单的设置。 只需使用以下内容:
embed.set_thumbnail(url=ctx.guild.icon)
现在让我们开始魔术吧。
for name, value in polls:
embed.add_field(name=name, value=value)
如果我正确理解您的代码,就不会带您到任何与 polls = [(...)]
相关的地方。我们可以只使用 time
参数并自己添加一个参数。
看看下面的代码:
def to_emoji(c):
base = 0x1f1e6 # Our base unicode emoji
return chr(base + c)
# Your Class here
@commands.command()
@commands.has_permissions(administrator=True)
async def poll(self, ctx, time: int, *questions_and_choices: str):
embed = discord.Embed(title=questions_and_choices[0],
colour=0xF0000)
# The first argument will be the title
embed.set_thumbnail(url=ctx.guild.icon) # Get the guild icon
if len(questions_and_choices) < 3: # Check that at least two choices are given
return await ctx.send('**Please at least give one question and two choices**')
choices = [(to_emoji(e), v) for e, v in enumerate(questions_and_choices[1:])]
# Get the emojis that need to be added from our 'def'
body = "\n".join(f"{key}: {c}" for key, c in choices) # Get the choices and "join" them into the embed with the specific key
embed.description = f':stopwatch: Poll will end in **{time} minute**!\n\n {body}'
poll = await ctx.send(embed=embed)
for emoji, _ in choices:
await poll.add_reaction(emoji) # Add the reactions
我们做了什么
- 我们构建了一个函数,returns 表情符号和下一个最高的
- 枚举我们给定的选择
- 为我们的选择分配表情符号
示例截图:
如果您想使用数字而不是字符,只需在我们的函数中更改即可。