GoldyBot.objects.member
1from __future__ import annotations 2import nextcord 3 4from .. import database 5import GoldyBot 6from GoldyBot.objects.slash import Interaction, InteractionToCtx 7 8from . import role 9 10class Member(database.member.Member): 11 """A class representing a discord member in Goldy Bot.""" 12 def __init__(self, ctx, member_id:str|int=None, mention_str:str=None, member_object:nextcord.Member=None): 13 self.ctx = ctx 14 self.member_id_ = member_id 15 self.mention_str_ = mention_str 16 self.member_object_ = member_object 17 18 if self.member_object_ == None: 19 # Find the member. 20 self.member = self.find_member(self.member_id) 21 else: 22 self.member = self.member_object_ 23 24 super().__init__(ctx, self) 25 26 @property 27 def member_id(self) -> str: 28 """Returns id of discord member. Defaults to ctx author if ``member_id``, ``mention_str`` and ``member_user_object`` are None.""" 29 if not self.member_id_ == None: return str(self.member_id_) 30 if not self.mention_str_ == None: return self.mention_str_[self.mention_str_.index("@") + 1: self.mention_str_.index(">")] # Getting id from mention (e.g. <@332592361307897856>). 31 if not self.member_object_ == None: return str(self.member_object_.id) 32 else: 33 if isinstance(self.ctx, Interaction): 34 return str(InteractionToCtx(self.ctx).author.id) 35 else: 36 return str(self.ctx.author.id) 37 38 @property 39 def name(self): 40 """Returns the discord name of member including tag. Does not return server nickname!""" 41 return self.member.name 42 43 @property 44 def display_name(self): 45 """Returns display name of member, so server nickname.""" 46 return self.member.display_name 47 48 # Roles 49 #--------- 50 def has_role(self, role:role.Role): 51 """Checks if the member has a certain role.""" 52 if role.role in self.member.roles: 53 return True 54 else: 55 return False 56 57 async def add_role(self, role:role.Role): 58 """This method adds the specified role to this member.""" 59 if not self.has_role(role): 60 await self.member.add_roles(role) 61 return True 62 else: 63 return True 64 65 async def remove_role(self, role:role.Role): 66 """This method removes the specified role from this member.""" 67 if self.has_role(role): 68 await self.member.remove_roles(role) 69 return True 70 else: 71 return None 72 73 def find_member(self, member_id:int|str) -> nextcord.Member | None: 74 """Finds the damn member!""" 75 76 if not member_id == None: 77 member = nextcord.utils.get(self.ctx.guild.members, id=int(member_id)) 78 79 if member is None: raise GoldyBot.errors.FailedToFindMember() 80 81 return member 82 else: 83 return None 84 85 # Utils 86 async def send(self, **args): 87 args.pop("ephemeral") 88 await self.member.send(**args)
11class Member(database.member.Member): 12 """A class representing a discord member in Goldy Bot.""" 13 def __init__(self, ctx, member_id:str|int=None, mention_str:str=None, member_object:nextcord.Member=None): 14 self.ctx = ctx 15 self.member_id_ = member_id 16 self.mention_str_ = mention_str 17 self.member_object_ = member_object 18 19 if self.member_object_ == None: 20 # Find the member. 21 self.member = self.find_member(self.member_id) 22 else: 23 self.member = self.member_object_ 24 25 super().__init__(ctx, self) 26 27 @property 28 def member_id(self) -> str: 29 """Returns id of discord member. Defaults to ctx author if ``member_id``, ``mention_str`` and ``member_user_object`` are None.""" 30 if not self.member_id_ == None: return str(self.member_id_) 31 if not self.mention_str_ == None: return self.mention_str_[self.mention_str_.index("@") + 1: self.mention_str_.index(">")] # Getting id from mention (e.g. <@332592361307897856>). 32 if not self.member_object_ == None: return str(self.member_object_.id) 33 else: 34 if isinstance(self.ctx, Interaction): 35 return str(InteractionToCtx(self.ctx).author.id) 36 else: 37 return str(self.ctx.author.id) 38 39 @property 40 def name(self): 41 """Returns the discord name of member including tag. Does not return server nickname!""" 42 return self.member.name 43 44 @property 45 def display_name(self): 46 """Returns display name of member, so server nickname.""" 47 return self.member.display_name 48 49 # Roles 50 #--------- 51 def has_role(self, role:role.Role): 52 """Checks if the member has a certain role.""" 53 if role.role in self.member.roles: 54 return True 55 else: 56 return False 57 58 async def add_role(self, role:role.Role): 59 """This method adds the specified role to this member.""" 60 if not self.has_role(role): 61 await self.member.add_roles(role) 62 return True 63 else: 64 return True 65 66 async def remove_role(self, role:role.Role): 67 """This method removes the specified role from this member.""" 68 if self.has_role(role): 69 await self.member.remove_roles(role) 70 return True 71 else: 72 return None 73 74 def find_member(self, member_id:int|str) -> nextcord.Member | None: 75 """Finds the damn member!""" 76 77 if not member_id == None: 78 member = nextcord.utils.get(self.ctx.guild.members, id=int(member_id)) 79 80 if member is None: raise GoldyBot.errors.FailedToFindMember() 81 82 return member 83 else: 84 return None 85 86 # Utils 87 async def send(self, **args): 88 args.pop("ephemeral") 89 await self.member.send(**args)
A class representing a discord member in Goldy Bot.
Member( ctx, member_id: str | int = None, mention_str: str = None, member_object: nextcord.member.Member = None)
13 def __init__(self, ctx, member_id:str|int=None, mention_str:str=None, member_object:nextcord.Member=None): 14 self.ctx = ctx 15 self.member_id_ = member_id 16 self.mention_str_ = mention_str 17 self.member_object_ = member_object 18 19 if self.member_object_ == None: 20 # Find the member. 21 self.member = self.find_member(self.member_id) 22 else: 23 self.member = self.member_object_ 24 25 super().__init__(ctx, self)
member_id: str
Returns id of discord member. Defaults to ctx author if member_id
, mention_str
and member_user_object
are None.
51 def has_role(self, role:role.Role): 52 """Checks if the member has a certain role.""" 53 if role.role in self.member.roles: 54 return True 55 else: 56 return False
Checks if the member has a certain role.
58 async def add_role(self, role:role.Role): 59 """This method adds the specified role to this member.""" 60 if not self.has_role(role): 61 await self.member.add_roles(role) 62 return True 63 else: 64 return True
This method adds the specified role to this member.
66 async def remove_role(self, role:role.Role): 67 """This method removes the specified role from this member.""" 68 if self.has_role(role): 69 await self.member.remove_roles(role) 70 return True 71 else: 72 return None
This method removes the specified role from this member.
def
find_member(self, member_id: int | str) -> nextcord.member.Member | None:
74 def find_member(self, member_id:int|str) -> nextcord.Member | None: 75 """Finds the damn member!""" 76 77 if not member_id == None: 78 member = nextcord.utils.get(self.ctx.guild.members, id=int(member_id)) 79 80 if member is None: raise GoldyBot.errors.FailedToFindMember() 81 82 return member 83 else: 84 return None
Finds the damn member!