Repository navigation
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
32
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
32 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 53f05d2
Merge remote-tracking branch 'origin/main' into colour-command
MattyTheHacker 2826f27
Use lazy imports in colour selector cog
MattyTheHacker 6f0a827
Merge main into colour-command
automatic-pr-updater[bot] f3b4dcc
Merge main into colour-command
automatic-pr-updater[bot] ba4c9a4
Minor fixes
MattyTheHacker 712b051
Fixes
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
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,167 @@ | ||
| """Contains cog classes for any select_colour_role interactions.""" | ||
|
|
||
| __all__: Sequence[str] = ("SelectColourRoleCommandCog",) | ||
|
|
||
| import logging | ||
| lazy from collections.abc import Collection, Sequence | ||
| lazy from collections.abc import Set as AbstractSet | ||
| lazy from logging import Logger | ||
| lazy from typing import Final | ||
|
|
||
| import discord | ||
|
|
||
| from utils import CommandChecks, TeXBotBaseCog | ||
| lazy from exceptions import GuildDoesNotExistError, RoleNotFoundInMainGuildError | ||
| lazy from utils import TeXBotApplicationContext, TeXBotAutocompleteContext | ||
|
|
||
| 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 SelectColourRoleCommandCog(TeXBotBaseCog): | ||
| """Cog class that defines the "/select-colour-role" command and its call-back method.""" | ||
|
|
||
| @staticmethod | ||
| async def autocomplete_get_colour_roles( | ||
| ctx: TeXBotAutocompleteContext, | ||
| ) -> AbstractSet[discord.OptionChoice] | AbstractSet[str]: | ||
| """Autocomplete callable that generates the set of selectable colour roles.""" | ||
| 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_get_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: | ||
| """ | ||
| 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(): | ||
| missing_colour_role_names: AbstractSet[str] = COLOUR_ROLE_NAMES - { | ||
| role.name.lower() for role in self.bot.main_guild.roles | ||
| } | ||
| if missing_colour_role_names: | ||
| logger.warning( | ||
| "Colour roles %s could not be found.", | ||
| ", ".join(sorted(missing_colour_role_names)), | ||
| ) | ||
|
|
||
| try: | ||
| role_to_add: discord.Role = await self.bot.get_role_from_str_id(str_role_id) | ||
| except RoleNotFoundInMainGuildError: | ||
| await self.command_send_error( | ||
| ctx, | ||
| message=( | ||
| "The specified role could not be found " | ||
| f"in the {self.bot.group_short_name} Discord server. " | ||
| "Please use the autocomplete." | ||
| ), | ||
| ) | ||
| return | ||
| except ValueError: | ||
| await self.command_send_error( | ||
| ctx, | ||
| message=( | ||
| "The specified role ID is not a valid role ID. " | ||
| "Please use the autocomplete." | ||
| ), | ||
| ) | ||
| return | ||
|
MattyTheHacker marked this conversation as resolved.
Resolved
|
||
|
|
||
| if role_to_add.name.lower() not in COLOUR_ROLE_NAMES: | ||
| await ctx.followup.send( | ||
| content=( | ||
| ":information_source: No changes made. " | ||
| f"{role_to_add.name} is not a valid colour role. " | ||
| ":information_source:" | ||
| ), | ||
| ephemeral=True, | ||
| ) | ||
| return | ||
|
|
||
| interaction_member: discord.Member = await self.bot.get_main_guild_member(ctx.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( | ||
| content=( | ||
| ":information_source: No changes made. " | ||
| f"You already have the {role_to_add.name} colour role. " | ||
| ":information_source:" | ||
| ), | ||
| ephemeral=True, | ||
| ) | ||
| return | ||
|
|
||
| SELECT_COLOUR_ROLE_AUDIT_MESSAGE: Final[str] = ( | ||
| f'{interaction_member} used TeX-Bot slash-command "/select-colour-role".' | ||
| ) | ||
|
|
||
| if not member_already_has_role_to_add: | ||
| await interaction_member.add_roles( | ||
| role_to_add, reason=SELECT_COLOUR_ROLE_AUDIT_MESSAGE | ||
| ) | ||
|
|
||
| await interaction_member.remove_roles( | ||
| *roles_to_remove, reason=SELECT_COLOUR_ROLE_AUDIT_MESSAGE | ||
| ) | ||
|
|
||
| await ctx.followup.send( | ||
| content=( | ||
| ( | ||
| f"You already have the {role_to_add.name} colour role, " | ||
| "so your other colour roles have been removed." | ||
| ) | ||
| if member_already_has_role_to_add | ||
| else 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.
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.