1

I want to make a discord bot in python

this is my code:

@app.event
async def on_message(message):
    sendu = exec(message.content)
    await message.channel.send(sendu)

if I send a message in discord and save in var sendu and use exec function to run the sendu but every time I try my code example: random.randint(1, 100) it returns this error, what is wrong with my code?

discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message
nonimportant
  • 406
  • 4
  • 15
Doi Lee
  • 23
  • 1
  • 2
    [Exec doesn't return the output](https://stackoverflow.com/questions/2220699/whats-the-difference-between-eval-exec-and-compile#:~:text=eval%20and%20exec%20have%20these%20two%20differences%3A&text=eval%20returns%20the%20value%20of,really%20does%20not%20return%20anything), use eval instead. Also make sure only you can use a command like that, it is very dangerous to leave something like that unprotected. – Buster Jan 01 '21 at 02:24

2 Answers2

0

According to the error message Cannot send an empty message, it looks like sendu is empty. Make sure it's not empty, and then try again!

Ayush Garg
  • 2,234
  • 2
  • 12
  • 28
  • I send msg in discord ran(random class).randint(1, 100) But it returns error Im sure i send it as sendu is not empty. – Doi Lee Jan 04 '21 at 05:15
0

exec() from programiz:

exec() doesn't return any value, it returns None.

The exec() method executes the dynamically created program, which is either a string or a code object.

Consider a situation, you are using a Unix system (macOS, Linux, etc) and you have imported os module. The os module provides a portable way to use operating system functionalities like read or write a file.

If you allow users to input a value using exec(input()), the user may issue commands to change file or even delete all the files using the command os.system('rm -rf *').

try using eval() instead as it has a return value but be careful! as it is similar to exec()!

nonimportant
  • 406
  • 4
  • 15