#get_channel returns NonType, how can I fix that?

1 messages · Page 1 of 1 (latest)

snow raft
#
    intents = discord.Intents.default()
    bot = commands.Bot(command_prefix=Auth.COMMAND_PREFIX, intents=intents, auto_sync_commands=True)


    async def embedder(self, ctx):
        user = ctx.author

        #The get channel part
        target_channel = bot.get_channel(1128731013875511316)

        if len(ctx.message.attachments) == 0:
            await ctx.send("No image attachment found.")
            return

        image_attachment = ctx.message.attachments[0]
        if not image_attachment.url.endswith(('.png', '.jpeg', '.jpg', '.gif')):
            await ctx.send("Invalid image format. Only PNG, JPEG, JPG, and GIF images are supported.")
            return

        embed = discord.Embed()
        embed.set_image(url=image_attachment.url)

        #The part that throws an error
        embed.set_author(name=ctx.author.name, icon_url=user.display_avatar)

        await target_channel.send(embed=embed)
#

The channel id is correct and exists, the bot has access to it too

jovial pilot
#

try

#

.rtfm bot.fetch_channel

snow raft
jovial pilot
#

you should await it

#

await the fetch

snow raft
#

It keeps getting odder: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: '_MissingSentinel' object has no attribute 'request'

jovial pilot
#

are you running embedder after on_ready

frigid spade
#

also, if you only want to send a message, you should use partial channel object

#

instead of fetching the channel

#

fetching the channel is over the api. which is a slow process

#
- channel = bot.fetch_channel(123)
+ channel = bot.get_partial_messageable(123)
#

thats all

snow raft
#
Traceback (most recent call last):
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 378, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\dimen\Desktop\economy-bot-discord.py\economy with MYSQL\cogs\events.py", line 63, in on_command_error
    raise error
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 347, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 950, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 187, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: '_MissingSentinel' object has no attribute 'request'
#
intents = discord.Intents.default()
bot = commands.Bot(command_prefix=Auth.COMMAND_PREFIX, intents=intents, auto_sync_commands=True)

@commands.command(usage="submit to voting")
    async def embedder(self, ctx):
        user = ctx.author

        #The get channel part
        target_channel = bot.get_partial_messageable(1128731013875511316)

        if len(ctx.message.attachments) == 0:
            await ctx.send("No image attachment found.")
            return

        image_attachment = ctx.message.attachments[0]
        if not image_attachment.url.endswith(('.png', '.jpeg', '.jpg', '.gif')):
            await ctx.send("Invalid image format. Only PNG, JPEG, JPG, and GIF images are supported.")
            return

        embed = discord.Embed()
        embed.set_image(url=image_attachment.url)

        #The part that throws an error
        embed.set_author(name=ctx.author.name, icon_url=user.display_avatar)

        await target_channel.send(embed=embed)

Here's the new full code

frigid spade
#

can you show on_command_error

snow raft
frigid spade
#

Till what line does the code run?

#

Add a few print lines, or use a debugger

snow raft
#

until await target_channel.send(embed=embed)

#

That's where it kicks the bucket

frigid spade
#

hmm

frigid spade
snow raft
#

That's a lot, get ready xd

#

Maybe if you have a code snippet that you know works I can import it and check if it works with me as well

frigid spade
#

oh wow

#

you should uninstall these

discordpy-antispam      1.0.3
disnake                 2.9.0
disnake-pagination      1.1
#

and reinstall pycord

#

also, consider a venv

snow raft
#

Deinstalled those and reinstalled pycord, same error message 😅

white estuary
#

After you uninstalled them, did you reinstall py-cord?

#

If not, uninstall & reinstall

snow raft
#

I did

white estuary
#

again

snow raft
#

I'm back at my computer in an hour or so

snow raft
white estuary
#
pip uninstall py-cord -y
pip install py-cord
snow raft
#

This must be the programmer equivalent of have you try turning it off and on again lol

#

Yeah still doesn't work

#

Exact same error fsr

#
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 178, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\dimen\Desktop\economy-bot-discord.py\economy with MYSQL\cogs\inventory.py", line 95, in embedder
    await target_channel.send(embed=embed)
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\abc.py", line 1616, in send
    data = await state.http.send_message(
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 283, in request
    async with self.__session.request(
AttributeError: '_MissingSentinel' object has no attribute 'request'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 378, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\dimen\Desktop\economy-bot-discord.py\economy with MYSQL\cogs\events.py", line 63, in on_command_error
    raise error
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 347, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 950, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\dimen\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 187, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: '_MissingSentinel' object has no attribute 'request'``` Here is the entire error again
white estuary
#

What is MissingSential pointing to?

#

Whats the code

snow raft
#

I also get this when printing the target_channel variable, maybe this might help? <discord.channel.PartialMessageable object at 0x00000182FDEDBDF0>

white estuary
#

Like
Channel = bot.get_channel(284848229299494)

snow raft
#

Maybe it'll work now since I uninstalled those other packages, I'll try it out

#

Nope, still returns None

snow raft
#

VSCode debugger

#

just F5

frigid spade
#

Hmm

#

Can you show the main file

snow raft
#
from modules import bank_funcs, inventory_funcs
from config import Auth

import os
import discord

from pycolorise.colors import *
from discord.ext import commands

intents = discord.Intents.all()
client = commands.Bot(command_prefix=Auth.COMMAND_PREFIX, intents=intents, auto_sync_commands=True)


@client.event
async def on_ready():
    await client.change_presence(
        status=discord.Status.online,
        activity=discord.Game(f"{Auth.COMMAND_PREFIX}help")
    )

    # if you are using 'discord.py >=v2.0' comment(remove) below code
    print(Purple("\nLoading Cogs:"))
    for file in os.listdir("./cogs"):
        if file.endswith(".py"):
            filename = file[:-3]
            try:
                client.load_extension(f"cogs.{filename}")
                print(Blue(f"- {filename} :white_check_mark: "))
            except:
                print(Blue(f"- {filename} :x: "))

    # if you are using 'discord.py >=v2.0' uncomment(add) below code
    # print(Purple("\nLoading Cogs:"))
    # for file in os.listdir("./cogs"):
    #     if file.endswith(".py"):
    #         filename = file[:-3]
    #         try:
    #             await client.load_extension(f"cogs.{filename}")
    #             print(Blue(f"- {filename} :white_check_mark: "))
    #         except:
    #             print(Blue(f"- {filename} :x: "))

    print()
    await inventory_funcs.DB.connect()
    if not inventory_funcs.DB.is_connected:
        raise RuntimeError("Database access denied")

    await bank_funcs.create_table()
    await inventory_funcs.create_table()
    print(Cyan("Created/modified tables successfully"))

    print(Cyan(f"{client.user.name} is online !"))


if __name__ == "__main__":
    # Make sure to add Bot Token in '.env' file
    client.run(Auth.TOKEN)
``` This is main, the embedder command I made is in the inventory cog
frigid spade
#

You made a new variable bot in the cog file

#

You are supposed to use the one you set in the init of the Cog

#

See the guide

#

It has an example

#

.tag guide

tired leafBOT
snow raft
frigid spade
snow raft
#

Thank you all everyone for the help hollowparty

snow raft
#

.solved

tired leafBOT
#

This thread was archived by the user that opened it.