pythondiscorddiscord.py

cannot access message id beacuse the interaction.response.send_message() returns none with discord.py


@bot.tree.command(name='create_dungeon')
async def create_dungeon(interaction: discord.Interaction):
    new_doc_id = create_document(str(interaction.user))
    if new_doc_id == False:
        await interaction.response.send_message("Can't create more than one dungeon per user!")
    else:
        created_doc_query = maplestory_collection.find_one({"_id": new_doc_id})
        dps_players_count = len(created_doc_query["Dps"])
        tank_players_count = len(created_doc_query["Tank"])
        healer_players_count = len(created_doc_query["Healers"])
        class_types_max_players_and_counts = [
            ("Dps", dps_players_count, 10),
            ("Healer", healer_players_count, 3),
            ("Tank", tank_players_count, 4)
        ]

        embed = discord.Embed(title="Dungeon", description=f'Leader: {interaction.user} - ID {interaction.user.id}')
        for class_type, count, max_players in class_types_max_players_and_counts:
            embed.add_field(name=f"{class_type}", value=f"{max_players}/{count}")
        
        view = MyView(doc_id=new_doc_id, original_message_id=None, creator_id=interaction.user.id)
        view.create_buttons(buttons_info=buttons_info)
        
        message = await interaction.response.send_message(view=view, embed=embed)
        # message returns None even though it should return a Message object

Message var should return message object but returns none? im trying to get the id of the message and then setting it for the view attribute original_message_id but its outputting an error TypeError


Solution

  • I have looked at the documentation of discord.py and have come to the conclusion that your problem is likely due to how interaction.response.send_message() does not return any value, so attempting to assign a variable to its output assigns None.

    I found the above information at The documentation page for InteractionResponse.send_message(), as no return values are listed for the method.

    It may be necessary for you to find another way to achieve your goal that doesn't require the ID of the response, as I cannot find a way to retrieve that ID other than sending it then getting the most recent post in the channel, which may cause problems due to potential messages being sent between the bot's response and getting the message.