blob/commands/role.py
2025-01-25 16:31:29 -05:00

68 lines
3.0 KiB
Python

import discord
import pymongo
import os
from discord import app_commands
from discord.ext import commands
client = pymongo.MongoClient(os.getenv("mongo_url"))
db = client.roles
coll = db.guild
class Role(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
print("Role Online")
role = app_commands.Group(name="role", description="Manage roles in your server!")
@role.command(name="give", description="Gives a user a role")
@commands.has_permissions(administrator=True)
async def give(self, interaction: discord.Interaction, user: discord.Member, role: discord.Role):
await user.add_roles(role)
await interaction.response.send_message(f"Gave {user.mention} the {role.name} role", ephemeral=True)
@role.command(name="join", description="Select the role you want to give to a user when they join the server")
@commands.has_permissions(manage_roles=True)
async def join(self, interaction: discord.Interaction, role: discord.Role):
guild = coll.find_one({"_id": interaction.guild.id})
if guild is None:
coll.insert_one({"_id": interaction.guild.id, "join_role": role.id})
else:
coll.update_one({"_id": interaction.guild.id}, {"$set": {"join_role": role.id}})
await interaction.response.send_message(f"Set the join role to {role.mention}")
@role.command(name="remove", description="Remove a role from a user")
@commands.has_permissions(administrator=True)
async def remove(self, interaction: discord.Interaction, user: discord.Member, role: discord.Role):
await user.remove_roles(role)
await interaction.response.send_message(f"Removed {role} from {user.display_name}")
@role.command(name="info", description="Get info about a role")
async def info(self, interaction: discord.Interaction, role: discord.Role):
embed = discord.Embed(title=role.name, color=role.color)
embed.add_field(name="ID", value=role.id, inline=False)
embed.add_field(name="Color", value=role.color, inline=False)
embed.add_field(name="Position", value=role.position, inline=False)
embed.add_field(name="Mentionable", value=role.mentionable, inline=False)
embed.add_field(name="Hoisted", value=role.hoist, inline=False)
embed.add_field(name="Managed", value=role.managed, inline=False)
embed.add_field(name="Created At", value=role.created_at.strftime("%a, %#d %B %Y, %I:%M %p UTC"),
inline=False)
embed.add_field(name="Members", value=len(role.members), inline=False)
embed.set_thumbnail(url=role.guild.icon)
await interaction.response.send_message(embed=embed)
@commands.Cog.listener()
async def on_member_join(self, member):
role = coll.find_one({"_id": member.guild.id})
if role:
role = member.guild.get_role(role["join_role"])
await member.add_roles(role)
async def setup(bot):
await bot.add_cog(Role(bot))