discordRebot package

discordRebot.manager module

class discordRebot.manager.Manager(P2F={re.compile('^!echo (.*)$'): <function Manager.<lambda>>}, authorize=True, escMD=False, listenBot=False, filter=None)[source]

Bases: object

To manage all commands with their respective authorized authors

Parameters
  • P2F (Union[Mapping[Union[Pattern, str], CallBack], Mapper]) –

    Pattern or Str to Function (callback) Map (or) Mapper object

    if Pattern matchs with message.content then callback is called with message, captured groups
    if Str is equal to message.content then callback is called with message
    In both the return string by callback is the reply message. No reply if return value is None.

  • authorize (bool) –

    allow only Authorized members to execute command (to call callback)

    if False all callbacks (P2F.values()) is callable by all members
    else for each command only members authorized by AuthorizeCallBack() is allowded.

  • escMD (bool) –

    escape MarkDown while reply

    if true reply (returned by callback) is filtered to escape MarkDown
    else no filtering to escape MarkDown.

  • listenBot (bool) –

    allow reply to bot’s message (this may leads to infinte loop of messages)

    if true bot’s message can’t be skiped in on_message

  • filter (Optional[Callable[[NewType()(Message, Message)], bool]]) –

    function to filter messages

    if filter(message) is True then proceed on_message
    else skip that message and continue

ContentFieldLimits = 2000
async on_message(message)[source]

It is the on_message event listner.

set it in client.event like client.event(Manager().on_message)

async execute(callback, message, *args)[source]

To execute the matched function, if the author is authorized by AuthorizeCallBack()

Example:

def cmd(msg, *args):
    #code#
cmd.auth = None
Parameters
  • callback (CallBack) – function to be called if message.author is Authorized by discordRebot.manager.AuthorizeCallBack()

  • message (NewType()(Message, Message)) – message object from discord, having all details about a message

  • *args (str) – arguments to pass into callback

async reply(message, response)[source]

To send the response from callback in execute()

Parameters
  • message (NewType()(Message, Message)) – message object from discord, having all details about a message

  • response (Optional[str]) – response from callback

async on_denied(callback, message)[source]

Called when unauthorized and returns the denial message

Parameters
  • callback (CallBack) – command which is skiped due to unauthorized

  • message (NewType()(Message, Message)) – message object from discord, having all details about a message

Return type

Optional[str]

Returns

denial message

class discordRebot.manager.Mapper(P2F=None)[source]

Bases: object

To create P2F map easily using decorator

Example:

key = Mapper()
@key(re.compile(r'^! ([\s\S]*)$'))
def cmd1(msg, arg):
    #code#
@key('cmd2','command2') # 'command2' and 'cmd2' both mapped to cmd2 callback
def cmd2(msg):
    #code#
client.event(Manager(key).on_message)
Parameters

P2F (Optional[MutableMapping[Union[Pattern, str], CallBack]]) – dict if you want to continue with previous P2F or None to create new one

discordRebot.manager.Authorize(members, message)[source]

Authorize whether member belongs to members

Parameters
  • members (Union[Members, MembersSet, int, str, Pattern, Roles, Permissions, Set[Union[int, str, Pattern, Roles, Permissions]], Sequence[Union[int, str, Pattern, Roles, Permissions]], Callable[[NewType()(Message, Message)], bool]]) – authorized members

  • message (NewType()(Message, Message)) – message.author to be authorized

Return type

Union[int, str, Pattern, Roles, Permissions, None]

Returns

memberIdentity used for authorization or None if not authorized (member not belongs to members)

discordRebot.manager.AuthorizeCallBack(callback, message)[source]

Checks whether member is authorized to access the callback

Parameters
  • callback (CallBack) – callback for which authorizing member

  • message (NewType()(Message, Message)) – message.author to be authorized

Return type

bool

Returns

True if authorized else False

discordRebot.members module

class discordRebot.members.Roles(guild_id=None, roles=None)[source]

Bases: object

To group members with their roles in a server.

This can’t be used to Authorize in DM channel.

Example:

server1 = Roles(1234567890) # guild_id of server1
Fn.auth = {server1['Admin','Mod'], Roles[0987654321]}
# authorizes:
#    members with both 'Admin' and 'Mod' roles in server1
#    members with role.id 0987654321
class discordRebot.members.RolesMetaClass[source]

