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

Question:

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.

Asked By: Allexio

||

Answers:

First of all: Don’t create 2 bot/client instances. Just use commands.Bot once. That’s the reason you made a mistake. You used client instead of bot. Replace it and it should work.

Otherwise, if it would be still None, here are some possible reasons why:

• Not subscribed to the relevant intent.
• Wrong key
 ◦ Remember, IDs are ints, not strings
 ◦ If you’re trying to copy an emoji ID, right-clicking the emoji in a message will copy message ID
• Bot not logged in, and trying to grab objects from cache
 ◦ Subvairant, using two client objects.
• Bot cannot "see" the object.
 ◦ It has to be on the server, share a server with the member, etc
 ◦ If you’re sharded on separate processes, each process will only have objects for that shard.
• Objects retuned by fetch_x will not have a populated cache.

It doesn’t make a huge difference if you do bot.get_channel() or bot.get_guild().get_channel(). You can use both.

Answered By: puncher

To do so, 1. import commands aka from discord.ext import commands

Then remove your bot = discord.Bot and edit client do it is client = commands.Bot()

thats what i do

Answered By: dpy

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.

Answered By: Allexio
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.