Use new database for some role commands
This commit is contained in:
parent
e13b74ce38
commit
59e42d8182
109
cogs/roles.py
109
cogs/roles.py
|
@ -23,7 +23,8 @@ class Roles:
|
||||||
|
|
||||||
EXAMPLE: !colour red
|
EXAMPLE: !colour red
|
||||||
RESULT: A role that matches red (#e74c3c) will be given to you"""
|
RESULT: A role that matches red (#e74c3c) will be given to you"""
|
||||||
if not self.bot.db.load('server_settings', key=ctx.guild.id, pluck="colour_roles_allowed"):
|
result = await ctx.bot.db.fetchrow("SELECT colour_roles FROM guilds WHERE id = $1", ctx.guild.id)
|
||||||
|
if result and result["colour_roles"]:
|
||||||
await ctx.send("Colour roles not allowed on this server! "
|
await ctx.send("Colour roles not allowed on this server! "
|
||||||
"The command `allowcolours` must be ran to enable them!")
|
"The command `allowcolours` must be ran to enable them!")
|
||||||
return
|
return
|
||||||
|
@ -392,8 +393,12 @@ class Roles:
|
||||||
return
|
return
|
||||||
|
|
||||||
author = ctx.message.author
|
author = ctx.message.author
|
||||||
key = str(ctx.message.guild.id)
|
result = await ctx.bot.db.fetchrow("SELECT assignable_roles FROM guilds WHERE id = $1", ctx.guild.id)
|
||||||
self_assignable_roles = self.bot.db.load('server_settings', key=key, pluck='self_assignable_roles') or []
|
|
||||||
|
if result is None:
|
||||||
|
await ctx.send("There are no self-assignable roles on this server")
|
||||||
|
return
|
||||||
|
self_assignable_roles = result["assignable_roles"]
|
||||||
|
|
||||||
if len(self_assignable_roles) == 0:
|
if len(self_assignable_roles) == 0:
|
||||||
await ctx.send("There are no self-assignable roles on this server")
|
await ctx.send("There are no self-assignable roles on this server")
|
||||||
|
@ -425,8 +430,12 @@ class Roles:
|
||||||
return
|
return
|
||||||
|
|
||||||
author = ctx.message.author
|
author = ctx.message.author
|
||||||
key = str(ctx.message.guild.id)
|
result = await ctx.bot.db.fetchrow("SELECT assignable_roles FROM guilds WHERE id = $1", ctx.guild.id)
|
||||||
self_assignable_roles = self.bot.db.load('server_settings', key=key, pluck='self_assignable_roles') or []
|
|
||||||
|
if result is None:
|
||||||
|
await ctx.send("There are no self-assignable roles on this server")
|
||||||
|
return
|
||||||
|
self_assignable_roles = result["assignable_roles"]
|
||||||
|
|
||||||
if len(self_assignable_roles) == 0:
|
if len(self_assignable_roles) == 0:
|
||||||
await ctx.send("There are no self-assignable roles on this server")
|
await ctx.send("There are no self-assignable roles on this server")
|
||||||
|
@ -445,96 +454,6 @@ class Roles:
|
||||||
except discord.HTTPException:
|
except discord.HTTPException:
|
||||||
await ctx.send("I cannot remove roles from you {}".format(author.mention))
|
await ctx.send("I cannot remove roles from you {}".format(author.mention))
|
||||||
|
|
||||||
@assign.command(name='add')
|
|
||||||
@commands.guild_only()
|
|
||||||
@utils.can_run(manage_roles=True)
|
|
||||||
async def _add_assigns(self, ctx, *role: discord.Role):
|
|
||||||
"""Adds the provided role(s) to the list of available self-assignable roles
|
|
||||||
|
|
||||||
EXAMPLE: !assigns Member NSFW
|
|
||||||
RESULT: Allows users to self-assign the roles Member, and NSFW"""
|
|
||||||
roles = [str(r.id) for r in role]
|
|
||||||
key = str(ctx.message.guild.id)
|
|
||||||
|
|
||||||
self_assignable_roles = self.bot.db.load('server_settings', key=key, pluck='self_assignable_roles') or []
|
|
||||||
self_assignable_roles.extend(roles)
|
|
||||||
self_assignable_roles = list(set(self_assignable_roles))
|
|
||||||
entry = {
|
|
||||||
'server_id': key,
|
|
||||||
'self_assignable_roles': self_assignable_roles
|
|
||||||
}
|
|
||||||
|
|
||||||
await self.bot.db.save('server_settings', entry)
|
|
||||||
|
|
||||||
if len(roles) == 1:
|
|
||||||
fmt = "Successfully added {} as a self-assignable role".format(role[0].name)
|
|
||||||
else:
|
|
||||||
fmt = "Succesfully added the following roles as self-assignable:\n{}".format(
|
|
||||||
"\n".join(["**{}**".format(r.name) for r in role])
|
|
||||||
)
|
|
||||||
await ctx.send(fmt)
|
|
||||||
|
|
||||||
@assign.command(name='list')
|
|
||||||
@commands.guild_only()
|
|
||||||
@utils.can_run(send_messages=True)
|
|
||||||
async def _list_assigns(self, ctx):
|
|
||||||
"""Lists the roles that can be self-assigned
|
|
||||||
|
|
||||||
EXAMPLE: !assigns list
|
|
||||||
RESUL: A list of all the self-assignable roles"""
|
|
||||||
key = str(ctx.message.guild.id)
|
|
||||||
self_assignable_roles = self.bot.db.load('server_settings', key=key, pluck='self_assignable_roles') or []
|
|
||||||
if len(self_assignable_roles) == 0:
|
|
||||||
await ctx.send("There are no self-assignable roles on this server")
|
|
||||||
return
|
|
||||||
|
|
||||||
roles = []
|
|
||||||
for role_id in self_assignable_roles:
|
|
||||||
role = discord.utils.get(ctx.message.guild.roles, id=int(role_id))
|
|
||||||
if role:
|
|
||||||
roles.append(role.name)
|
|
||||||
|
|
||||||
if len(roles) == 0:
|
|
||||||
await ctx.send("There are no self-assignable roles on this server")
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
pages = utils.Pages(ctx, entries=roles)
|
|
||||||
await pages.paginate()
|
|
||||||
except utils.CannotPaginate as e:
|
|
||||||
await ctx.send(str(e))
|
|
||||||
|
|
||||||
@assign.command(name='remove', aliases=['delete'])
|
|
||||||
@commands.guild_only()
|
|
||||||
@utils.can_run(manage_roles=True)
|
|
||||||
async def _delete_assigns(self, ctx, *role: discord.Role):
|
|
||||||
"""Removes the provided role(s) from the list of available self-assignable roles
|
|
||||||
|
|
||||||
EXAMPLE: !assigns remove Member NSFW
|
|
||||||
RESULT: Removes the ability for users to self-assign the roles Member, and NSFW"""
|
|
||||||
key = str(ctx.message.guild.id)
|
|
||||||
self_assignable_roles = self.bot.db.load('server_settings', key=key, pluck='self_assignable_roles') or []
|
|
||||||
if len(self_assignable_roles) == 0:
|
|
||||||
await ctx.send("There are no self-assignable roles on this server")
|
|
||||||
return
|
|
||||||
|
|
||||||
fmt = ""
|
|
||||||
for r in role:
|
|
||||||
rid = str(r.id)
|
|
||||||
try:
|
|
||||||
self_assignable_roles.remove(rid)
|
|
||||||
except ValueError:
|
|
||||||
fmt += "\n{} is not a self-assignable role".format(r.name)
|
|
||||||
else:
|
|
||||||
fmt += "\n{} is no longer a self-assignable role".format(r.name)
|
|
||||||
|
|
||||||
update = {
|
|
||||||
'self_assignable_roles': self_assignable_roles,
|
|
||||||
'server_id': key
|
|
||||||
}
|
|
||||||
await self.bot.db.save('server_settings', update)
|
|
||||||
await ctx.send(fmt)
|
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
bot.add_cog(Roles(bot))
|
bot.add_cog(Roles(bot))
|
||||||
|
|
Loading…
Reference in a new issue