Ask coding questions

← Back to all posts
discord.py Not sending message
h
elipie (323)

So, I am making another discord bot, but this time in python. I hoped I would get flawless, beautiful code, but nahhhh. That is not coding. Coding is errorss

The code:

import discord
# Work with Python 3.6

TOKEN = 'NzgxNTE5NzM0NzA4ODk1Nzk0.X7-0-A.wOKSBq-umxSk9nuDe3VJ67300C0'

client = discord.Client()

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content.startswith('--help'):
        msg = 'Cmds:\n--: Prefix\n--help\n--pie\n'.format(message)
        await client.send_message(message.channel, msg)
    if message.content.startswith('--pie'):
        msg = 'Hey this command is still under development!'.format(message)
        await client.send_message(message.channel, msg)
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.run(TOKEN)

Its not sending the message, and when I run the command:
--pie or --help, I get the following error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 16, in on_message
    await client.send_message(message.channel,msg)
  AttributeError: 'Client' object has no attribute 'send_message' 
  Ignoring exception in on_message 
  Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 16, in on_message
    await client.send_message(message.channel,msg)
  AttributeError: 'Client' object has no attribute 'send_message'

pls help

kthxbai

Answered by CoolJames1610 (656) [earned 5 cycles]
View Answer
Commentshotnewtop
CoolJames1610 (656)

It is

message.channel.send

:)

CoolJames1610 (656)

Also, I would store your TOKEN in a .env file as it is visible TO ALL - which means anybody can control your bot.

To do this in python, create a .env file and put token=[YOUR TOKEN]
then in your main program have

import os
token = os.environ.get("token")
bot.run(token)
RazaAli2486 (0)

don't reveal your bot in public. anyone can control it.