138 lines
No EOL
5.7 KiB
Python
138 lines
No EOL
5.7 KiB
Python
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
|
|
buttonlist = []
|
|
|
|
class RoleView(nextcord.ui.View):
|
|
def __init__(self,bot):
|
|
super().__init__(timeout=None)
|
|
self.bot = bot
|
|
for b in buttonlist:
|
|
b.callback = self.buttoncallback
|
|
self.add_item(b)
|
|
|
|
async def buttoncallback(self, interaction: nextcord.Interaction) -> None:
|
|
role = interaction.guild.get_role(int(interaction.data["custom_id"].split(":")[1]))
|
|
if role in interaction.user.roles:
|
|
await interaction.user.remove_roles(role, reason=f"{interaction.user.name} used role button")
|
|
await interaction.response.send_message(f"Removed <@&{role.id}> from you!", ephemeral=True, allowed_mentions=nextcord.AllowedMentions.none())
|
|
else:
|
|
await interaction.user.add_roles(role, reason=f"{interaction.user.name} used role button")
|
|
await interaction.response.send_message(f"Added <@&{role.id}> to you!", ephemeral=True, allowed_mentions=nextcord.AllowedMentions.none())
|
|
return
|
|
|
|
async def on_error(self, error: nextcord.DiscordException, item: nextcord.ui.Item, interaction: nextcord.Interaction):
|
|
error = traceback.format_exc()
|
|
print(error, file=sys.stderr)
|
|
message = f"```py\n{error[-1800:]}\n```\n Contact <@{self.bot.owner_id}> if the error persists"
|
|
try:
|
|
await interaction.send(message, ephemeral=True)
|
|
except:
|
|
try:
|
|
await interaction.followup.send(message, ephemeral=True)
|
|
except:
|
|
await interaction.response.send_message(message, ephemeral=True)
|
|
|
|
|
|
|
|
class rolebutton(commands.Cog):
|
|
|
|
def __init__(self, bot: commands.Bot):
|
|
self.bot = bot
|
|
#errors if first loaded but is needed after
|
|
try:
|
|
self.bot.add_view(RoleView(self.bot))
|
|
except:
|
|
pass
|
|
|
|
@commands.Cog.listener('on_ready')
|
|
async def roleready(self):
|
|
for g in self.bot.guilds:
|
|
res = await self.bot.cur.execute(f"SELECT roleid FROM rolebutton WHERE serverid = {g.id}")
|
|
res = await res.fetchall()
|
|
buttonlist.clear()
|
|
for [r] in res:
|
|
role = g.get_role(int(r))
|
|
button = nextcord.ui.Button(label=role.name, style=nextcord.ButtonStyle.gray, custom_id=f"rolebutton:{role.id}")
|
|
buttonlist.append(button)
|
|
self.bot.add_view(RoleView(self.bot))
|
|
|
|
@nextcord.slash_command(
|
|
name="rolebuttons",
|
|
description="modify role buttons",
|
|
default_member_permissions=nextcord.Permissions(manage_roles=True),
|
|
)
|
|
async def rolebuttons(self, interaction: nextcord.Interaction):
|
|
return
|
|
self.bot.roleguild = interaction.guild
|
|
await interaction.response.send_message(view=RoleView(self.bot))
|
|
|
|
@nextcord.message_command(
|
|
name="Refresh Role Buttons",
|
|
default_member_permissions=nextcord.Permissions(manage_roles=True),
|
|
)
|
|
|
|
@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:
|
|
await interaction.response.send_message("I can't edit that message", ephemeral=True)
|
|
return
|
|
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))
|
|
button = nextcord.ui.Button(label=role.name, style=nextcord.ButtonStyle.gray, custom_id=f"rolebutton:{role.id}")
|
|
buttonlist.append(button)
|
|
await message.edit(view=RoleView(self.bot))
|
|
await interaction.response.send_message("Edited!", ephemeral=True)
|
|
|
|
@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)
|
|
|
|
@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)
|
|
|
|
@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)
|
|
|
|
|
|
def setup(bot: commands.Bot):
|
|
bot.add_cog(rolebutton(bot)) |