0

This is my code and I added error handling, but it's not working. I'm getting an error in bash when the command is executed by a non-admin. Below command if I type ?hello with admin role it works, but when a non-admin types that commands, they receive You don't have permission to use this command.

token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
prefix = "?"

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command(pass_context=True)
async def on_command_error(self, exception, ctx):
    if isinstance(exception, CheckFailure):
        print("{0} does not have permission to run `{1}`".format(ctx.message.author, ctx.command.name))
    elif isinstance(exception, CommandNotFound):
        # This is handled in CustomCommands
        pass
    else:
        print(exception)
        await self.on_error("on_command_error", exception, ctx) 

@bot.command(pass_context=True)
@commands.has_any_role("Admin", "Moderator")
async def hello(ctx):
    msg = 'Hello... {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

bot.run(token)
5
  • 1
    "i getting error can't run script". So when you run this program, it prints exactly "can't run script", and nothing else? Commented Jun 1, 2018 at 14:53
  • You "tried all codes from internet"? That is a lot of codes ;) Commented Jun 1, 2018 at 14:57
  • yes tried but no idea were i doing wrong. I know something i doing wrong. Commented Jun 1, 2018 at 14:59
  • Looks like it doesn't like your await keyword, there. AFAICT await can only appear inside async functions, and on_command_error hasn't been marked as async. (posting this as a comment and not an answer because I don't know enough about async and/or discord to know whether this completely solves the problem) Commented Jun 1, 2018 at 15:00
  • I added async now updated main script and it runs but can't receive custom error message. Commented Jun 1, 2018 at 15:23

1 Answer 1

1

on_command_error must be a coroutine, and you must register it as an event with your bot

@bot.event
async def on_command_error(error, ctx):
    if isinstance(error, commands.NoPrivateMessage):
        await bot.send_message(ctx.message.author, 'This command cannot be used in private messages.')
    elif isinstance(error, commands.DisabledCommand):
        await bot.send_message(ctx.message.author, 'Sorry. This command is disabled and cannot be used.')
    elif isinstance(error, commands.CheckFailure):
        await bot.send_message(ctx.message.author, 'Sorry. You dont have permission to use this command.')
    elif isinstance(error, commands.MissingRequiredArgument):
        command = ctx.message.content.split()[1]
        await bot.send_message(ctx.message.channel, "Missing an argument: " + command)
    elif isinstance(error, commands.CommandNotFound):
        await bot.send_message(ctx.message.channel, codify("I don't know that command"))
Sign up to request clarification or add additional context in comments.

1 Comment

really great bro your's all reply is awesome everything works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.