0
votes

So I've tried to do this simple poll function using python for my discord bot:

@command(name="makePoll")
@has_permissions(manage_guild=True)
async def createPoll(self, ctx, question, *options):
    if len(options) > 10:
        await ctx.send("You can only supply a maximum of 10 options.")

    else:
        embed = Embed(title="Poll",
                     description=question,
                     colour=ctx.author.colour,
                     timestamp=datetime.utcnow())
    fields = [("Options", "\n".join([f"{numbers[idx]} {options}" for idx, option in enumerate(options)]), False),
              ("Instructions", "React to case a vote!", False)]

    for name, value, inline in fields:
        embed.add_field(name=name, value=value, inline=inline)

    await ctx.send(embed=embed)

And the problem is that it throws me an error when I try to call the function. The error:Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "makePoll" is not found.

edit: tried to name client.command makepoll but know it gives me this error:Ignoring exception in command makePoll: Traceback (most recent call last): File "F:\first try on python\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped ret = await coro(*args, **kwargs) File "F:/first try on python/for loop/bot1.py", line 40, in createPoll colour=ctx.author.colour, AttributeError: 'str' object has no attribute 'author'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "F:\first try on python\venv\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke await ctx.command.invoke(ctx) File "F:\first try on python\venv\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke await injected(*ctx.args, **ctx.kwargs) File "F:\first try on python\venv\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: AttributeError: 'str' object has no attribute 'author'

1
Can you show your imports?Nurqm
Can you try to do @client.command(name="makePoll") instead of @command(name="makePoll")?Nurqm
import discord, import time, import random, from discord import Embed, from date time, from discord.ext import comands, tasks, from itertools import cycle from discord.ext.comand import has_permissions, commandArin CelMare
@Nurqm used your method but now it throws out a completely different errorArin CelMare
Try commands.command(name='makepool').Nurqm

1 Answers

1
votes

Referring to your edit, it says ‘str’ object does not have ‘author’ attribute.

And the str object is referring the ctx in your function. You don’t have to add the variable self because I don’t think that this command is in a class.

Discord.py sees this, and thinks “self is the context, ctx is a string argument(for the command)…”

The solution is to remove “self”.

So the code is:

@client.command(name="makePoll")
@has_permissions(manage_guild=True)
async def createPoll(ctx, question, *options):
    if len(options) > 10:
        await ctx.send("You can only supply a maximum of 10 options.")

    else:
        embed = Embed(title="Poll",
                     description=question,
                     colour=ctx.author.colour,
                     timestamp=datetime.utcnow())
    fields = [("Options", "\n".join([f"{numbers[idx]} {options}" for idx, option in enumerate(options)]), False),
              ("Instructions", "React to case a vote!", False)]

    for name, value, inline in fields:
        embed.add_field(name=name, value=value, inline=inline)

    await ctx.send(embed=embed)

self is mostly used in classes btw

I also suggest you to set the name of the command same as the function name