[bot-base] Get-role en plus joli pour permettre de faire des recherche de roles avec plus de critères
This commit is contained in:
parent
cc4704c90c
commit
b5947176a8
35
main.py
35
main.py
@ -400,32 +400,29 @@ class ClientById:
|
||||
channel = self.client.get_channel(id_)
|
||||
return channel.send(*args, **kwargs)
|
||||
|
||||
async def get_role(self, id_=None, name=None, guild=None, case_sensitive=True):
|
||||
guilds = self.client.guilds
|
||||
if guild is not None:
|
||||
guilds = [guild]
|
||||
async def get_role(self, id_=None, name=None, check=None, guilds=None):
|
||||
"""Get role by id or with custom check"""
|
||||
if guilds is None:
|
||||
guilds = self.client.guilds
|
||||
if id_ is not None:
|
||||
for guild in guilds:
|
||||
role = discord.utils.get(guild.roles, id=id_)
|
||||
if role:
|
||||
return role
|
||||
if name is not None:
|
||||
if case_sensitive:
|
||||
for guild in guilds:
|
||||
role = discord.utils.get(guild.roles, name=name)
|
||||
if role:
|
||||
return role
|
||||
else:
|
||||
name = name.lower()
|
||||
role = None
|
||||
|
||||
for guild in guilds:
|
||||
for role_ in guild.roles:
|
||||
if role_.name.lower() == name:
|
||||
role = role_
|
||||
break
|
||||
if role is not None:
|
||||
for guild in guilds:
|
||||
role = discord.utils.get(guild.roles, name=name)
|
||||
if role:
|
||||
return role
|
||||
if check is not None:
|
||||
role = None
|
||||
for guild in guilds:
|
||||
for role_ in guild.roles:
|
||||
if check(role_):
|
||||
role = role_
|
||||
break
|
||||
if role is not None:
|
||||
break
|
||||
return role
|
||||
return None
|
||||
|
||||
|
@ -19,12 +19,13 @@ class MainClass(BaseClassPython):
|
||||
for i, member in enumerate(guild.members):
|
||||
if len(member.roles) == 1:
|
||||
await member.add_roles(await self.client.id.get_role(id_=self.config.new_role,
|
||||
guild=self.client.config.main_guild))
|
||||
guilds=[self.client.get_guild(
|
||||
self.client.config.main_guild)]))
|
||||
if i % 50 == 0:
|
||||
self.client.log(f"Attribution des roles automatique manqués... {i}/{len(guild.members)}")
|
||||
|
||||
async def on_member_join(self, member):
|
||||
await member.add_roles(await self.client.id.get_role(id_=self.config.new_role,
|
||||
guild=self.client.get_guild(
|
||||
self.client.config.main_guild)))
|
||||
guilds=[self.client.get_guild(
|
||||
self.client.config.main_guild)]))
|
||||
await member.send(self.config.motd)
|
||||
|
@ -26,11 +26,11 @@ class MainClass(BaseClassPython):
|
||||
return
|
||||
if message.channel.id == self.config.listen_chan:
|
||||
if message.content.lower() in self.config.passwords:
|
||||
new_role = await self.client.id.get_role(id_=self.config.new_role, guild=message.channel.guild)
|
||||
new_role = await self.client.id.get_role(id_=self.config.new_role, guilds=[message.channel.guild])
|
||||
if new_role in message.author.roles:
|
||||
await message.author.remove_roles(new_role)
|
||||
await message.author.add_roles(await self.client.id.get_role(id_=self.config.accepted_role,
|
||||
guild=message.channel.guild))
|
||||
guild=[message.channel.guild]))
|
||||
await message.author.send(self.config.succes_pm)
|
||||
await message.channel.guild.get_channel(self.config.log_chan).send(
|
||||
self.config.succes.format(user=message.author.mention))
|
||||
|
@ -23,7 +23,7 @@ class MainClass(BaseClassPython):
|
||||
async def com_list(self, message, args, kwargs):
|
||||
response = discord.Embed(title="Roles disponibles", color=self.config.color)
|
||||
for id_ in self.config.roles.keys():
|
||||
role = message.guild.get_role(int(id_))
|
||||
role = message.guild.get_role(id_=int(id_))
|
||||
if role is not None:
|
||||
response.add_field(name=role.name, value=f"-> `{self.config.roles[id_]}`", inline=True)
|
||||
await message.channel.send(embed=response)
|
||||
@ -34,7 +34,8 @@ class MainClass(BaseClassPython):
|
||||
if len(args) <= 1:
|
||||
await message.channel.send("Il manque des arguments à la commande")
|
||||
for role_ in args[1:]:
|
||||
role = await self.client.id.get_role(name=role_, guild=guild, case_sensitive=False)
|
||||
role = await self.client.id.get_role(name=role_, guilds=[guild],
|
||||
check=lambda x: x.name.lower() == role_.lower())
|
||||
if role is None or str(role.id) not in self.config.roles.keys():
|
||||
await message.channel.send(f"Le role {role_} n'est pas disponible.")
|
||||
else:
|
||||
@ -46,7 +47,8 @@ class MainClass(BaseClassPython):
|
||||
if len(args) <= 1:
|
||||
await message.channel.send("Il manque des arguments à la commande")
|
||||
for role_ in args[1:]:
|
||||
role = await self.client.id.get_role(name=role_, guild=guild, case_sensitive=False)
|
||||
role = await self.client.id.get_role(name=role_, guilds=[guild],
|
||||
check=lambda x: x.name.lower() == role_.lower())
|
||||
if role is None or str(role.id) not in self.config.roles.keys():
|
||||
await message.channel.send(f"Le role {role_} n'est pas disponible.")
|
||||
else:
|
||||
@ -58,7 +60,8 @@ class MainClass(BaseClassPython):
|
||||
if len(args) <= 1:
|
||||
await message.channel.send("Il manque des arguments à la commande")
|
||||
for role_ in args[1:]:
|
||||
role = await self.client.id.get_role(name=role_, guild=guild, case_sensitive=False)
|
||||
role = await self.client.id.get_role(name=role_, guilds=[guild],
|
||||
check=lambda x: x.name.lower() == role_.lower())
|
||||
if role is None or str(role.id) not in self.config.roles.keys():
|
||||
await message.channel.send(f"Le role {role_} n'est pas disponible.")
|
||||
else:
|
||||
@ -70,7 +73,8 @@ class MainClass(BaseClassPython):
|
||||
if len(args) < 1:
|
||||
await message.channel.send("Il manque des arguments à la commande")
|
||||
for role_ in args:
|
||||
role = await self.client.id.get_role(name=role_, guild=guild, case_sensitive=False)
|
||||
role = await self.client.id.get_role(name=role_, guilds=[guild],
|
||||
check=lambda x: x.name.lower() == role_.lower())
|
||||
if role is None or str(role.id) not in self.config.roles.keys():
|
||||
await message.channel.send(f"Le role {role_} n'est pas disponible.")
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user