1
votes

My goal is to make a discord bot that responds with the same message the user sent, and if the person sends a file, the bot sends the link of it.

I can get both parts working separately, but together I just run into issues (see bottom), in my current code only "stand-alone" file sending works, text messages not, and if you have text + a file, the file link will be sent by the bot but the text wont. If I put the 2nd part on top, then I get basically the complete opposite, only the text gets sent, no files.

My code:

@client.event # Clone message
async def on_message(message):
    await client.process_commands(message)
    if message.author == client.user:
        return
    ch = message.channel
    await ch.send(message.content)

@client.event # Clone file
async def on_message(message):
    await client.process_commands(message)
    if message.author == client.user:
        return
    url = message.attachments[0].url
    ch = message.channel
    await ch.send(url)

Clone file on bottom always gives a IndexError: list index out of range error

Clone message on bottom always gives a 400 Bad Request (error code: 50006): Cannot send an empty message error

1
well what you are doing with the bottom function is overwriting the first function you make... You should merge them into a single on_message function where you can handle expected errors with a try except loop or make sure you dont get errors by checking your message data firstFierySpectre

1 Answers

1
votes

As @FierySpectre pointed out, you're overwriting the on_message function. You can combine what you want to do into one single function.

@client.event
async def on_message(message):
    await client.process_commands(message)
    if message.author == client.user:
        return
    ch = message.channel
    try: # tries to send the url of the file
        await ch.send(message.attachments[0].url)
    except IndexError: # if index error is received, that means the user entered a regular message
        pass
    await ch.send(message.content) # prints out the message