My code Wont run any line other than the bottom line of code

Question:

I was using the program fine and the bot and commands all worked as intended however after restarting the program only the code at the bottom would work, i.e if I deleted the slap section the punch would work but no other section.

If I deleted the punch section after that the help would work but no other section apart from that.

Here is my code:

# installed: discord api
import discord
import random
from datetime import date
from discord.ext import commands
from discord.embeds import Embed
from discord import colour

TOKEN = '***********'

intents = discord.Intents.all()
intents.message_content = True
client = discord.Client(intents=intents)

bot = commands.Bot(command_prefix='!', intents=intents)

@client.event
async def on_ready():
    print(f"Bot logged in as {client.user}")

@client.event
async def on_message(message):
    # greeting the bot
    if message.author != client.user:
        if message.content.lower().startswith('!hello'):
            await message.channel.send(f"Wassup, `{message.author}`")

    # help command which states all available bot commands
    if message.author != client.user:
        if message.content.lower().startswith('!help'):
            await message.channel.send(f"`{message.author}` nHere are all my commands:n n!hello ~ greet the bot n"
                                       f"!roll ~ the bot will give a random value between 1 and 100 n"
                                       f"!day ~ the bot will give the date and time n"
                                       f"!punch ~ the bot will send a punching gif ( not nsfw )n"
                                       f"!slap ~ the bot will send a slapping gifn"
                                       f"")

    # roll command which returns a random value between 1 and 100
    if message.author != client.user:
        if message.content.lower().startswith('!roll'):
            number = str(random.randint(1, 100))
            await message.channel.send('Your number is: ' + number)

    # day command which returns the date and time of the local system
    if message.author != client.user:
        if message.content.lower().startswith('!day'):
            today = date.today()
            await message.channel.send(f"The local date today is {today}")

# gif command which sends a punching gif to a tagged person
@client.event
async def on_message(message):
    if message.author != client.user:
        if message.content.lower().startswith('!punch'):
            punch_gifs = ['https://media.tenor.com/6Cp5tiRwh-YAAAAC/meme-memes.gif']
            punch_names = ['Punched You']
            embed = discord.Embed(
                colour=(discord.Colour.random()),
                description=f'{message.author.mention} {random.choice(punch_names)}'
            )
            embed.set_image(url=(random.choice(punch_gifs)))
            await message.channel.send(embed=embed)

# gif command which sends a slapping gif to a specified person
@client.event
async def on_message(message):
    if message.author != client.user:
        if message.content.lower().startswith('!slap'):
            slap_gifs = ['https://media.tenor.com/mNAs5CO1deIAAAAC/slap.gif']
            slap_names = ['slapped you']
            embed = discord.Embed(
                colour=(discord.Colour.random()),
                description=f'{message.author.mention} {random.choice(slap_names)}'
            )
            embed.set_image(url=(random.choice(slap_gifs)))
            await message.channel.send(embed=embed)

client.run(TOKEN)

I tried retyping the code and attempting to change certain code functions to avoid this but to no available.

Asked By: xanebot

||

Answers:

You have multiple functions named on_message, so Python doesn’t know which one to call. Just put each command in its own function, then you can stack elif statements in your on_message.

# installed: discord api
import discord
import random
from datetime import date
from discord.ext import commands
from discord.embeds import Embed
from discord import colour

TOKEN = 'TOKEN'

intents = discord.Intents.all()
intents.message_content = True
client = discord.Client(intents=intents)

bot = commands.Bot(command_prefix='!', intents=intents)


@client.event
async def on_ready():
    print(f"Bot logged in as {client.user}")


# greeting the bot
async def hello_cmd(message):
    await message.channel.send(f"Wassup, `{message.author}`")


async def help_cmd(message):
    await message.channel.send(f"`{message.author}` nHere are all my commands:n n!hello ~ greet the bot n"
                               f"!roll ~ the bot will give a random value between 1 and 100 n"
                               f"!day ~ the bot will give the date and time n"
                               f"!punch ~ the bot will send a punching gif ( not nsfw )n"
                               f"!slap ~ the bot will send a slapping gifn"
                               f"")


async def roll_cmd(message):
    # roll command which returns a random value between 1 and 100
    number = str(random.randint(1, 100))
    await message.channel.send('Your number is: ' + number)


# day command which returns the date and time of the local system
async def day_cmd(message):
    today = date.today()
    await message.channel.send(f"The local date today is {today}")


# gif command which sends a punching gif to a tagged person
async def punch_cmd(message):
    punch_gifs = ['https://media.tenor.com/6Cp5tiRwh-YAAAAC/meme-memes.gif']
    punch_names = ['Punched You']
    embed = discord.Embed(
        colour=(discord.Colour.random()),
        description=f'{message.author.mention} {random.choice(punch_names)}'
    )
    embed.set_image(url=(random.choice(punch_gifs)))
    await message.channel.send(embed=embed)


# gif command which sends a slapping gif to a specified person
async def slap_cmd(message):
    slap_gifs = ['https://media.tenor.com/mNAs5CO1deIAAAAC/slap.gif']
    slap_names = ['slapped you']
    embed = discord.Embed(
        colour=(discord.Colour.random()),
        description=f'{message.author.mention} {random.choice(slap_names)}'
    )
    embed.set_image(url=(random.choice(slap_gifs)))
    await message.channel.send(embed=embed)


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

    if message.content.lower().startswith('!slap'):
        await slap_cmd(message)
    elif message.content.lower().startswith('!punch'):
        await punch_cmd(message)
    elif message.content.lower().startswith('!roll'):
        await roll_cmd(message)
    elif message.content.lower().startswith('!hello'):
        await hello_cmd(message)
    elif message.content.lower().startswith('!day'):
        await day_cmd(message)
    elif message.content.lower().startswith('!help'):
        await help_cmd(message)

client.run(TOKEN)
Answered By: Michael M.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.