pythondiscorddiscord.py

discord.py - making a slash command send a premade poll


I am extremely new to this (started experimenting with it today) and am trying to make a slash command that will create a poll using a poll class i found in documentation but can't figure out how to get it to actually send.

here is my bot code not including some other slash commands and the ending run command

import discord
from discord.ext import commands
from discord import app_commands
from discord import Color as c
from discord import Poll

class Client(commands.Bot):
    async def on_ready(self):
        print(f'Logged on as {self.user}!')
    # sends message when bot is ready

        try:
            guild = discord.Object(id=1009126140633415730)
            synced = await self.tree.sync(guild=guild)
            print(f'Synced {len(synced)} commands to guild {guild.id}')
        # syncs slash commands to server
    
        except Exception as e:
            print(f'Error syncing commands: {e}')
        # prints error message if syncing fails


    async def on_message(self, message):   
        if message.author == self.user:
            return
        # stops bot from replying to itself

        if message.content.startswith('hello'):
            await message.channel.send(f'Hi there {message.author}!')
        # says hi there when user says hello

    async def on_reaction_add(self, reaction, user):
        await reaction.message.channel.send('You reacted')
    # says when you react to message


intents = discord.Intents.default()
intents.message_content = True
client = Client(command_prefix="!", intents=intents)


GUILD_ID = discord.Object(id=1009126140633415730)

@client.tree.command(name="repo", description="Create a poll to see who's free for REPO", guild=GUILD_ID)
async def repo(interaction: discord.Interaction):

    p = discord.Poll(question="Are you free for REPO?", duration=4)
    p.add_answer(text="Yes")
    p.add_answer(text="No")
    
    await interaction.response.send(poll=p)

# /REPO command

My issue is when i try to use the repo slash command in my discord server i get this error

2025-03-24 16:44:56 ERROR    discord.app_commands.tree Ignoring exception in command 'repo'
Traceback (most recent call last):
  File "C:\Users\Dan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\app_commands\commands.py", line 858, in _do_call
    return await self._callback(interaction, **params)  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Discord Bot\example_bot.py", line 93, in repo
    await interaction.response.send(poll=p)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'InteractionResponse' object has no attribute 'send'

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

Traceback (most recent call last):
  File "C:\Users\Dan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\app_commands\tree.py", line 1310, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "C:\Users\Dan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\app_commands\commands.py", line 883, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Dan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\discord\app_commands\commands.py", line 876, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'repo' raised an exception: AttributeError: 'InteractionResponse' object has no attribute 'send'

again I'm very new and am basing the code Im writing off of the code given in the video tutorial I watched in how to set up a discord bot so if a mistake seems obvious I will probably still need it pointed out to me.

also my first time using stack overflow so if you need any more details or if i should lay out anything differently please let me know

Fixed Code:

import time
import datetime

@client.tree.command(name="repo", description="Create a poll to see who's free for REPO", guild=GUILD_ID)
async def repo(interaction: discord.Interaction):

    p = discord.Poll(question="Are you free for REPO?", duration=datetime.timedelta(hours=4))
    p.add_answer(text="Yes")
    p.add_answer(text="No")
    
    await interaction.response.send_message(poll=p)

Those who commented to use send_message were actually right, my other problem was that I needed to import and use delta time specifically for the duration to work. Thanks to everyone who replied!


Solution

  • The answer was to replace send with send_message and to use datetime.timedelta for the duration of the poll.

    import datetime
    
    @client.tree.command(name="repo", description="Create a poll to see who's free for REPO", guild=GUILD_ID)
    async def repo(interaction: discord.Interaction):
        p = discord.Poll(question="Are you free for REPO?", duration=datetime.timedelta(hours=4))
        p.add_answer(text="Yes")
        p.add_answer(text="No")
        
        await interaction.response.send_message(poll=p)