feat: Add Cog to perform role addition and deletion to a user from predefined config file #3
95
cogs/roles.py
Normal file
95
cogs/roles.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
import discord, os, json
|
||||||
|
from discord import option
|
||||||
|
from discord.ext import commands, bridge
|
||||||
|
from setup.config import Config
|
||||||
|
from setup.logger import LOGGER
|
||||||
|
|
||||||
|
CONFIG_FILE = os.path.join(Config.CONFIG_PATH,"roles.json")
|
||||||
|
|
||||||
|
class RolesCog(commands.Cog):
|
||||||
|
def __init__(self,bot:discord.Bot):
|
||||||
|
self.bot = bot
|
||||||
|
self.load_config()
|
||||||
|
|
||||||
|
def load_config(self):
|
||||||
|
with open(CONFIG_FILE) as file:
|
||||||
|
self.config = json.load(file)
|
||||||
|
|
||||||
|
def save_config(self):
|
||||||
|
with open(CONFIG_FILE,"w") as file:
|
||||||
|
json.dump(self.config,file)
|
||||||
|
|
||||||
|
@bridge.bridge_command()
|
||||||
|
async def roles(self,ctx: discord.ApplicationContext, category:bridge.BridgeOption(name="category",description="The category to manipulate", input_type=str,required=True)):
|
||||||
|
filtered = [config_category for config_category in self.config if config_category["category_id"] == category]
|
||||||
|
if len(filtered):
|
||||||
|
await ctx.defer()
|
||||||
|
config_category = filtered.pop()
|
||||||
|
embed = discord.Embed(
|
||||||
|
title=config_category["category_name"],
|
||||||
|
description=config_category["message_header"],
|
||||||
|
color=discord.Color.blue()
|
||||||
|
)
|
||||||
|
roles = [
|
||||||
|
self.bot.get_guild(897157935006900254).get_role(role_config["role_id"])
|
||||||
|
for role_config in config_category["roles"]
|
||||||
|
]
|
||||||
|
for role_instance,role_config in zip(roles,config_category["roles"]):
|
||||||
|
embed.add_field(name=role_config["reaction"],value=role_instance.mention)
|
||||||
|
message = await ctx.send(embed=embed)
|
||||||
|
if message:
|
||||||
|
config_category["message_id"] = message.id
|
||||||
|
for config_role in config_category["roles"]:
|
||||||
|
await message.add_reaction(config_role["reaction"])
|
||||||
|
self.save_config()
|
||||||
|
await ctx.followup.send("Role message generated",ephemeral=True)
|
||||||
|
|
||||||
|
@commands.Cog.listener()
|
||||||
|
async def on_raw_reaction_add(self, payload:discord.RawReactionActionEvent):
|
||||||
|
if payload.user_id == self.bot.user.id:
|
||||||
|
return
|
||||||
|
|
||||||
|
message_id = payload.message_id
|
||||||
|
filtered = [config_category for config_category in self.config if config_category["message_id"] == message_id]
|
||||||
|
if len(filtered):
|
||||||
|
config_category = filtered.pop()
|
||||||
|
role_ids = [config_role["role_id"] for config_role in config_category["roles"] if config_role["reaction"] == payload.emoji.name]
|
||||||
|
if len(role_ids):
|
||||||
|
role_id = role_ids.pop()
|
||||||
|
|
||||||
|
guild = self.bot.get_guild(payload.guild_id)
|
||||||
|
if guild:
|
||||||
|
role = guild.get_role(role_id)
|
||||||
|
if role:
|
||||||
|
user = guild.get_member(payload.user_id)
|
||||||
|
if user:
|
||||||
|
if "parent_role_id" in config_category.keys():
|
||||||
|
parent_role = guild.get_role(config_category["parent_role_id"])
|
||||||
|
await user.add_roles(parent_role,role)
|
||||||
|
else:
|
||||||
|
await user.add_roles(role)
|
||||||
|
|
||||||
|
@commands.Cog.listener()
|
||||||
|
async def on_raw_reaction_remove(self, payload:discord.RawReactionActionEvent):
|
||||||
|
if payload.user_id == self.bot.user.id:
|
||||||
|
return
|
||||||
|
|
||||||
|
message_id = payload.message_id
|
||||||
|
filtered = [config_category for config_category in self.config if config_category["message_id"] == message_id]
|
||||||
|
if len(filtered):
|
||||||
|
config_category = filtered.pop()
|
||||||
|
role_ids = [config_role["role_id"] for config_role in config_category["roles"] if config_role["reaction"] == payload.emoji.name]
|
||||||
|
if len(role_ids):
|
||||||
|
role_id = role_ids.pop()
|
||||||
|
|
||||||
|
guild = self.bot.get_guild(payload.guild_id)
|
||||||
|
if guild:
|
||||||
|
role = guild.get_role(role_id)
|
||||||
|
if role:
|
||||||
|
user = guild.get_member(payload.user_id)
|
||||||
|
if user:
|
||||||
|
if "parent_role_id" in config_category.keys():
|
||||||
|
parent_role = guild.get_role(config_category["parent_role_id"])
|
||||||
|
await user.remove_roles(parent_role,role)
|
||||||
|
else:
|
||||||
|
await user.remove_roles(role)
|
||||||
Reference in New Issue
Block a user