0

asking another question. This time more complicated (for me at least). So i'm making a discord bot and when I do for example k!ship, it will print "a" (this is there for debugging) but wont show the embed. When I remove the keyword detector for the words "madoka" and "magical girl", evrey thing is fine. I assume this code bit is the problem. Here is the full code. (But not the token ofc) Also the couple_list has the names removed, there friends names so it makes sense.

# Imports

import discord
from discord.ext import commands
import random

# Credentials
TOKEN = 'use your imagination and imagine a token here :)'

# Create bot
client = commands.Bot(command_prefix='k!')


# Startup Information
@client.event
async def on_message(message):
    if "madoka" in message.content:
        await message.channel.send("✨being meguca is suffering✨")


@client.event
async def on_message(message):
    if "magical girl" in message.content:
        await message.channel.send("✨being meguca is suffering✨")


# Commands

@client.command()
async def jupiter(ctx):
    await ctx.send('Peanut Butter')


# lists for commands
couple_list = ['imagination™']
ship_list = ['Madoka Kaname', 'Homura Akemi', 'Sayaka Miki', 'Mami Tomoe', 'Kyo(u)ko Sakura', 'Hitomi Shizuki',
             'Kyosuke Kamijio']
ship_dis = ['It would be so intense!', 'A nail biter!', 'Its the best pick I got!']


# couple command

@client.command()
async def couple(ctx):
    embed = discord.Embed(title=('In my opinion, ' + (random.choice(couple_list)) + ' and ' + (
        random.choice(couple_list)) + ' would be a great pair!'), color=0x8b0000)
    embed.set_footer(text="What a nice couple ❤♥♥")
    await ctx.send(embed=embed)


# ship command
@client.command()
async def ship(ctx):
    ship_cmd = discord.Embed(title=('In my opinion, ' + (random.choice(ship_list)) + ' and ' + (
        random.choice(ship_list)) + ' would be a great ship!'), color=0x8b0000)
    ship_cmd.set_footer(text=(random.choice(ship_dis)))
    ship_cmd.set_thumbnail(
        url="https://cdn.discordapp.com/attachments/801105512933752882/845050099678576640/unknown.png")
    await ctx.send(embed=ship_cmd)
print("a")

# status
@client.event
async def on_ready():
    await client.change_presence(
        activity=discord.Activity(type=discord.ActivityType.watching, name='TOO MANY NAGITO EDITS!!'))



client.run(TOKEN)

Any help solving this would be great! -Jake

  • 2
    You can't have multiple `on_message` events, your code works fine for me. `a` is printed because you have not indented it correctly, it has to be under your `await` function. – Dominik May 21 '21 at 23:05
  • 3
    Does this answer your question? [Why does on\_message stop commands from working?](https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working) – Dominik May 21 '21 at 23:11

1 Answers1

1

Your error is pretty much easy to handle. You have actually added on_message function multiple times which is not how you do it.

You use if statements and thus you can use them by keeping them all in one single function.

For Example:

I am going to perform two task both of which will be in on_message defined only once.

@bot.event
async def on_message(message):
    if "Hello" in message.content:
        await message.channel.send("Hi!")
    if "Bye" in message.content:
        await message.channel.send("Bye!")

So this shows you how can you do it. But now we are implementing it in your code. So if we compile all of that in one function:

@client.event
async def on_message(message):
    if "madoka" in message.content:
        await message.channel.send("✨being meguca is suffering✨")

    if "magical girl" in message.content:
        await message.channel.send("✨being meguca is suffering✨")

Now this will help you let you use all of it correctly.

But now, I saw that you are using that you are creating command() as well, so you have to know one thing. If you do not add a process_command() statement then your commands won't work at all. You would literally keep trying to find error but your won't know. So I am making it clear.

The following statement is very important when you are trying to use event with the command().

await client.process_command(message)

Now Let's add it to the your code.

@client.event
async def on_message(message):
    if "madoka" in message.content:
        await message.channel.send("✨being meguca is suffering✨")

    if "magical girl" in message.content:
        await message.channel.send("✨being meguca is suffering✨")
    
    await client.process_command(message)

This will surely help you with the error. If still you have any problem, please ask my in the comments.

Thank You! :D

Bhavyadeep Yadav
  • 819
  • 8
  • 25