user13524876
user13524876

Reputation:

Send a message to a channel using a command discord.py

I am trying to make a -say #channel-name message command for my discord bot using the code:

@commands.command()
@commands.has_permissions(manage_messages=True)
async def say(self, ctx, channel, *, message):
    channel = discord.
    await channel.send(message)

And got an error
discord.ext.commands.errors.CommandInvokeError: CommandAttributeError: 'str' object has no attribute 'send'
As far as I'm aware, this code is correct and should be able to send a message to the channel mentioned, but it does not and throws the error instead.

What am I doing wrong?

Upvotes: 1

Views: 2549

Answers (1)

Sachin Raja
Sachin Raja

Reputation: 304

The channel is of type str, not discord.TextChannel. To convert it to a channel you can do channel : discord.TextChannel in the arguments:

@commands.command()
@commands.has_permissions(manage_messages=True)
    async def say(self, ctx, channel : discord.TextChannel, *, message):
    channel = discord.
    await channel.send(message)

Upvotes: 1

Related Questions