pythondiscord.py

bot.get.channel returns None


I have been trying to create a ticket system, I made it work via the buttons, create a channel, post a message in a created channel, but the problem is that it doesnt post info about a created ticket in the admins channel. Here is the code:

import discord
from discord.ext import commands
from discord.utils import get

bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())

class CombinedTicketButtonsView(discord.ui.View):
    def __init__(self):
        super().__init__()
        self.reportbutton()
        self.verify()

    def reportbutton(self):
        reportbutton = discord.ui.Button(label="Report")
        self.add_item(reportbutton)

        async def report_buttoncallback(interaction: discord.Interaction):
            from ticket_number import ticket_number  # Assuming this is a module managing ticket numbers
            guild = interaction.guild
            user = interaction.user
            admin = get(guild.roles, name="Admin")
            ticket_number += 1  # Increment ticket number
            channel_name = f'Ticket-{ticket_number}'

            # Set up permissions for the channel
            overwrites = {
                guild.default_role: discord.PermissionOverwrite(read_messages=False),
                user: discord.PermissionOverwrite(read_messages=True),
                admin: discord.PermissionOverwrite(read_messages=True)
            }

            # Create the channel
            channel = await guild.create_text_channel(channel_name, overwrites=overwrites)
            message = f'''## Hello {user.mention}.
            
Please fill this report template:
1. (Who has violated the rule.)
2. (In which channel/thread the rule was violated.)
3. (Proof of a violation, preferably a screenshot.)

An admin will assist you shortly.
Thank you for helping us moderate the server.'''
            await channel.send(message)

            # Notify the admin channel
            embed = discord.Embed(
                colour=discord.Color.yellow(),
                title="A ticket has been created",
                description=(
                    f"**Reason**: Report\n"
                    f"**Link**: [Ticket Channel](https://discord.com/channels/{guild.id}/{channel.id})\n"
                    f"**Created by**: {user.mention}\n"
                    "*Response required*."
                )
            )
            admin_msg_channel = bot.get_channel(1275428501016547338)
            print(admin_msg_channel)  # For debugging

            if admin_msg_channel is not None:
                await admin_msg_channel.send(embed=embed)
            else:
                print("Admin message channel not found!")

        reportbutton.callback = report_buttoncallback

    def verify(self):
        verifybutton = discord.ui.Button(label="Verify")
        self.add_item(verifybutton)

        async def verify_buttoncallback(interaction: discord.Interaction):
            from ticket_number import ticket_number  # Assuming this is a module managing ticket numbers
            guild = interaction.guild
            user = interaction.user
            admin = get(guild.roles, name="Admin")
            ticket_number += 1  # Increment ticket number
            channel_name = f'Ticket-{ticket_number}'

            # Set up permissions for the channel
            overwrites = {
                guild.default_role: discord.PermissionOverwrite(read_messages=False),
                user: discord.PermissionOverwrite(read_messages=True),
                admin: discord.PermissionOverwrite(read_messages=True)
            }

            # Create the channel
            channel = await guild.create_text_channel(channel_name, overwrites=overwrites)
            message = f'''## Hello {user.mention}.

In order to get verified, please send a link to the post of your level.
An admin will assist you shortly.'''
            await channel.send(message)

            # Notify the admin channel
            admin_msg_channel = bot.get_channel(1275428501016547338)
            embed = discord.Embed(
                colour=discord.Color.yellow(),
                title="A ticket has been created",
                description=(
                    f"**Reason**: Verification\n"
                    f"**Link**: [Ticket Channel](https://discord.com/channels/{guild.id}/{channel.id})\n"
                    f"**Created by**: {user.mention}\n"
                    "*Response required*."
                )
            )

            if admin_msg_channel is not None:
                await admin_msg_channel.send(embed=embed)
            else:
                print("Admin message channel not found!")

        verifybutton.callback = verify_buttoncallback

In the console i recive:

PS C:\Users\HP\Desktop\benevolence test> & C:/Users/HP/AppData/Local/Microsoft/WindowsApps/python3.12.exe "c:/Users/HP/Desktop/benevolence test/main.py"
The script has started
2024-08-20 18:45:20 INFO     discord.client logging in using static token
2024-08-20 18:45:22 INFO     discord.gateway Shard ID None has connected to Gateway (Session ID: 04d67ddd00824ec679197cf1e9fa862e).
The bot is working and ready to use.
Synced 11 commands.
None
Admin message channel not found!

I have been trying to fix it but I haven't found the answer, so I am asking here for help.

Tried:

message = "1275428501016547338"
admin_msg_channel = bot.get_channel(message)

Didn't work. Tried other ways of doing this but they didnt work too.


Solution

    1. Use guild object instead of bot

    2. Make sure that the parameter of the get_channel function is the ID of the channel

    3. If the ID is a string, make sure to parse it as int

      channel = guild.get_channel(int(message))