Search code examples
pythondiscorddiscord.pypycord

How to send a message to a specific channel as part of a slash command


When I receive a slash command with my bot, I send a modal to a user asking for information. All this works, however, as part of that, I would also like to send a message to a specific channel on a specific server (guild) to say that a request has been made. I am having trouble with that second part.

import discord

bot = discord.Bot()
client = discord.Client()

@bot.slash_command(name = "create-trial-request", description = "Create a new trial request from a bbcode template.")
async def trial_request(ctx):
    modal = my_modal(title="Fill this in please")
    await ctx.send_modal(modal)

class my_modal(discord.ui.Modal):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        self.add_item(discord.ui.InputText(label="Some Label Name"))

    async def callback(self, interaction: discord.Interaction):
        
        request_number = request_number_generator()
        
        # here is where I want to send my message to a specific channel.
        # I know the ID of that channel, I just don't know how to send the message there.

        code_snippet = format_to_code(bbcode)
        request_response = "Created request #" + str(request_number)
        await interaction.response.send_message(request_response, ephemeral=True)

I have tried the following (placed where my comments are in the code above):

        channel = client.get_channel(6648250855168XXXXX)
        await channel.send("Created trial request #" + str(request_number))

...but I get: AttributeError: 'NoneType' object has no attribute 'send'

Obviously the bot has access to the channel, and if I write to it as part of the response to the slash command, it successfully writes there, but I can't seem to make it work on its own.

Is there any way to do what I am trying to?

Thanks for any help.


Solution

  • Thanks to @3nws, I now know the answer to this question.

    The issue in my case was that I was using both client and bot and should have only been using one of the two (in this instance, bot, since I am using slash commands.

    There is a usable bot.getchannel command that does what I wanted.

    I hope this helps anyone else with the same issue.