GalaxyCogs/forums/forums.py

156 lines
7.9 KiB
Python
Raw Normal View History

import discord
from discord import ui
from redbot.core import Config, checks, commands
class Forums(commands.Cog):
"""Custom cog intended for use on the Galaxy discord server.
Developed by SeaswimmerTheFsh."""
def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(self, identifier=2352711325)
self.config.register_guild(
2023-09-07 18:30:57 -04:00
request_roles=[],
forum_channel="",
forum_tag=""
)
@commands.command()
async def resolved(self, ctx: commands.Context, reason: str = None):
"""Marks a thread as resolved."""
if isinstance(ctx.channel, discord.Thread) and ctx.channel.parent_id == await self.config.guild(ctx.guild).forum_channel():
match = any(role_id in ctx.author.roles for role_id in await self.config.guild(ctx.guild).request_roles())
passed_info = {
"ctx": ctx,
"match": match
}
if match and reason:
passed_info.update({"reason": reason})
if match or ctx.author.id == ctx.channel.owner.id:
msg = await ctx.send("Are you sure you'd like to mark this thread as resolved?")
passed_info.update({"msg": msg})
await msg.edit(view=self.ResolvedButtons(timeout=180, passed_info=passed_info))
else:
await ctx.message.add_reaction("")
class ResolvedButtons(ui.View):
def __init__(self, timeout, passed_info: dict):
super().__init__()
self.timeout = timeout
self.ctx: commands.Context = passed_info['ctx']
self.match: bool = passed_info['match']
self.msg: discord.Message = passed_info['msg']
if 'reason' in passed_info:
self.reason: str = passed_info['reason']
self.config = Config.get_conf(None, cog_name='Forums', identifier=2352711325)
@ui.button(label="Yes", style=discord.ButtonStyle.success, emoji="")
async def resolved_button_yes(self, interaction: discord.Interaction, button: ui.Button):
match = any(role_id in interaction.user.roles for role_id in await self.config.guild(interaction.guild).request_roles())
if match or interaction.user.id == interaction.channel.owner.id:
await interaction.response.defer()
if self.reason:
response_reason = f"Thread closed by {interaction.user.mention} with reason: {self.reason}"
reason = f"Thread closed by {interaction.user.name} ({interaction.user.id}) with reason: {self.reason}"
else:
response_reason = f"Thread closed by {interaction.user.mention}"
reason = f"Thread closed by {interaction.user.name} ({interaction.user.id})"
await interaction.channel.edit(locked=True, archived=True, applied_tags=interaction.channel.applied_tags + await self.config.guild(interaction.guild).forum_tag(), reason=reason)
await self.msg.edit(content=response_reason, view=None)
await self.ctx.message.add_reaction("")
else:
await interaction.response.send_message(content="You cannot close this thread!", ephemeral=True)
@ui.button(label="No", style=discord.ButtonStyle.danger, emoji="✖️")
async def resolved_button_no(self, interaction: discord.Interaction, button: ui.Button):
match = any(role_id in interaction.user.roles for role_id in await self.config.guild(interaction.guild).request_roles())
if match or interaction.user.id == interaction.channel.owner.id:
await interaction.response.defer()
await self.msg.delete()
await self.ctx.message.delete()
else:
await interaction.response.send_message(content="You cannot close this thread!", ephemeral=True)
@commands.group(name='forumsconfig', invoke_without_command=True, aliases=['forumconfig'])
@commands.guild_only()
@commands.admin()
async def forumsconfig(self, ctx: commands.Context):
"""Manages the request roles list."""
current_list = await self.config.guild(ctx.guild).request_roles()
already_in_list = []
for role_id in current_list:
role_obj = ctx.guild.get_role(role_id)
if role_obj:
already_in_list.append(role_obj.mention)
if already_in_list:
2023-09-07 18:30:57 -04:00
await ctx.send("Roles already in the request roles list:\n" + "\n".join(already_in_list), allowed_mentions=discord.AllowedMentions(roles=False))
else:
await ctx.send("No roles are currently in the request roles list.")
@forumsconfig.command(name='add')
@commands.guild_only()
@commands.admin()
async def forumsconfig_add(self, ctx: commands.Context, role: discord.Role = None):
"""Adds roles to the request roles list."""
current_list = await self.config.guild(ctx.guild).request_roles()
if role:
if role.id in current_list:
await ctx.send("This role is already in the request roles list!")
return
else:
current_list.append(role.id)
await self.config.guild(ctx.guild).request_roles.set(current_list)
await ctx.send(f"{role.mention} has been added to the request roles list.", allowed_mentions = discord.AllowedMentions(roles=False))
else:
await ctx.send("Please provide a valid role.")
@forumsconfig.command(name='remove')
@commands.guild_only()
@commands.admin()
async def forumsconfig_remove(self, ctx: commands.Context, role: discord.Role = None):
"""Removes roles from the request roles list."""
current_list = await self.config.guild(ctx.guild).request_roles()
if role.id in current_list:
current_list.remove(role.id)
await self.config.guild(ctx.guild).request_roles.set(current_list)
await ctx.send(f"{role.mention} has been removed from the request roles list.", allowed_mentions = discord.AllowedMentions(roles=False))
else:
await ctx.send("Please provide a valid role that exists in the request roles list.")
2023-09-01 22:04:59 -04:00
def create_select_options(self, ctx: commands.Context, data):
options = []
for tag in data:
2023-09-01 22:04:59 -04:00
emoji = ctx.guild.get_emoji(tag.emoji.id) if tag.emoji.id else str(tag.emoji.name)
options.append(discord.SelectOption(label=tag.name, emoji=emoji, description="", value=tag.id))
return options
@forumsconfig.command(name="tag")
async def forumsconfig_tag_set(self, ctx: commands.Context, channel: discord.ForumChannel):
2023-09-01 22:04:59 -04:00
options = self.create_select_options(ctx, channel.available_tags)
tag = channel.get_tag(await self.config.guild(ctx.guild).forum_tag())
msg = await ctx.send(f"Forum tag is currently set to `{str(tag)}`.")
await msg.edit(view=SelectView(msg, options))
@forumsconfig.command(name="channel")
async def forumsconfig_channel_set(self, ctx: commands.Context, channel: discord.ForumChannel):
await self.config.guild(ctx.guild).forum_channel.set(channel.id)
await ctx.send(f"Forum channel has been set to `{channel.name}`.")
class Select(ui.Select):
def __init__(self, message, options):
self.message = message
super().__init__(placeholder="Select an option", max_values=1, min_values=1, options=options)
async def callback(self, interaction: discord.Interaction):
msg: discord.Message = self.message
config = Config.get_conf(None, cog_name='Forums', identifier=2352711325)
await config.guild(msg.guild).forum_tag.set(int(self.values[0]))
await msg.edit(content=f"Set resolved tag to {self.values[0]}", view=None)
await interaction.response.defer()
class SelectView(ui.View):
def __init__(self, message, options, *, timeout=180):
super().__init__(timeout=timeout)
self.add_item(Select(message, options))