0

I’m making a say command for a discord bot and everything is working fine for the most part. What i want to make is a command that one person can use in any channel, and it sends the message delegated, in the channel that it is told to send it in. This all works, and here is my code:

async def say(self, ctx, channelname, *, message):
        """Make the bot say whatever you want it to say"""
        channel = discord.utils.get(ctx.guild.channels, name=channelname)
        await channel.send(message)

However, my problem is that every channel in the intended discord server has an emoji in front of the channel name, so its much easier to just use the discord feature where if you type a # you can click on the name of the channel. But whenever I do that and send it with the # still there it throws this error code:

AttributeError: NoneType object has no attribute “send”

Does anyone know how I might fix this?

Thanks in advance!

2tmb
  • 105
  • 4
  • 11
  • https://stackoverflow.com/questions/16891340/remove-a-prefix-from-a-string – SuperStormer Jan 14 '21 at 00:34
  • Have you enabled intents? – Nurqm Jan 14 '21 at 00:35
  • @Nurqm sorry, whats that? I’m not familiar with that term – 2tmb Jan 14 '21 at 00:38
  • @SuperStormer that didn’t seem to help because even if I remove those characters and get just the ID, i still get the same error because the discord.utils.get(ctx.guild.channels) method takes a string of characters (The “name” of a channel) as opposed to the specific ID of the channel. Is there a way to make a channel object from that ID? Because if so that would solve my issue. – 2tmb Jan 14 '21 at 02:41

3 Answers3

1

It would probably be best to use ids (as you mentioned in your previous comments). You can use channel = bot.get_channel(channel_id) to get the channel object, and then call channel.send() on that. No intents should be necessary to simply get a channel. You just need to make sure that the bot has access to the channel and permission to send there.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Catogram
  • 303
  • 1
  • 12
  • Thanks! This was exactly what I needed to do. The bot had admin privelages as well so that wasn’t an issue. I had kinda already figured it out - i had found the actual method - but when i tried to do client.get_channel or even self.client.get_channel it wasn’t working because i defined that as bot. Thanks again! – 2tmb Jan 14 '21 at 05:01
  • 1
    Yep, no problem. It can be confusing to tell the difference between client, bot, and util commands. – Catogram Jan 14 '21 at 05:04
  • One more thing, @dguis, I actually already had the ID - that was the only thing I had. After I posted my question I realized that the only thing I really needed was how to get a channel object from the channel ID. I had found the command but had no earthly idea how to actually run it lol - i found someone running it with client.get_channel(channel_id) but that wasn’t working because my client object was called as bot. – 2tmb Jan 14 '21 at 13:56
  • Ah, that makes sense. That confused me for a while too. – Catogram Jan 14 '21 at 23:44
0

In the version of discord.py 1.5.x, there are Gateway Intents that is necessary in order to getting members, channels, guilds, using events etc. You have to define it in your code and also enable it from Developer Portal.

You can check the API References to define the intents you need and enable the Privileged Gateway Intents from Developer Portal.

Nurqm
  • 4,715
  • 2
  • 11
  • 35
  • that doesn’t seem to be my issue. I’ve managed to get just the channel ID, do you know any way i could get the channel object from that channel ID? I have the plain id (just the number) and the ID with the <# and > part. If i was able to get the object then I’d be done, thats the main thing i need to get, the object version of that channel. – 2tmb Jan 14 '21 at 01:21
  • The reason you're not getting the channel is because of intents. Without enabling and defining intents, you cannot get channels or members. – Nurqm Jan 14 '21 at 02:07
  • I enabled both intents, although neither seemed to prevent getting guilds/channels. However its still not working, i think because i am getting the channel ID as an input to the function but the discord.utils.get(ctx.guild.channels, name=“”) function creates a channel object based on a string that is the name of the channel, not based on a channel ID. Is there another method i can replace that with so it works with channel ID’s? – 2tmb Jan 14 '21 at 02:30
0

WARNING: I DO NOT RECOMMEND USING THIS CODE IF YOU RUN THIS BOT ON MULTIPLE SERVERS BECAUSE IT WILL SEND THE MESSAGE TO THE SPECIFIED CHANNEL EVEN IF THE COMMAND IS RUN ON A DIFFERENT SERVER WITH THE BOT IN IT!

Depending on if you're using @client or @bot you need to switch every "client" in the code below to "bot"

@client.command()
async def say(ctx, *, message):
    channel = client.get_channel(put the channel ID that you need the bot to send to)
    
    await channel.send(f"{message}")

All you need to do after this is go to your discord server and type in any channel:

(prefix)say message

And the bot will send your message into the channel that you entered in the code.