-
Notifications
You must be signed in to change notification settings - Fork 11
Implement member colour change command #827
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
MattyTheHacker
wants to merge
26
commits into
main
Choose a base branch
from
colour-command
base: main
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
26 commits
Select commit
Hold shift + click to select a range
1a4f883
Implement member colour change command
MattyTheHacker b27aff8
Fix errors
MattyTheHacker 9523733
Add util function
MattyTheHacker 8f14666
Drop in real role names
MattyTheHacker 23ddee8
Fixes
MattyTheHacker fba38c6
Improvements
MattyTheHacker fa8f7e5
Fixes
MattyTheHacker 38053a9
More fixes
MattyTheHacker 5c4dd46
Merge main into colour-command
automatic-pr-updater[bot] 439ca33
Merge main into colour-command
automatic-pr-updater[bot] 6dc3138
Merge main into colour-command
automatic-pr-updater[bot] 360b065
Merge main into colour-command
automatic-pr-updater[bot] 9c4bef6
Merge main into colour-command
automatic-pr-updater[bot] a815f16
Merge main into colour-command
automatic-pr-updater[bot] 0656dde
Run ruff format
MattyTheHacker 161c037
[autofix.ci] apply automated fixes
autofix-ci[bot] c9104a3
Merge main into colour-command
automatic-pr-updater[bot] 476fd0e
Merge main into colour-command
automatic-pr-updater[bot] 1b456e4
Refactor role fetching
MattyTheHacker ec53b4a
Other fixes
MattyTheHacker abca8e5
Formatting
MattyTheHacker 4b8c11a
Fix
MattyTheHacker 62adb7e
More fixes
MattyTheHacker 144b333
Reformat
MattyTheHacker 5c41c77
Move TODO comment
MattyTheHacker 5a6c87a
Improve removing logic
MattyTheHacker 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| """Contains cog classes for the colour selector command.""" | ||
|
|
||
| import logging | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import discord | ||
|
|
||
| from exceptions import ( | ||
| GuildDoesNotExistError, | ||
| RoleNotFoundInMainGuildError, | ||
| ) | ||
| from utils import CommandChecks, TeXBotBaseCog | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Collection, Sequence | ||
| from collections.abc import Set as AbstractSet | ||
| from logging import Logger | ||
| from typing import Final | ||
|
|
||
| from utils import TeXBotApplicationContext, TeXBotAutocompleteContext | ||
|
|
||
|
|
||
| __all__: Sequence[str] = ("MemberColourSelectorCommandCog",) | ||
|
|
||
|
|
||
| logger: Final[Logger] = logging.getLogger("TeX-Bot") | ||
|
|
||
| # TODO: Make this a config option in the future # noqa: FIX002 | ||
| COLOUR_ROLE_NAMES: Final[AbstractSet[str]] = { | ||
| "og-green", | ||
| "pink", | ||
| "orange", | ||
| "purple", | ||
| "new-green", | ||
| "yellow", | ||
| "red", | ||
| } | ||
|
|
||
|
|
||
| class MemberColourSelectorCommandCog(TeXBotBaseCog): | ||
| """Cog class for the colour selector command.""" | ||
|
|
||
| @staticmethod | ||
| async def autocomplete_colour_roles( | ||
| ctx: TeXBotAutocompleteContext, | ||
| ) -> AbstractSet[discord.OptionChoice] | AbstractSet[str]: | ||
| """Autocomplete function for the colour roles option of the colour selector command.""" | ||
| try: | ||
| main_guild: discord.Guild = ctx.bot.main_guild | ||
| except GuildDoesNotExistError: | ||
| return set() | ||
|
|
||
| roles: set[discord.Role] = { | ||
| role for role in main_guild.roles if role.name.lower() in COLOUR_ROLE_NAMES | ||
| } | ||
|
|
||
| if not ctx.value or ctx.value.startswith("@"): | ||
| return { | ||
| discord.OptionChoice(name=f"@{role.name}", value=str(role.id)) | ||
| for role in roles | ||
| } | ||
|
|
||
| return {discord.OptionChoice(name=role.name, value=str(role.id)) for role in roles} | ||
|
|
||
| @discord.slash_command( | ||
| name="select-colour-role", description="Select a colour role for yourself." | ||
| ) | ||
| @discord.option( | ||
| name="colour-role", | ||
| description="The colour role you want to select.", | ||
| autocomplete=discord.utils.basic_autocomplete(autocomplete_colour_roles), | ||
| input_type=str, | ||
| required=True, | ||
| parameter_name="str_role_id", | ||
| ) | ||
| @CommandChecks.check_interaction_user_in_main_guild | ||
| @CommandChecks.check_interaction_user_has_member_role | ||
| async def select_colour_role( | ||
| self, ctx: TeXBotApplicationContext, str_role_id: str | ||
| ) -> None: | ||
| """ | ||
| Slash command for selecting a colour role for the user. | ||
|
|
||
| Definition & callback response of the "select-colour-role" command. | ||
| The "select-colour-role" command assigns a colour role to the member that used | ||
| the command to allow them to change their display colour. | ||
| """ | ||
| await ctx.defer(ephemeral=True) | ||
|
|
||
| async with ctx.typing(): | ||
| if not ctx.interaction.user: | ||
| await self.command_send_error( | ||
| ctx=ctx, | ||
| message=( | ||
| "Cannot assign colour role when interaction user was not available." | ||
| ), | ||
| ) | ||
| return | ||
|
|
||
| role_to_add: discord.Role | ||
| try: | ||
| role_to_add = await ctx.bot.get_role_from_str_id(str_role_id) | ||
| except RoleNotFoundInMainGuildError: | ||
| await ctx.followup.send( | ||
| "The specified role could not be found in the main guild. " | ||
| "Please use the autocomplete.", | ||
| ephemeral=True, | ||
| ) | ||
| return | ||
| except ValueError: | ||
| await ctx.followup.send( | ||
| "The specified role ID is not a valid role ID. " | ||
| "Please use the autocomplete.", | ||
| ephemeral=True, | ||
| ) | ||
| return | ||
|
MattyTheHacker marked this conversation as resolved.
|
||
|
|
||
| if role_to_add.name.lower() not in COLOUR_ROLE_NAMES: | ||
| await ctx.followup.send( | ||
| ":information_source: No changes made. " | ||
| f"{role_to_add.name} is not a valid colour role. " | ||
| ":information_source:" | ||
| ) | ||
| return | ||
|
|
||
| interaction_member: discord.Member = await self.bot.get_main_guild_member( | ||
| ctx.interaction.user | ||
| ) | ||
|
|
||
| roles_to_remove: Collection[discord.Role] = [ | ||
| role | ||
| for role in interaction_member.roles | ||
| if role.name.lower() in COLOUR_ROLE_NAMES and role != role_to_add | ||
| ] | ||
|
|
||
| member_already_has_role_to_add: bool = role_to_add in interaction_member.roles | ||
|
|
||
| if member_already_has_role_to_add and not roles_to_remove: | ||
| await ctx.followup.send( | ||
| ":information_source: No changes made. " | ||
| f"You already have the {role_to_add.name} colour role. " | ||
| ":information_source:", | ||
| ephemeral=True, | ||
| ) | ||
| return | ||
|
|
||
| await interaction_member.remove_roles( | ||
| *roles_to_remove, | ||
| reason=( | ||
| f'{interaction_member} used TeX-Bot slash-command "/select-colour-role".' | ||
| ), | ||
| ) | ||
|
|
||
| if member_already_has_role_to_add: | ||
| await ctx.followup.send( | ||
| ":information_source: No changes made. " | ||
| f"You already have the {role_to_add.name} colour role.", | ||
| ephemeral=True, | ||
| ) | ||
| return | ||
|
|
||
| await interaction_member.add_roles( | ||
| role_to_add, | ||
| reason=( | ||
| f'{interaction_member} used TeX-Bot slash-command "/select-colour-role".' | ||
| ), | ||
| ) | ||
|
|
||
| await ctx.followup.send( | ||
| f"Successfully gave you the {role_to_add.name} colour role!", ephemeral=True | ||
| ) | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.