-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add role changing for custom commands #292
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,32 @@ | ||
"""Manage commands.""" | ||
|
||
from . import Command | ||
from ..command import ROLES | ||
|
||
_ROLE_NAMES = [ROLES[role].lower() for role in ROLES] | ||
_REVERSED_ROLES = {v: k for k, v in ROLES.items()} | ||
|
||
|
||
def _role_id(role_name): | ||
return _REVERSED_ROLES[role_name] if role_name in _REVERSED_ROLES else -1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use |
||
|
||
|
||
def _is_role(role): | ||
return role.lower() in _ROLE_NAMES | ||
|
||
|
||
async def _update_role(api, command, role): | ||
"""Update the role of a command.""" | ||
|
||
data = { | ||
"attributes": { | ||
"response": { | ||
"role": role | ||
} | ||
} | ||
} | ||
|
||
return await api.command.update(command, data) | ||
|
||
|
||
class Meta(Command): | ||
|
@@ -91,3 +117,30 @@ async def count(self, command: r'?command', | |
response = await self.api.command.update_count(command, action_string) | ||
if response.status == 200: | ||
return "Count updated." | ||
|
||
@Command.command(role="moderator") | ||
async def permission(self, command: r'?command', role=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't we want to use the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We did, but not using that for permissions. That becomes a problem of having to readd the command to change both, and right now I would prefer if the two were separated. |
||
"""Change the role of a command.""" | ||
|
||
if role is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. If we switch to
|
||
# Didn't get a value, return the role of the command | ||
response = await self.api.command.get(command) | ||
|
||
if response.status == 200: | ||
data = await response.json() | ||
minimum_role = data["data"]["attributes"]["response"]["role"] | ||
return "Minimum role for !{command} is '{role}'".format( | ||
command=command, role=ROLES[minimum_role]) | ||
|
||
valid_role = _is_role(role) | ||
if not valid_role: | ||
# not a real role, tell the user. | ||
return "'{role}' is not a valid role!".format(role=role) | ||
# Valid role, update it. | ||
role_id = _role_id(role) | ||
if role_id == -1: | ||
return "Invalid role {}".format(role) | ||
response = await _update_role(self.api, command, role) | ||
if response.status == 200: | ||
return "Updated role for !{command} to '{role}'".format( | ||
command=command, role=role) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,3 +87,21 @@ Otherwise, `action` may begin with either a `+` or `-`, to increase or decrease | |
[MindlessPuppetz] !command count derp +12 | ||
[CactusBot] Count updated. | ||
``` | ||
|
||
## `!command role <command> [role]` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yay, docs! (Nice choices for usernames, BTW.) |
||
|
||
Retrieve or set the `role` of a custom command. | ||
|
||
If `role` is not supplied, the current `role` is returned. | ||
|
||
``` | ||
[EvilNotion] !command role magical | ||
[CactusBot] Minimum role for !magical is 'Moderator' | ||
``` | ||
|
||
Otherwise, the role is set: | ||
|
||
``` | ||
[HauntedKnight] !command role magical User | ||
[CactusBot] Updated role for !magical to 'User' | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if the type signature was based more on values, rather than just
json.dumps
ingvalue
.For example, something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No - I copied this update function from configs. Keeping it the way it is incase we ever want to update something else about the command, say the name.