Bases: type

For roles without guild_id. Roles[x] <==> Roles(None)[x]

this is useful for authorize some specific roles irrespective of server

Example:

# To authorize Admin roles in all servers
# roles = ('Admin',) and guild_id = None
Fn.auth = Roles['Admin']
class discordRebot.members.Permissions(guild_id=None, permissions=0, **kwargs)[source]

Bases: discord.permissions.Permissions

To group members with their permissions in a server.

This can’t be used to Authorize in DM channel.

Example:

server1 = Permissions(1234567890) # guild_id of server1
Fn.auth = {server1['manage_guild', 'manage_roles'],
           Permissions(0987654321, administrator=True)}
# authorizes:
#    members with both ManageServer and ManageRoles permissions in server1
#    members with Administrator permission in server with guild_id 0987654321
class discordRebot.members.PermissionsMetaClass[source]

Bases: type

For permissions without guild_id. Permissions[x] <==> Permissions(None)[x]

this is useful for check some permissions irrespective of server

Example:

# To authorize members with Administrator permission in all servers
# discord.permissions.Permissions(administrator=True) and guild_id = None
Fn.auth = Permissions['administrator']
class discordRebot.members.Members(groups)[source]

Bases: object

To group members for authorization

Example:

level = Members([{'root#0000','admin#1000'}, {'user1#1001'}])
def privileged_cmd(msg, *args):
    #code#
privileged_cmd.auth = level[0]
Parameters

groups (Union[Sequence[Set[Union[int, str, Pattern, Roles, Permissions]]], Mapping[Hashable, Set[Union[int, str, Pattern, Roles, Permissions]]]]) – Sequence or Mapping of Set of MemberIdentity

property members

To return all the members

Return type

Set[Union[int, str, Pattern, Roles, Permissions]]

class discordRebot.members.MembersSet(Groups, *MethodCalls)[source]

Bases: object

Set of members subscripted from Members.

It is alos possible to do set operations.

Example:

group = Members({'Admin': {'admin1#0001', 'admin2#0002'},
                 'users': {'user1#1001', 1234567890}})
def privileged_cmd(msg, *args):
    #code#
privileged_cmd.auth = group['Admin']|{'root#0000'}
def cmd(msg, *args):
    #code#
cmd.auth = group # <==> group['Admin']|group['users']
Parameters
  • MemberGroups – Members instance for groups

  • MethodCalls – previous method calls

property members

members set with all operations done.

All the operations done to Members is only executed at the time of getattr(MembersObj, "members")

So, no need to update auth of each callbacks after updating the group.

Example:

group = Members([{'root#0000'}])
Admin = group[0]|{'admin1#0001'}
print(Admin.members) #{'root#0000', 'admin1#0001'}
group.Groups[0] = {'admin2#0002'}
print(Admin.members) #{'admin1#0001', 'admin2#0002'}
Return type

Set[Union[int, str, Pattern, Roles, Permissions]]

discordRebot.converter module

class discordRebot.converter.Converter(bot)[source]

Bases: object

To convert string to actual discord.* class objects

Example:

Convert = Converter(bot=client)
member1 = await Convert(msg, 'nickname', discord.Member)
assert isinstance(member1, discord.Member)

client: discord.Client and msg: discord.Message is for lookup purpose

Parameters

bot (Union[Client, Bot]) – for creating ctx

async __call__(msg, from_, to)[source]

To convert

Parameters
  • msg (NewType()(Message, Message)) – for _create_ctx()

  • from – from which we have to convert

  • to (Any) – to which we have to convert

Returns

converted obj`to`

Module contents

RegEx based command mapping discord BOT framework with authorization

discordRebot:
__init__.py
__main__.py
manager.py # Main module
members.py # has Roles, Permissions and Members classes
converter.py # has Converter
__all__:
discord : discord
re : re
RE_MARKDOWN : re.Pattern to escape MD
ZWS : '\u200B'
Tested on:
python v3.8.2-final
discord.py v1.3.3-final
re 2.2.1
Author

Naveen S R

Maintainers
Naveen S R (github: nkpro2000sr)