insert2-cogs/rolebuttons.py

119 lines
4.7 KiB
Python
Raw Normal View History

2024-08-09 12:41:32 +00:00
import nextcord
from nextcord.ext import commands
from nextcord import TextInputStyle
from random import randint
import aiosqlite as sqlite3
import aiohttp
from urllib.parse import urlparse
import os
2024-08-10 20:21:53 +00:00
buttonlist = []
2024-08-09 12:41:32 +00:00
class RoleView(nextcord.ui.View):
2024-08-09 12:59:25 +00:00
def __init__(self,bot):
2024-08-09 12:41:32 +00:00
super().__init__(timeout=None)
self.bot = bot
2024-08-10 20:21:53 +00:00
for b in buttonlist:
2024-08-10 20:27:03 +00:00
b.callback = self.buttoncallback
2024-08-10 20:21:53 +00:00
self.add_item(b)
2024-08-09 12:41:32 +00:00
2024-08-09 14:41:22 +00:00
async def buttoncallback(self, interaction: nextcord.Interaction) -> None:
2024-08-09 19:19:50 +00:00
role = interaction.guild.get_role(int(interaction.data["custom_id"].split(":")[1]))
2024-08-09 19:14:33 +00:00
if role in interaction.user.roles:
2024-08-10 20:48:23 +00:00
await interaction.user.remove_roles(role, reason=f"{interaction.user.name} used role button")
2024-08-09 19:23:07 +00:00
await interaction.response.send_message(f"Removed <@&{role.id}> from you!", ephemeral=True, allowed_mentions=nextcord.AllowedMentions.none())
2024-08-09 19:14:33 +00:00
else:
2024-08-10 20:48:23 +00:00
await interaction.user.add_roles(role, reason=f"{interaction.user.name} used role button")
2024-08-09 19:23:07 +00:00
await interaction.response.send_message(f"Added <@&{role.id}> to you!", ephemeral=True, allowed_mentions=nextcord.AllowedMentions.none())
2024-08-09 14:30:36 +00:00
return
2024-08-09 14:26:25 +00:00
2024-08-09 13:52:31 +00:00
2024-08-09 12:41:32 +00:00
2024-08-09 13:33:01 +00:00
class rolebutton(commands.Cog):
2024-08-09 12:41:32 +00:00
def __init__(self, bot: commands.Bot):
self.bot = bot
2024-08-10 19:57:19 +00:00
#errors if first loaded but is needed after
try:
self.bot.add_view(RoleView(self.bot))
except:
pass
2024-08-10 19:17:17 +00:00
@commands.Cog.listener('on_ready')
async def roleready(self):
self.bot.add_view(RoleView(self.bot))
2024-08-09 12:41:32 +00:00
@nextcord.slash_command(
2024-08-09 21:54:24 +00:00
name="rolebuttons",
description="modify role buttons",
2024-08-10 20:27:03 +00:00
default_member_permissions=nextcord.Permissions(manage_roles=True),
2024-08-09 12:41:32 +00:00
)
2024-08-09 21:54:24 +00:00
async def rolebuttons(self, interaction: nextcord.Interaction):
return
2024-08-09 12:51:22 +00:00
self.bot.roleguild = interaction.guild
2024-08-09 13:34:45 +00:00
await interaction.response.send_message(view=RoleView(self.bot))
2024-08-09 21:54:24 +00:00
2024-08-10 20:17:33 +00:00
@nextcord.message_command(
2024-08-10 20:48:23 +00:00
name="Refresh Role Buttons",
2024-08-10 20:27:03 +00:00
default_member_permissions=nextcord.Permissions(manage_roles=True),
2024-08-09 21:54:24 +00:00
)
2024-08-10 20:48:23 +00:00
2024-08-10 20:17:33 +00:00
@nextcord.ext.application_checks.has_permissions(manage_roles=True)
async def rolerefresh(self, interaction: nextcord.Interaction, message: nextcord.Message):
if message.author.id != self.bot.application_id:
2024-08-09 21:54:24 +00:00
await interaction.response.send_message("I can't edit that message", ephemeral=True)
return
2024-08-10 20:07:44 +00:00
res = await self.bot.cur.execute(f"SELECT roleid FROM rolebutton WHERE serverid = {interaction.guild.id}")
res = await res.fetchall()
2024-08-10 20:21:53 +00:00
buttonlist.clear()
2024-08-10 20:37:05 +00:00
for [r] in res:
role = interaction.guild.get_role(int(r))
role.name
button = nextcord.ui.Button(label=role.name, style=nextcord.ButtonStyle.gray, custom_id=f"rolebutton:{role.id}")
2024-08-10 20:21:53 +00:00
buttonlist.append(button)
2024-08-10 20:17:33 +00:00
await message.edit(view=RoleView(self.bot))
2024-08-09 21:54:24 +00:00
await interaction.response.send_message("Edited!", ephemeral=True)
2024-08-10 20:48:23 +00:00
@rolebuttons.subcommand(
name="create",
description="Create a role message",
)
@nextcord.ext.application_checks.has_permissions(manage_roles=True)
async def rolecreate(self, interaction: nextcord.Interaction, content: str):
res = await self.bot.cur.execute(f"SELECT roleid FROM rolebutton WHERE serverid = {interaction.guild.id}")
res = await res.fetchall()
buttonlist.clear()
for [r] in res:
role = interaction.guild.get_role(int(r))
role.name
button = nextcord.ui.Button(label=role.name, style=nextcord.ButtonStyle.gray, custom_id=f"rolebutton:{role.id}")
buttonlist.append(button)
await interaction.channel.send(content=content, view=RoleView(self.bot))
await interaction.response.send_message("Done", ephemeral=True)
2024-08-10 20:07:44 +00:00
@rolebuttons.subcommand(
name="add",
description="add a role to the selection message",
)
async def roleadd(self, interaction: nextcord.Interaction, role: nextcord.Role):
await self.bot.cur.execute(f"""
INSERT INTO rolebutton VALUES
({interaction.guild_id}, {role.id})
""")
await self.bot.db.commit()
await interaction.response.send_message("Done", ephemeral=True)
2024-08-10 20:48:23 +00:00
@rolebuttons.subcommand(
name="remove",
description="remove a role from the selection message",
)
async def roleremove(self, interaction: nextcord.Interaction, role: nextcord.Role):
await self.bot.cur.execute(f"""
DELETE FROM rolebutton WHERE serverid = {interaction.guild.id} AND roleid = {role.id})
""")
await self.bot.db.commit()
await interaction.response.send_message("Done", ephemeral=True)
2024-08-09 12:41:32 +00:00
def setup(bot: commands.Bot):
2024-08-09 13:33:01 +00:00
bot.add_cog(rolebutton(bot))