Default role update with new member default role and default role, also automatic switch when use reaction role
This commit is contained in:
parent
ffe63c87b9
commit
6e1983a8c0
1 changed files with 21 additions and 11 deletions
30
bot.py
30
bot.py
|
@ -40,7 +40,7 @@ async def on_member_join(member):
|
||||||
If available, add a default role to new members of guild"""
|
If available, add a default role to new members of guild"""
|
||||||
with shelve.open("config.conf") as conf:
|
with shelve.open("config.conf") as conf:
|
||||||
if "default_role_id" in conf[str(member.guild.id)] and conf[str(member.guild.id)]["default_role_id"]:
|
if "default_role_id" in conf[str(member.guild.id)] and conf[str(member.guild.id)]["default_role_id"]:
|
||||||
role = member.guild.get_role(conf[str(member.guild.id)]["default_role_id"]) # Get the role from guild
|
role = member.guild.get_role(conf[str(member.guild.id)]["default_role_id"][0]) # Get the role from guild
|
||||||
await member.add_roles(role)
|
await member.add_roles(role)
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,6 +73,13 @@ async def reaction_role(message_id, guild, emoji, member, state):
|
||||||
message_id in conf[str(guild.id)]["reaction_messages"] and \
|
message_id in conf[str(guild.id)]["reaction_messages"] and \
|
||||||
emoji in conf[str(guild.id)]["reaction_messages"][message_id]:
|
emoji in conf[str(guild.id)]["reaction_messages"][message_id]:
|
||||||
|
|
||||||
|
# If member has default new role, give him default role
|
||||||
|
def_new_role = guild.get_role(conf[str(guild.id)]["default_role_id"][0])
|
||||||
|
if def_new_role in member.roles:
|
||||||
|
def_role = guild.get_role(conf[str(guild.id)]["default_role_id"][1])
|
||||||
|
await member.remove_roles(def_new_role)
|
||||||
|
await member.add_roles(def_role)
|
||||||
|
|
||||||
role = guild.get_role(conf[str(guild.id)]["reaction_messages"][message_id][emoji]) # Get the target role
|
role = guild.get_role(conf[str(guild.id)]["reaction_messages"][message_id][emoji]) # Get the target role
|
||||||
|
|
||||||
# State-dependent action
|
# State-dependent action
|
||||||
|
@ -86,8 +93,9 @@ async def reaction_role(message_id, guild, emoji, member, state):
|
||||||
@commands.has_permissions(administrator=True)
|
@commands.has_permissions(administrator=True)
|
||||||
async def help_cmd(ctx):
|
async def help_cmd(ctx):
|
||||||
embed = Embed(title="Help", description="", color=0xffff00)
|
embed = Embed(title="Help", description="", color=0xffff00)
|
||||||
embed.add_field(name="Set default role", value="``set_default_role @role`` to set new member default role, if any "
|
embed.add_field(name="Set default role", value="``set_default_roles @new_default @default`` to set new member "
|
||||||
"role mentioned this disable the option")
|
"default role and default role, if any role mentioned this disable "
|
||||||
|
"the option")
|
||||||
embed.add_field(name="Default role", value="``default_role`` show the new member default role")
|
embed.add_field(name="Default role", value="``default_role`` show the new member default role")
|
||||||
embed.add_field(name="Reaction message", value="""``reaction_message <action> <message id> <...>``
|
embed.add_field(name="Reaction message", value="""``reaction_message <action> <message id> <...>``
|
||||||
**__actions :__**
|
**__actions :__**
|
||||||
|
@ -109,13 +117,15 @@ async def help_cmd_error(ctx, error):
|
||||||
@bot.command()
|
@bot.command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
@commands.has_permissions(administrator=True)
|
@commands.has_permissions(administrator=True)
|
||||||
async def set_default_role(ctx):
|
async def set_default_roles(ctx):
|
||||||
"""ctx: context object
|
"""ctx: context object
|
||||||
Set the default role of a guild"""
|
Set the default role of a guild"""
|
||||||
with shelve.open("config.conf", writeback=True) as conf:
|
with shelve.open("config.conf", writeback=True) as conf:
|
||||||
if len(ctx.message.role_mentions) == 1: # Accept only one mentioned role
|
if len(ctx.message.role_mentions) == 2: # Accept only two mentioned role
|
||||||
conf[str(ctx.guild.id)]["default_role_id"] = ctx.message.role_mentions[0].id # Set in configuration
|
conf[str(ctx.guild.id)]["default_role_id"] = [ctx.message.role_mentions[0].id,
|
||||||
await ctx.send(f"Role ``{ctx.message.role_mentions[0].name}`` set to default role :white_check_mark:")
|
ctx.message.role_mentions[1].id] # Set in configuration
|
||||||
|
await ctx.send(f"Roles ``{ctx.message.role_mentions[0].name}`` and ``{ctx.message.role_mentions[1].name}``"
|
||||||
|
f" set to default role :white_check_mark:")
|
||||||
elif len(ctx.message.role_mentions) == 0: # If any role, disable default role option
|
elif len(ctx.message.role_mentions) == 0: # If any role, disable default role option
|
||||||
conf[str(ctx.guild.id)]["default_role_id"] = "" # Set in configuration
|
conf[str(ctx.guild.id)]["default_role_id"] = "" # Set in configuration
|
||||||
await ctx.send("Default role disabled :warning:")
|
await ctx.send("Default role disabled :warning:")
|
||||||
|
@ -123,10 +133,10 @@ async def set_default_role(ctx):
|
||||||
raise commands.BadArgument
|
raise commands.BadArgument
|
||||||
|
|
||||||
|
|
||||||
@set_default_role.error
|
@set_default_roles.error
|
||||||
async def set_default_role_error(ctx, error):
|
async def set_default_roles_error(ctx, error):
|
||||||
"""ctx: context object, error: raised error
|
"""ctx: context object, error: raised error
|
||||||
Manage set_default_role command errors"""
|
Manage set_default_roles command errors"""
|
||||||
if isinstance(error, commands.BadArgument):
|
if isinstance(error, commands.BadArgument):
|
||||||
await ctx.send("Invalid mentioned role ! :x:")
|
await ctx.send("Invalid mentioned role ! :x:")
|
||||||
if isinstance(error, commands.errors.MissingPermissions):
|
if isinstance(error, commands.errors.MissingPermissions):
|
||||||
|
|
Loading…
Reference in a new issue