-
Notifications
You must be signed in to change notification settings - Fork 52
Migrated reaction role cog to new extension format #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FirePlank
wants to merge
10
commits into
SylteA:dev
Choose a base branch
from
FirePlank:roles
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8b40f9a
Migrated reaction roles to use views instead
FirePlank bb713c2
Fixed transformer, added embed to selectable_roles and made it possib…
FirePlank 5c13693
Switched to using commands to get roles
FirePlank 4835bb9
bug fixes
FirePlank ab9c21c
fixed permissions and added list command
FirePlank f2edb91
removed useless code
FirePlank 3d380fb
remove view file
FirePlank 4d6e3a3
fixed issue where roles were getting pushed to memory more than once
FirePlank efcae3b
added break
FirePlank 02c7598
exclude non-assignable roles
HETHAT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from bot.core import DiscordBot | ||
|
|
||
| from .commands import SelectableRoleCommands | ||
|
|
||
|
|
||
| async def setup(bot: DiscordBot) -> None: | ||
| await bot.add_cog(SelectableRoleCommands(bot=bot)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| from typing import List | ||
|
|
||
| import discord | ||
| from discord import app_commands | ||
| from discord.ext import commands | ||
| from pydantic import BaseModel | ||
|
|
||
| from bot import core | ||
| from bot.models import SelectableRole | ||
|
|
||
|
|
||
| class Role(BaseModel): | ||
| name: str | ||
| id: int | ||
FirePlank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class SelectableRoleCommands(commands.Cog): | ||
| admin_commands = app_commands.Group( | ||
| name="selectable-roles", | ||
| description="Commands for managing selectable roles", | ||
| default_permissions=discord.Permissions(administrator=True), | ||
| guild_only=True, | ||
| ) | ||
|
|
||
| def __init__(self, bot: core.DiscordBot): | ||
| self.bot = bot | ||
| self.roles: dict[int, list[Role]] = {} | ||
|
|
||
| def update_roles(self, guild_id: int, data: tuple[str, int]) -> None: | ||
| if self.roles.get(guild_id): | ||
| for role in self.roles[guild_id]: | ||
| if role.id == data[1]: | ||
| return | ||
| self.roles[guild_id].append(Role(name=data[0], id=data[1])) | ||
| else: | ||
| self.roles[guild_id] = [Role(name=data[0], id=data[1])] | ||
|
|
||
| async def cog_load(self) -> None: | ||
| query = "SELECT * FROM selectable_roles" | ||
| records = await SelectableRole.fetch(query) | ||
|
|
||
| for record in records: | ||
| self.update_roles(record.guild_id, (record.role_name, record.role_id)) | ||
|
|
||
| async def role_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: | ||
| if not self.roles.get(interaction.guild.id): | ||
| return [] | ||
|
|
||
| return [ | ||
| app_commands.Choice(name=role.name, value=str(role.id)) | ||
| for role in self.roles[interaction.guild.id] | ||
| if current.lower() in role.name.lower() | ||
| ][:25] | ||
|
|
||
| @app_commands.command(name="get-role") | ||
| @app_commands.guild_only() | ||
FirePlank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @app_commands.autocomplete(role=role_autocomplete) | ||
| async def get( | ||
| self, | ||
| interaction: core.InteractionType, | ||
| role: str, | ||
| ): | ||
| """Get the selected role""" | ||
|
|
||
| if not self.roles.get(interaction.guild.id) or not role.isdigit(): | ||
| return await interaction.response.send_message("That role isn't selectable!", ephemeral=True) | ||
|
|
||
| role = interaction.guild.get_role(int(role)) | ||
| if role is None or not any(role.id == role_.id for role_ in self.roles[interaction.guild.id]): | ||
| return await interaction.response.send_message("That role isn't selectable!", ephemeral=True) | ||
|
|
||
| await interaction.user.add_roles(role, reason="Selectable role") | ||
|
|
||
| to_remove = [] | ||
| for role_ in self.roles[interaction.guild.id]: | ||
| if role_.id != role.id: | ||
| to_remove.append(interaction.guild.get_role(role_.id)) | ||
| await interaction.user.remove_roles(*to_remove, reason="Selectable role") | ||
FirePlank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| await interaction.response.send_message(f"Successfully added {role.mention} to you!", ephemeral=True) | ||
|
|
||
| @admin_commands.command() | ||
| async def add( | ||
| self, | ||
| interaction: core.InteractionType, | ||
| role: discord.Role, | ||
| ): | ||
| """Add a selectable role to the database""" | ||
|
|
||
| if not role.is_assignable(): | ||
| return await interaction.response.send_message( | ||
| "That role is non-assignable by the bot. Please ensure the bot has the necessary permissions.", | ||
| ephemeral=True, | ||
| ) | ||
|
|
||
| await SelectableRole.ensure_exists(interaction.guild.id, role.id, role.name) | ||
| self.update_roles(interaction.guild.id, (role.name, role.id)) | ||
| await interaction.response.send_message(f"Successfully added {role.mention} to the database!", ephemeral=True) | ||
|
|
||
| @admin_commands.command() | ||
| @app_commands.autocomplete(role=role_autocomplete) | ||
| async def remove( | ||
| self, | ||
| interaction: core.InteractionType, | ||
| role: str, | ||
| ): | ||
| """Remove a selectable role from the database""" | ||
|
|
||
| if not self.roles.get(interaction.guild.id): | ||
| return await interaction.response.send_message("There are no selectable roles!", ephemeral=True) | ||
|
|
||
| role = interaction.guild.get_role(int(role)) | ||
| query = "DELETE FROM selectable_roles WHERE guild_id = $1 AND role_id = $2" | ||
| await SelectableRole.execute(query, interaction.guild.id, role.id) | ||
|
|
||
| for i, role_ in enumerate(self.roles[interaction.guild.id]): | ||
| if role_.id == role.id: | ||
| del self.roles[interaction.guild.id][i] | ||
| break | ||
|
|
||
| await interaction.response.send_message( | ||
| f"Successfully removed {role.mention} from the database!", ephemeral=True | ||
| ) | ||
|
|
||
| @admin_commands.command() | ||
| async def list( | ||
| self, | ||
| interaction: core.InteractionType, | ||
| ): | ||
| """List all selectable roles""" | ||
|
|
||
| if not self.roles.get(interaction.guild.id): | ||
| return await interaction.response.send_message("There are no selectable roles!", ephemeral=True) | ||
|
|
||
| roles = [f"<@&{role.id}>" for role in self.roles[interaction.guild.id]] | ||
| embed = discord.Embed(title="Selectable roles", description="\n".join(roles), color=discord.Color.gold()) | ||
| await interaction.response.send_message(embed=embed) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| DROP TABLE IF EXISTS selectable_roles; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| CREATE TABLE IF NOT EXISTS selectable_roles ( | ||
| guild_id BIGINT NOT NULL, | ||
| role_id BIGINT NOT NULL, | ||
| role_name VARCHAR NOT NULL, | ||
FirePlank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| PRIMARY KEY (guild_id, role_id) | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from .model import Model | ||
|
|
||
|
|
||
| class SelectableRole(Model): | ||
| guild_id: int | ||
| role_id: int | ||
| role_name: str | ||
|
|
||
| @classmethod | ||
| async def ensure_exists(cls, guild_id: int, role_id: int, role_name: str): | ||
| """Inserts or updates the selectable role.""" | ||
| query = """ | ||
| INSERT INTO selectable_roles (guild_id, role_id, role_name) | ||
| VALUES ($1, $2, $3) | ||
| ON CONFLICT (guild_id, role_id) | ||
| DO UPDATE SET | ||
| role_name = $3 | ||
| """ | ||
FirePlank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return await cls.fetchrow(query, guild_id, role_id, role_name) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.