Browse Source

self assign moved over to the db

tags/v2.2.0
Roxie Gibson 5 years ago
parent
commit
2ea22d4eca
1 changed files with 65 additions and 59 deletions
  1. +65
    -59
      roxbot/cogs/selfassign.py

+ 65
- 59
roxbot/cogs/selfassign.py View File

from discord.ext import commands from discord.ext import commands


import roxbot import roxbot
from roxbot import guild_settings as gs
from roxbot.db import *


class SelfAssignRoles(db.Entity):
role_id = Required(int, unique=True, size=64)
guild_id = Required(int, size=64)


class SelfAssignSingle(db.Entity):
enabled = Required(bool, default=False)
guild_id = Required(int, unique=True, size=64)




class SelfAssign(): class SelfAssign():
"""The SelfAssign cog allows guild's to mark roles as 'self assignable'. This allows users to give themselves these roles and to see all the roles marked as 'self assignable'.""" """The SelfAssign cog allows guild's to mark roles as 'self assignable'. This allows users to give themselves these roles and to see all the roles marked as 'self assignable'."""
def __init__(self, Bot): def __init__(self, Bot):
self.bot = Bot self.bot = Bot
self.settings = {
"self_assign": {
"enabled": 0,
"convert": {"enabled": "bool", "roles": "role"},
"roles": []
}
}
self.autogen_db = SelfAssignSingle


async def on_guild_role_delete(self, role): async def on_guild_role_delete(self, role):
"""Cleans up settings on removal of stored IDs.""" """Cleans up settings on removal of stored IDs."""
settings = gs.get(role.guild)
sa = settings["self_assign"]
for sa_role in sa["roles"]:
if int(sa_role) == role.id:
sa["roles"].remove(role.id)
return settings.update(sa, "self_assign")
with db_session:
query = SelfAssignRoles.get(role_id=role.id)
if query:
query.delete()


@commands.guild_only() @commands.guild_only()
@commands.has_permissions(manage_roles=True) @commands.has_permissions(manage_roles=True)
`;sa enable` `;sa enable`
`;sa add ROLE` `;sa add ROLE`
""" """
settings = roxbot.guild_settings.get(ctx.guild)
self_assign = settings["self_assign"]
setting = setting.lower()


if setting == "enable":
self_assign["enabled"] = 1
await ctx.send("'self_assign' was enabled!")
elif setting == "disable":
self_assign["enabled"] = 0
await ctx.send("'self_assign' was disabled :cry:")
elif setting == "add":
try:
if role.id in self_assign["roles"]:
return await ctx.send("{} is already a self-assignable role.".format(role.name))
self_assign["roles"].append(role.id)
await ctx.send('Role "{}" added'.format(str(role)))
except AttributeError:
raise commands.BadArgument("Could not find that role.")
elif setting == "remove":
try:
if role.id in self_assign["roles"]:
self_assign["roles"].remove(role.id)
await ctx.send('"{}" has been removed from the self-assignable roles.'.format(str(role)))
else:
return await ctx.send("That role was not in the list.")
except AttributeError:
raise commands.BadArgument("Could not find that role.")
else:
return await ctx.send("No valid option given.")
return settings.update(self_assign, "self_assign")
with db_session:
self_assign = SelfAssignSingle.get(guild_id=ctx.guild.id)
if setting == "enable":
self_assign.enabled = True
await ctx.send("'self_assign' was enabled!")
elif setting == "disable":
self_assign.enabled = False
await ctx.send("'self_assign' was disabled :cry:")
elif setting == "add":
try:
SelfAssignRoles(role_id=role.id, guild_id=ctx.guild.id)
await ctx.send('Role "{}" added'.format(str(role)))
except AttributeError:
raise commands.BadArgument("Could not find that role.")
except TransactionIntegrityError:
raise commands.BadArgument("{} is already a self-assignable role.".format(role.name))
elif setting == "remove":
try:
query = SelfAssignRoles.get(role_id=role.id)
if query:
query.delete()
return await ctx.send('"{}" has been removed from the self-assignable roles.'.format(str(role)))
else:
return await ctx.send("That role was not in the list.")
except AttributeError:
raise commands.BadArgument("Could not find that role.")
else:
return await ctx.send("No valid option given.")


