-2

This is my code, and on_message is not working when used twice, only the 2nd one is working. Please help me.

async def on_message(message):<br>
  if message.content.startswith('-coinflip'):<br>
    embedVar = discord.Embed(<br>
      title="Toss",<br>
      description=(f'You got {random.choice(heads_tails)}'),<br>
      color=(0xFF0000))<br>
    print(f'-coinflip command used by {message.author}')<br>
    await message.channel.send(embed=embedVar)<br>

@client.event<br>
async def on_message(message):<br>
  if message.content.startswith('-help'):<br>
        embedVar = discord.Embed(<br>
            title="Help arrived!",<br>
            description="So, it looks like you need help, let me help you",<br>
            colour=0xFF0000)<br>
        embedVar.add_field(name="Bot Prefix", value="-", inline=False)<br>
        embedVar.add_field(name="Moderation Commands",<br>
                           value="-help",<br>
                           inline=True)<br>
        embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)<br>
        embedVar.set_thumbnail(<br>
            url=<br>
            "https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif"<br>
        )<br>
        print(f'-help command used by {message.author}')<br>
        await message.channel.send(embed=embedVar)<br>```
Game Maker
  • 31
  • 6

1 Answers1

0

Here's the answer I wrote but couldn't post:

You cannot have 2 on_message event listeners. You can merge the two event listeners and their responses by using if...elif like this instead:

@bot.event 
async def on_message(message): #When any message is sent
    if message.content.startswith('-coinflip'):
        embedVar = discord.Embed(
            title="Toss",
            description=f'You got {random.choice(heads_tails)}',
            color=(0xFF0000))
        print(f'-coinflip command used by {message.author}')
        await message.channel.send(embed=embedVar)

    elif message.content.startswith('-help'):
        embedVar = discord.Embed(
            title="Help arrived!",
            description="So, it looks like you need help, let me help you",
            colour=0xFF0000)
        embedVar.add_field(name="Bot Prefix", value="-", inline=False)
        embedVar.add_field(name="Moderation Commands",
                           value="-help",
                           inline=True)
        embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
        embedVar.set_thumbnail(
            url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
        print(f'-help command used by {message.author}')
        await message.channel.send(embed=embedVar)

elif is the same as else if; in this case, if the message's content doesn't start with "-coinflip" and starts with "-help", it creates and sends an Embed.

I've replaced the heads_tails variable for a fully-functioning code:

from discord.ext import commands
import discord
import discord.utils
import random

intent = discord.Intents(messages=True, message_content=True, guilds=True)

bot = commands.Bot(command_prefix="", description="", intents=intent)

heads_tails = ("Heads", "Tails") #Replace this with your sequence

@bot.event
async def on_ready(): #When the bot comes online
    print("It's online.")

@bot.event
async def on_message(message): #When any message is sent
    if message.content.startswith('-coinflip'):
        embedVar = discord.Embed(
            title="Toss",
            description=f'You got {random.choice(heads_tails)}',
            color=(0xFF0000))
        print(f'-coinflip command used by {message.author}')
        await message.channel.send(embed=embedVar)

    elif message.content.startswith('-help'):
        embedVar = discord.Embed(
            title="Help arrived!",
            description="So, it looks like you need help, let me help you",
            colour=0xFF0000)
        embedVar.add_field(name="Bot Prefix", value="-", inline=False)
        embedVar.add_field(name="Moderation Commands",
                           value="-help",
                           inline=True)
        embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
        embedVar.set_thumbnail(
            url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
        print(f'-help command used by {message.author}')
        await message.channel.send(embed=embedVar)

Also, is "-help" a moderation command? And, try searching for and solving such problems yourself. StackOverflow shouldn't be the first place to ask such questions.

The Amateur Coder
  • 789
  • 3
  • 11
  • 33