Home > front end >  I am having problem in making a discord bot
I am having problem in making a discord bot

Time:05-31

This is my code. @client.command() is being ignored. Please help me if you can. @client.event is working fine but @client.command() is completely being ignored. Even if I add a new command or change the current one, it just does not works. I don't even get any errors.

import discord
import os
import random
from keep_alive import keep_alive
from discord.ext import commands

paint=[0xff0000 , 0x0000ff , 0x000000 , 0x00ffff , 0xffffff , 0xffd700 , 0x4b0082]
heads_tails = 'Heads', 'Tails'
prefix = ['-']
client = commands.Bot(prefix)
client.remove_command('help')

@client.event
async def on_ready():
    await client.change_presence(activity=discord.Game('with The Boys'))
    print("Ready to help you.")


@client.event
async def on_message(message):
    if message.content.startswith('-help'):
      embedVar = discord.Embed(
            title="Help arrived!",
            description="Here are a list of commands for your help",
            colour=(random.choice(paint)))
      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"
        )
      await message.channel.send(embed=embedVar)
    if message.content.startswith('-coinflip'):
        embedVar = discord.Embed(
            title="Coinflip",
            description=(f'You got {random.choice(heads_tails)}'),
            colour=(random.choice(paint)))
        await message.channel.send(embed=embedVar)
        await client.process_commands(message)

@client.command(pass_context = True)
async def mute(ctx, member: discord.Member):
     if ctx.message.author.server_permissions.administrator:
        role = discord.utils.get(member.server.roles, name='Muted')
        await ctx.add_roles(member, role)
        embed=discord.Embed(title="User Muted!", description="**{0}** was muted by **{1}**!".format(member, ctx.message.author), color=0xff00f6)
        await ctx.send(embed=embed)
     else:
        embed=discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff00f6)
        await ctx.send(embed=embed)

keep_alive()
client.run(os.getenv('Token'))

CodePudding user response:

Try to run it through shell. I got same issue while running a simple bot , got code 0 as output but nothing output,

CodePudding user response:

The issue that you are facing is that you have accidentally placed your client.process_commands(message) function inside your coinflip condition. Moving this line of code outside of the if statement should resolve your issues.

@client.event
async def on_message(message):
    await client.process_commands(message)

CodePudding user response:

First of all, why didn't you make "help" and "coinflip" a bot command? Make everything possible a bot command and not the async def on_message(message)

@client.command()
async def help(ctx)
     embed = discord.Embed(
            title="Help arrived!",
            description="Here are a list of commands for your help",
            colour=(random.choice(paint)))
      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"
        )
      await message.channel.send(embed=embed)
  • Related