insert2-cogs/rolebuttons.py
2024-08-10 15:57:19 -04:00

72 lines
No EOL
2.9 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
class RoleView(nextcord.ui.View):
def __init__(self,bot):
super().__init__(timeout=None)
self.bot = bot
#res = await self.bot.cur.execute(f"SELECT roleid FROM rolebutton WHERE serverid = {self.bot.roleguild.id}")
button = nextcord.ui.Button(label="Test", style=nextcord.ButtonStyle.gray, custom_id="rolebutton:821095192831852554")
button.callback = self.buttoncallback
self.add_item(button)
button = nextcord.ui.Button(label="Test 2", style=nextcord.ButtonStyle.gray, custom_id="rolebutton:1058708072064884736")
button.callback = self.buttoncallback
self.add_item(button)
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.display_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.display_name} used role button")
await interaction.response.send_message(f"Added <@&{role.id}> to you!", ephemeral=True, allowed_mentions=nextcord.AllowedMentions.none())
return
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):
self.bot.add_view(RoleView(self.bot))
@nextcord.slash_command(
name="rolebuttons",
description="modify role buttons",
)
async def rolebuttons(self, interaction: nextcord.Interaction):
return
self.bot.roleguild = interaction.guild
await interaction.response.send_message(view=RoleView(self.bot))
@rolebuttons.subcommand(
name="refresh",
description="refresh a role selection message",
)
async def rolerefresh(self, interaction: nextcord.Interaction, msgid: float):
msg = await nextcord.abc.Messageable.fetch_message(msgid)
if msg.author.id != self.bot.application_id:
await interaction.response.send_message("I can't edit that message", ephemeral=True)
return
await msg.edit(view=RoleView(self.bot))
await interaction.response.send_message("Edited!", ephemeral=True)
def setup(bot: commands.Bot):
bot.add_cog(rolebutton(bot))