Raise custom error message for

Demotry

So below code blocks server and user id in the list when they use ?hello, so I am trying to raise custom error message. If user id is in list, it will tell User Blacklisted and if server id is in list, it will tell Server has been Blacklisted.

LIST_OF_USER_IDS = ['34534545546', '34534545546']
LIST_OF_SERVER_IDS = ['34534545546', '34534545546']

def blacklists(users, servers):
    def predicate(ctx):
        return ctx.message.author.id not in users and ctx.message.server.id not in servers
    return commands.check(predicate)

@bot.command(pass_context=True)
@blacklists(LIST_OF_USER_IDS, LIST_OF_SERVER_IDS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

So I tried below code but getting error. I'm just a beginner so my code is not correct, so I need a help to fix this.

def blacklists(users, servers):
    def predicate(ctx):
        return ctx.message.author.id not in users and ctx.message.server.id not in servers
    return commands.check(predicate)
try:
       if ctx.message.author.id in LIST_OF_USER_IDS:
           raise UserBlacklisted
       elif ctx.message.server.id in LIST_OF_SERVER_IDS:
           raise ServerBlacklisted
       break
   except UserBlacklisted:
       await bot.send_message(ctx.message.channel, "User Blacklisted")
   except ServerBlacklisted:
       await bot.send_message(ctx.message.channel, "Server has been Blacklisted")
Patrick Haugh

Instead of returning False if the check fails, instead raise a subclass of CommandError, then handle that error in the on_command_error event.

class UserBlacklisted(commands.CommandError):
    def __init__(self, user, *args, **kwargs):
        self.user = user
        super().__init__(*args, **kwargs)

class ServerBlacklisted(commands.CommandError):
    def __init__(self, server, *args, **kwargs):
        self.server = server
        super().__init__(*args, **kwargs)


def blacklists(users, servers):
    def predicate(ctx):
        if ctx.message.author.id in users:
            raise UserBlacklisted(ctx.message.author)
        elif ctx.message.server.id in servers:
            raise ServerBlacklisted(ctx.message.server)
        else:
            return True
    return commands.check(predicate)

@bot.event
async def on_command_error(error, ctx):
    if isinstance(error, UserBlacklisted):
        await bot.send_message(ctx.message.channel, "User {} has been blacklisted".format(error.user.mention))
    elif isinstance(error, ServerBlacklisted):
        await bot.send_message(ctx.message.channel, "Server {} has been blacklisted".format(error.server.name))



@bot.command(pass_context=True)
@blacklists(LIST_OF_USER_IDS, LIST_OF_SERVER_IDS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to raise custom error message in Sails.js

From Dev

Rspec: Custom failure message for raise_error test

From Dev

Django auth custom business logic - how do I raise custom error message?

From Dev

JWPLAYER custom error message

From Dev

Custom Error Message MVC

From Dev

rspec: raise_error usage to match error message

From Dev

raise in raise error

From Dev

Oracle: Raise custom error messages without conflict with default error IDs?

From Dev

How to raise Error message if any rows contains negative Values

From Dev

Custom 404 error message with Go

From Java

JHipster custom error message translation

From Dev

custom error message for assertThat() in junit?

From Dev

Custom message on wait timeout error

From Dev

NaN Custom Error Message in Javascript

From Dev

Custom Error Validation Message Not Showing

From Dev

Custom Error Message in HTML Form

From Dev

Url rewrite and custom error message

From Dev

Custom Error Validation Message Not Showing

From Dev

Custom Message on Runtime-Error

From Dev

Send custom error message in Ajax error in codeigniter

From Dev

send an email and raise message error when an error occurs in odoo v8

From Dev

displaying custom error message rather than system error message in Delphi

From Dev

Why is this rspec 'expect to raise_error' failing, when the message returned is 'ActiveRecord::RecordInvalid'

From Dev

Code for waiting my previous condition for 2 minutes if not work , then raise an error message using python

From Dev

Grails define custom error message for command object

From Dev

Parsley custom error message doesn't work

From Dev

Custom `assert` macro that supports commas and error message

From Dev

Returning the custom validation method parameter in error message

From Dev

Custom error message not working in gorm with grails

Related Related

  1. 1

    How to raise custom error message in Sails.js

  2. 2

    Rspec: Custom failure message for raise_error test

  3. 3

    Django auth custom business logic - how do I raise custom error message?

  4. 4

    JWPLAYER custom error message

  5. 5

    Custom Error Message MVC

  6. 6

    rspec: raise_error usage to match error message

  7. 7

    raise in raise error

  8. 8

    Oracle: Raise custom error messages without conflict with default error IDs?

  9. 9

    How to raise Error message if any rows contains negative Values

  10. 10

    Custom 404 error message with Go

  11. 11

    JHipster custom error message translation

  12. 12

    custom error message for assertThat() in junit?

  13. 13

    Custom message on wait timeout error

  14. 14

    NaN Custom Error Message in Javascript

  15. 15

    Custom Error Validation Message Not Showing

  16. 16

    Custom Error Message in HTML Form

  17. 17

    Url rewrite and custom error message

  18. 18

    Custom Error Validation Message Not Showing

  19. 19

    Custom Message on Runtime-Error

  20. 20

    Send custom error message in Ajax error in codeigniter

  21. 21

    send an email and raise message error when an error occurs in odoo v8

  22. 22

    displaying custom error message rather than system error message in Delphi

  23. 23

    Why is this rspec 'expect to raise_error' failing, when the message returned is 'ActiveRecord::RecordInvalid'

  24. 24

    Code for waiting my previous condition for 2 minutes if not work , then raise an error message using python

  25. 25

    Grails define custom error message for command object

  26. 26

    Parsley custom error message doesn't work

  27. 27

    Custom `assert` macro that supports commas and error message

  28. 28

    Returning the custom validation method parameter in error message

  29. 29

    Custom error message not working in gorm with grails

HotTag

Archive