Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code from Oct 26, 2019
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
import sqlite3
conn = sqlite3.connect('.\customroles.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS "custom_role"
(server_id INTEGER NOT NULL, user TEXT NOT NULL, user_id INTEGER NOT NULL, role TEXT NOT NULL, role_id INTEGER NOT NULL)''')
conn.commit()
class CustomRoles(commands.Cog, name="Custom Roles"):
def __init__(self, bot):
self.bot = bot
def connect(self):
self.conn = sqlite3.connect('.\customroles.db')
self.c = self.conn.cursor()
def disconnect(self):
self.c.close()
self.conn.close()
def query_role_server(self, server, discord_id):
self.c.execute("SELECT role_id FROM custom_role WHERE server_id = ? AND user_id = ?", (server, discord_id,))
self.existing_role = ', '.join([str(custom_role[0]) for custom_role in self.c])
return
@commands.group()
@commands.guild_only()
async def customrole(self, ctx):
if ctx.invoked_subcommand is None:
embed = discord.Embed(title="Custom Role Command Info", color=0x00ff40)
embed.add_field(name="**Command**: !customrole give (@user) (role name)",
value="**Description**: To give a user their own custom role that they can edit.",
inline=False)
embed.add_field(name="**Command**: !customrole name (new name for role)",
value="**Description**: To edit the name of the role given to you. (can only be used by the role owner)",
inline=False)
embed.add_field(name="**Command**: !customrole colour (hex colour code | do not add the #!)",
value="**Description**: To edit the colour of the role given to you. (can only be used by the role owner)",
inline=False)
await ctx.send(embed=embed)
@customrole.command()
@commands.guild_only()
@has_permissions(manage_roles=True)
async def give(self, ctx, user: discord.Member, *, name: str):
self.connect()
self.query_role_server(ctx.guild.id, user.id)
if self.existing_role != "":
await ctx.send(f"{user} already has a custom role, if you feel like this is a mistake contact <@590794167362388011>")
else:
guild = ctx.guild
await guild.create_role(name=name)
role = discord.utils.get(ctx.guild.roles, name=name)
await user.add_roles(role)
await ctx.send(f"Successfully gave {user.mention} the role {role.mention}")
c.execute(f"INSERT INTO custom_role VALUES ('{guild.id}','{user}','{user.id}','{role}','{role.id}')")
conn.commit()
self.disconnect()
@customrole.command()
@commands.guild_only()
@has_permissions(manage_roles=True)
async def giveexisting(self, ctx, user: discord.Member, *, name: str):
self.connect()
self.query_role_server(ctx.guild.id, user.id)
if self.existing_role != "":
await ctx.send(
f"{user} already has a custom role, if you feel like this is a mistake contact <@590794167362388011>")
else:
role = discord.utils.get(ctx.guild.roles, name=name)
await user.add_roles(role)
await ctx.send(f"Successfully gave {user.mention} the role {role.mention}")
c.execute(f"INSERT INTO custom_role VALUES ('{ctx.guild.id}','{user}','{user.id}','{role}','{role.id}')")
conn.commit()
self.disconnect()
@customrole.command()
@commands.guild_only()
async def name(self, ctx, *, newname):
self.connect()
self.query_role_server(ctx.guild.id, ctx.message.author.id)
if self.existing_role == "":
await ctx.send(f"Sorry {ctx.message.author.mention} but it seems that you have not been given a custom role!")
else:
role = ctx.guild.get_role(int(self.existing_role))
try:
await role.edit(name=newname)
await ctx.send("Done.")
except discord.Forbidden:
await ctx.send("I need permissions to manage roles first.")
except Exception as e:
print(e)
await ctx.send("Something went wrong.")
self.disconnect()
@customrole.command()
@commands.guild_only()
async def colour(self, ctx, value: discord.Colour):
self.connect()
self.query_role_server(ctx.guild.id, ctx.message.author.id)
if self.existing_role == "":
await ctx.send(f"Sorry {ctx.message.author.mention} but it seems that you have not been given a custom role!")
else:
role = ctx.guild.get_role(int(self.existing_role))
try:
await role.edit(color=value)
await ctx.send("Done.")
except discord.Forbidden:
await ctx.send("I need permissions to manage roles first.")
except Exception as e:
print(e)
await ctx.send("Something went wrong.")
self.disconnect()
@customrole.command()
@commands.guild_only()
async def remove(self, ctx, value: discord.Colour):
self.connect()
self.query_role_server(ctx.guild.id, ctx.message.author.id)
if self.existing_role == "":
await ctx.send(
f"Sorry {ctx.message.author.mention} but it seems that you have not been given a custom role!")
else:
role = ctx.guild.get_role(int(self.existing_role))
try:
c.execute(f"DELETE FROM custom_role WHERE server_id = ? AND user_id = ?", (ctx.guild.id, user.id))
await user.remove_roles(role)
except discord.Forbidden:
await ctx.send("I need permissions to manage roles first.")
except Exception as e:
print(e)
await ctx.send("Something went wrong.")
self.disconnect()
@customrole.command()
@commands.guild_only()
async def delete(self, ctx, user: discord.Member):
self.connect()
self.query_role_server(ctx.guild.id, ctx.message.author.id)
if self.existing_role == "":
await ctx.send(
f"Sorry {ctx.message.author.mention} but it seems that you have not been given a custom role!")
else:
role = ctx.guild.get_role(int(self.existing_role))
try:
await role.delete()
c.execute(f"DELETE FROM custom_role WHERE server_id = ? AND user_id = ?", (ctx.guild.id, user.id))
except discord.Forbidden:
await ctx.send("I need permissions to manage roles first.")
except Exception as e:
print(e)
await ctx.send("Something went wrong.")
self.disconnect()
def setup(bot):
bot.add_cog(CustomRoles(bot))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.