-1

This is the code:

async def on_message(message):
    if client.user.mentioned_in(message):
        await message.channel.send("Hey!")

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if "$example" in message.content:
        if message.author.id == int(discord id here):
            await message.channel.send("Hi Name")
        if message.author.id == int(discord id here):
            await message.channel.send("Hi Name2")

So, my issue is if I have the second piece of code in, my first piece of code doesn't work. No error messages, nothing it just doesn't seem to exist. I'm assuming it would also be the same vice versa. My question is: I'd like to know what's causing this and how I should go about fixing it. Is my code incorrect? Am I missing something? Or should I be defining one of them as something else? I'm stuck. Thanks in advance.

  • `on_message` can only refer to one of these functions at a time. I would have expected some kind of runtime error from this, but it's been a while since I wrote some python. See other SO post: [Methods with the same name in one class in Python](https://stackoverflow.com/questions/5079609/methods-with-the-same-name-in-one-class-in-python) – byxor Jan 14 '22 at 17:04
  • I'm they're not being used at the same time, I believe the point of discord py is to program each bot command individually meaning the program doesn't run synchronously. That's why I'm confused with the problem as each definition shouldn't know the other exists. –  Jan 14 '22 at 17:12
  • Python will not allow you to define two or more functions with the same name – byxor Jan 14 '22 at 17:14

2 Answers2

0

There can not exist two functions with the same name in the same file. You can try using a cog or merging the code of your functions.

  • would I just be separating each definition into their own classes? I'm sorry I'm pretty new. –  Jan 14 '22 at 17:15
  • 1
    Yes that's what a cog does, it seperates pieces of code related to the bot so they are easily readable and can be repeated. I have linked the documentation for cogs in my answer. – Konstantinos Georgiadis Jan 14 '22 at 17:17
  • Thank you I'll do that –  Jan 14 '22 at 17:20
  • unsure if you're still online @konstantinos-georgiadis but using classes is breaking my code, over half of the things in discord.py say they don't exist, or things just break no matter how I put them. for example everything comes up red unless I put "self" in my definition like so: async def on_message(self, message): but then when I actually try to use it, it's like I never even declared message. the class breaks my entire code. –  Jan 14 '22 at 17:46
  • Hey @Occy, the documentation page i linked says that event listeners are marked using the decorator `commands.Cog.listener()` that should solve your listener not being marked. Also i suggest that you take a look in bject Oriented Programming and decorators, they are valuable things to know about and will certainly help you with discord cogs. – Konstantinos Georgiadis Jan 15 '22 at 13:27
0

i was having the same problem but i managed to solve it. At the end of the second class put

`await bot.process_commands(message)`