Issue
I have searched the web several times for this issue, and there seem to be a lot of people having this problem. However, they all seemed to have fixed it by putting the expression in the on_ready() event. But I want to use it in a command, where the user passes a channel id, which will be a string, and I made this code:
@client.command()
async def send_message(ctx,channel,*,msg):
channelid = int(channel)
channel = client.get_channel(channelid)
await channel.send(msg)
When I run the code, and execute the command, it says:
Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send'
Does anyone know how to fix this?
Solution
You're making it a little complicated for yourself. You can make it much simpler.
In itself, there's not much wrong with the code for now, but it's best to use the tools at our disposal.
To get a channel you can use discord.TextChannel as an argument.
It will look like this:
channel: discord.TextChannel
Here you can use either the ID, the name of the channel or a mention.
The rest of your code looks right to me at first.
Your new code:
@client.command()
async def send_message(ctx, channel: discord.TextChannel, *, msg):
await channel.send(msg) # send the "msg" to your specified "channel".
Answered By - Dominik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.