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:
objectTo 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)
Mapperobjectif Pattern matchs with message.content then callback is called with message, captured groupsif Str is equal to message.content then callback is called with messageIn 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 memberselse for each command only members authorized byAuthorizeCallBack()is allowded.escMD (
bool) –escape MarkDown while reply
if true reply (returned by callback) is filtered to escape MarkDownelse 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_messagefilter (
Optional[Callable[[NewType()(Message,Message)],bool]]) –function to filter messages
if filter(message) is True then proceed on_messageelse 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 bydiscordRebot.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 messageresponse (
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 unauthorizedmessage (
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:
objectTo 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 membersmessage (
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 membermessage (
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:
objectTo 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:
typeFor 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.PermissionsTo 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:
typeFor 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:
objectTo 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:
objectSet 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
Membersis only executed at the time ofgetattr(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:
objectTo 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.Clientand msg:discord.Messageis for lookup purpose- Parameters
bot (
Union[Client,Bot]) – for creating ctx
Module contents¶
RegEx based command mapping discord BOT framework with authorization
- discordRebot:
- __init__.py__main__.pymanager.py # Main modulemembers.py # has Roles, Permissions and Members classesconverter.py # has Converter
- __all__:
- discord : discordre : reManager :
discordRebot.manager.ManagerMapper :discordRebot.manager.MapperConverter :discordRebot.converter.ConverterMembers :discordRebot.members.MembersRoles :discordRebot.members.RolesPermissions :discordRebot.members.PermissionsAuthorize :discordRebot.manager.Authorize()AuthorizeCallBack :discordRebot.manager.AuthorizeCallBack()RE_MARKDOWN :re.Patternto escape MDZWS :'\u200B' - Tested on:
- python v3.8.2-finaldiscord.py v1.3.3-finalre 2.2.1
- Author
Naveen S R
- Maintainers
- Naveen S R (github: nkpro2000sr)