1

when I am trying to send a message to a channel it won't work. Anyone knows why?

This here is the command I am using. This is just the code block of the not working section.

@bot.command(pass_context=True)
    async def test2(ctx):
    await ctx.message.delete()
    print("The Test is being started")
    Channel = config['bot']['id1']
    await Channel.send("test2")

And in My Config file, I have this

[bot]
id1= 916845047CENSORED

And when I try to run it I get this error:

The test is being started
Ignoring exception in command test2:
Traceback (most recent call last):
  File "C:\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/jp/Desktop/DISCORD BOT/bot.py", line 224, in test2
    await Channel.send("test2")
AttributeError: 'str' object has no attribute 'send'

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

Traceback (most recent call last):
  File "C:\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'send'

If anyone knows how to fix this I would thank you very much


Edit:

The way to solve this is:

add the int to the channel ID if it is from a txt file/config

@bot.command(pass_context=True)
async def test2(ctx):
    await ctx.message.delete()
    Channel_Id = config['bot']['id1']
    print("Test is being started")
    Chanel = bot.get_channel(int(Channel_Id))
    await Chanel.send("test2")
The DEV
  • 53
  • 1
  • 8

3 Answers3

0

Look at line 85 to find the line throwing the error. It's saying that the Channel object taken from config['bot']['id1'] is of type str. You are then trying to invoke the method .send() on a str type, which it does not have.

...
Channel = config['bot']['id1']
await Channel.send("test2")
Jimenemex
  • 3,104
  • 3
  • 24
  • 56
  • I have heard from some other people that this is the issue but how do I get it to be valid str? – The DEV Dec 05 '21 at 12:38
0

With discord.py, channel objects are not just integers. Instead, you have to fetch the channel, either from a guild after fetching the guild with its id, or fetching the channel straight away. Here are some ways to do this:

Channel_Id = config['bot']['id1']

# Based on your error, ensure to convert Channel_Id to an Integer
# Example: "123454" -> 123454
Channel_Id = int(Channel_Id)

# Method 1: Fetching channel straight away
Channel = bot.get_channel(Channel_id)

# Method 2: Fetching channel from guild
Guild_Id = config['bot']['GuildId'] # of course you may need to add a new config
# Remember to convert this to an Integer as well!
Guild_Id = int(Guild_Id)
Guild = bot.get_guild(Guild_Id)
Channel = Guild.get_channel(Channel_Id)

await Channel.send("Test")

Other helpful links:

Please note: When searching for this answer, it is recommended not to use bot.fetch_channel as this is an API call. The above methods are recommended for general use.


Edit: When using bot.get_channel or the likes, you are likely to require Intents.

Links to questions like this:

Bagle
  • 2,326
  • 3
  • 12
  • 36
  • Hi sadly this didn't work I corrected my code. I am still getting the same error – The DEV Dec 05 '21 at 12:35
  • `@bot.command(pass_context=True) async def test2(ctx): await ctx.message.delete() Channel_Id = config['bot']['id1'] Guild_Id = config['bot']['gu1'] print("Test is being started") Guild = bot.get_guild(Guild_Id) Channel = Guild.get_channel(Channel_Id) await Channel.send("test2")` this also doesnt work than I get this error: `discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'get_channel' – The DEV Dec 05 '21 at 12:39
  • Just to be sure, is both the `Channel_Id` and `Guild_Id` an integer? Or are they numbers under strings? Also check if the guild id is valid, such as by printing `Guild.name` or something similar. You can also try using Method 1 instead, as this usually works for me. @TheDEV – Bagle Dec 06 '21 at 05:49
  • Tried using this code: `Channel_Id = config['bot']['id1'] Guild_Id = config['bot']['gu1'] print("Test is being started") print("Channel ID: " + str(bot.get_channel(Channel_Id))) print("Guild ID: " + str(bot.get_guild(Guild_Id))) print("Guildname: " + str(bot.get_guild(Guild_Id).name)) print("Chanelname: " + str(bot.get_channel(Channel_Id).name))` But from the looks of it it cant fetch the guild name or the chanel name it says none and than it says: `NoneType' object has no attribute 'name'` – The DEV Dec 06 '21 at 10:18
  • @TheDEV my mistake, I completely misunderstood your situation. I have noticed that this may be an issue with `Intents`. Please view the revised answer, thanks! – Bagle Dec 06 '21 at 13:31
  • 1
    Thanks so much the way to solve the problem is just to add this: `Channel_Id = (int(Channel_Id))` – The DEV Dec 06 '21 at 15:19
0

The way to solve this is:

add the int to the channel ID if it is from a txt file/config

@bot.command(pass_context=True)
async def test2(ctx):
    await ctx.message.delete()
    Channel_Id = config['bot']['id1']
    print("Test is being started")
    Chanel = bot.get_channel(int(Channel_Id))
    await Chanel.send("test2")```
The DEV
  • 53
  • 1
  • 8