@commands.guild_only() @commands.guild_only()
@commands.command(pass_context=True) @commands.command(pass_context=True)
""" """
List's all roles that can be self-assigned on this server. List's all roles that can be self-assigned on this server.
""" """
settings = gs.get(ctx.guild)
paginator = commands.Paginator(prefix="`", suffix="`")
with db_session:
self_assign = SelfAssignSingle.get(guild_id=ctx.guild.id)
roles = select(r for r in SelfAssignRoles if r.guild_id == ctx.guild.id)[:]


if not settings["self_assign"]["enabled"]:
if not self_assign.enabled:
embed = discord.Embed(colour=roxbot.EmbedColours.pink, description="SelfAssignable roles are not enabled on this server") embed = discord.Embed(colour=roxbot.EmbedColours.pink, description="SelfAssignable roles are not enabled on this server")
return await ctx.send(embed=embed) return await ctx.send(embed=embed)


paginator = commands.Paginator(prefix="`", suffix="`")
paginator.add_line("The self-assignable roles for this server are: \n") paginator.add_line("The self-assignable roles for this server are: \n")
for role in settings["self_assign"]["roles"]:
for serverrole in ctx.guild.roles:
if role == serverrole.id:
paginator.add_line("- {}".format(serverrole.name))

for role in roles:
r = ctx.guild.get_role(role.role_id)
if r:
paginator.add_line("- {}".format(r.name))


for page in paginator.pages: for page in paginator.pages:
await ctx.send(page) await ctx.send(page)
Example: Example:
.iam OverwatchPing .iam OverwatchPing
""" """
settings = gs.get(ctx.guild)
with db_session:
self_assign = SelfAssignSingle.get(guild_id=ctx.guild.id)
query = SelfAssignRoles.get(role_id=role.id)


if not settings["self_assign"]["enabled"]:
if not self_assign.enabled:
embed = discord.Embed(colour=roxbot.EmbedColours.pink, description="SelfAssignable roles are not enabled on this server") embed = discord.Embed(colour=roxbot.EmbedColours.pink, description="SelfAssignable roles are not enabled on this server")
return await ctx.send(embed=embed) return await ctx.send(embed=embed)


if role in member.roles: if role in member.roles:
return await ctx.send("You already have that role.") return await ctx.send("You already have that role.")


if role.id in settings["self_assign"]["roles"]:
if query:
await member.add_roles(role, reason="'iam' command triggered.") await member.add_roles(role, reason="'iam' command triggered.")
return await ctx.send("Yay {}! You now have the {} role!".format(member.mention, role.name)) return await ctx.send("Yay {}! You now have the {} role!".format(member.mention, role.name))
else: else:
Example: Example:
.iamn OverwatchPing .iamn OverwatchPing
""" """
settings = gs.get(ctx.guild)
with db_session:
self_assign = SelfAssignSingle.get(guild_id=ctx.guild.id)
query = SelfAssignRoles.get(role_id=role.id)


if not settings["self_assign"]["enabled"]:
if not self_assign.enabled:
embed = discord.Embed(colour=roxbot.EmbedColours.pink, description="SelfAssignable roles are not enabled on this server") embed = discord.Embed(colour=roxbot.EmbedColours.pink, description="SelfAssignable roles are not enabled on this server")
return await ctx.send(embed=embed) return await ctx.send(embed=embed)


member = ctx.author member = ctx.author


if role in member.roles and role.id in settings["self_assign"]["roles"]:
if role in member.roles and query:
await member.remove_roles(role, reason="'iamn' command triggered.") await member.remove_roles(role, reason="'iamn' command triggered.")
return await ctx.send("{} has been successfully removed.".format(role.name)) return await ctx.send("{} has been successfully removed.".format(role.name))
elif role not in member.roles and role.id in settings["self_assign"]["roles"]:
elif role not in member.roles and query:
return await ctx.send("You do not have {}.".format(role.name)) return await ctx.send("You do not have {}.".format(role.name))
else: else:
return await ctx.send("That role is not self-assignable.") return await ctx.send("That role is not self-assignable.")

Loading…
Cancel
Save