I just recently started using cogs/extension files for Discord.py and ran into some issue that I don't know how to fix. I'm trying to send a message in a specific channel but I always just get the error AttributeError: 'NoneType' object has no attribute 'send'
. I know that I can send a message in a specific channel with:
@client.command()
async def test(ctx):
await ctx.send("test")
channel = client.get_channel(1234567890)
await channel.send("test2")
And this works perfectly fine in my "main file", but not in my "extension file", so it's not because of a wrong ID. The await ctx.send("test")
works without problems as well, together with any other command I have, just the channel.send
is causing troubles.
I'm importing the exact same libraries & co and otherwise should have the exact same "setup" in both files as well.
CodePudding user response:
NoneType
error means that it doesn't correctly recognize the channel. When you use get_channel
you are looking for the channel in the bot's cache which might not have it. You could use fetch_channel
instead - which is an API call.
@client.command()
async def test(ctx):
await ctx.send("test")
channel = await client.fetch_channel(1234567890)
await channel.send("test2")
CodePudding user response:
As you know, the error occurs because your channel is not recognized. The solution is fetch_channel(channel_id)
. The problem lies in your channel = client.get_channel (1234567890)
.
Try adding await
before client. Next replace get_channel
with fetch_channel
.
For general use consider your get_channel(), instead you need fetch_channel (channel_id) to retrieve an x.GuildChannel
or x.PrivateChannel
with the specified ID.
@client.command()
async def test(ctx):
await ctx.send("test")
channel = await client.fetch_channel(1234567890) #update
await channel.send("test2")