Reputation: 15
I'm a rookie programmer student who is looking for a solution in this code. I'm sure the solution is easier than I think, but it seems like I cannot find the problem. "for name, value in polls: embed.add_field(name=name, value=value)" line of code seems like it cannot take out the information of my polls variable. If i take out for name... line of code, then the function works but the embed has missing information. If I put back this for name, value etc... line of code then the poll function wont even execute. Thank you for your help
Sincerely
Charlie
@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)
Upvotes: 1
Views: 65
Reputation: 3592
Your code can be adapted in some ways.
What is self.emoji[index]
supposed to give out?
I see what you are trying here, but you have to keep in mind that you want to count up with every "choice" you give in the poll.
The thumbnail
of the embed can be set in an easier way. Simply use the following:
embed.set_thumbnail(url=ctx.guild.icon)
Now let's begin with the magic.
for name, value in polls:
embed.add_field(name=name, value=value)
Will not get you anywhere in connection with polls = [(...)]
if I understood your code correctly. We can just use the time
argument and add one argument ourselves.
Have a look at the following code:
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
What we did
Example screenshot:
If you want to use numbers instead of characters, just change it in our function.
Upvotes: 1
Reputation: 1337
It would be helpful if you added the error message, but I'm guessing you're getting something like ValueError: not enough values to unpack (expected 2, got 1)
. The reason is the following. Your polls
variable contains only two strings. When you're doing something like
for a, b in polls:
pass
, in the first iteration python is taking the first element of polls
and tries to unpack it. Here's an example that works because the tuple can be unpacked into two variables:
polls = [('a', 'b'), ('c', 'd')]
for i, j in polls:
print(i, j)
But in your case, you're trying to unpack the '\u200b'
string into two variables, which doesn't work. If you want to unpack polls
into name
and value
you can do it like this: name, value = polls
. But because there's really nothing to loop over, I'm not sure what you were trying to do.
Upvotes: 1