Search code examples
pythondiscordpycord

Trouble defining a bot.get_channel() with Pycord when calling from a list - Printing "None"


I'm simply attempting to call this channel ID from a list after it is assigned by the command user. All of the print() commands that I do yield the proper ID including print(a_string) which comes RIGHT before the bot.get_channel call.

I cannot fathom why, but for some reason it consistantly returns "None" no matter how I try to assign the variable.

The part of the code in question is inside the class "EmbedConfirmButton" and the variable in question is "esendChannel", assigned by the slash command at the bottom of the code block.

This is a lot of code, but I wanted to be sure to include the entire thing for reference just in case


eEdit = ["blank"]
esendChannel = ["blank"]
eTitle = ["blank"]
eDescription = ["blank"]
channel = []


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

        #self.title = kwargs.get("title")
        #self.description = kwargs.get("description")

        self.add_item(discord.ui.InputText(label="Title", value = eTitle[0]))
        self.add_item(discord.ui.InputText(label="Message Content", style=discord.InputTextStyle.long, value = eDescription[0])) 

    async def callback(self, interaction: discord.Interaction):
        eTitle[0] = self.children[0].value
        eDescription[0] = self.children[1].value

        esend = discord.Embed(title=''.join(eTitle), description=''.join(eDescription))
        eEdit[0] = [esend]

        print(eEdit)

        await interaction.response.send_message(embed = esend, view = EmbedConfirmButton(), ephemeral=True)
        



class EmbedConfirmButton(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)

    @discord.ui.button(label="Send", custom_id="button-embededit", style=discord.ButtonStyle.blurple) 
    async def first_button_callback(self, button, interaction):
        finalembed = discord.Embed(title=eTitle, description=eDescription)
        finalembed.set_image(url="")
        s = [str(integer) for integer in esendChannel]
        a_string = "".join(s)
        print(a_string)
        await bot.get_channel(a_string).send(embed = finalembed)
        await interaction.response.send_message("test", ephemeral = True)
    
    @discord.ui.button(label="Edit", custom_id="button-embededit2", style=discord.ButtonStyle.blurple) 
    async def second_button_callback(self, button, interaction):
        modal = EmbedSend(title=eTitle[0])
        await interaction.response.send_modal(modal)
        await interaction.followup.send("test")

    @discord.ui.button(label="Cancel", custom_id="button-embedcancel", style=discord.ButtonStyle.blurple) 
    async def third_button_callback(self, button, interaction):
        await interaction.response.send_message("Canceled Interaction", ephemeral = True)
        return


@bot.slash_command()
@option("channel")
async def embedsend(ctx, channel:discord.TextChannel):
    modal = EmbedSend(title = "Embed Create")
    await ctx.send_modal(modal)
    await ctx.respond("Success", ephemeral = True)
    esendChannel[0] = str(channel.id)
    print(esendChannel)

I've tried assigning the variable in several different ways to the point I've completely no clue why it would be returning "None" only when passed to bot.get_channel()


Solution

  • Are you saying that bot.get_channel is returning None when calling it with a_string?

    Have you tried using await bot.fetch_channel(a_string) instead? It's possible that that channel isn't in the cache so get_channel isn't returning anything.