forked from Pycord-Development/pycord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
278 additions
and
278 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,110 +1,110 @@ | ||
""" | ||
The MIT License (MIT) | ||
Copyright (c) 2015-2021 Rapptz | ||
Copyright (c) 2021-present Pycord Development | ||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. | ||
""" | ||
|
||
from typing import Callable | ||
|
||
from ..permissions import Permissions | ||
from .core import ApplicationCommand | ||
|
||
__all__ = ( | ||
"default_permissions", | ||
"guild_only", | ||
) | ||
|
||
|
||
def default_permissions(**perms: bool) -> Callable: | ||
"""A decorator that limits the usage of a slash command to members with certain | ||
permissions. | ||
The permissions passed in must be exactly like the properties shown under | ||
:class:`.discord.Permissions`. | ||
.. note:: | ||
These permissions can be updated by server administrators per-guild. As such, these are only "defaults", as the | ||
name suggests. If you want to make sure that a user **always** has the specified permissions regardless, you | ||
should use an internal check such as :func:`~.ext.commands.has_permissions`. | ||
Parameters | ||
---------- | ||
**perms: Dict[:class:`str`, :class:`bool`] | ||
An argument list of permissions to check for. | ||
Example | ||
------- | ||
.. code-block:: python3 | ||
from discord import default_permissions | ||
@bot.slash_command() | ||
@default_permissions(manage_messages=True) | ||
async def test(ctx): | ||
await ctx.respond('You can manage messages.') | ||
""" | ||
|
||
invalid = set(perms) - set(Permissions.VALID_FLAGS) | ||
if invalid: | ||
raise TypeError(f"Invalid permission(s): {', '.join(invalid)}") | ||
|
||
def inner(command: Callable): | ||
if isinstance(command, ApplicationCommand): | ||
if command.parent is not None: | ||
raise RuntimeError( | ||
"Permission restrictions can only be set on top-level commands" | ||
) | ||
command.default_member_permissions = Permissions(**perms) | ||
else: | ||
command.__default_member_permissions__ = Permissions(**perms) | ||
return command | ||
|
||
return inner | ||
|
||
|
||
def guild_only() -> Callable: | ||
"""A decorator that limits the usage of a slash command to guild contexts. | ||
The command won't be able to be used in private message channels. | ||
Example | ||
------- | ||
.. code-block:: python3 | ||
from discord import guild_only | ||
@bot.slash_command() | ||
@guild_only() | ||
async def test(ctx): | ||
await ctx.respond("You're in a guild.") | ||
""" | ||
|
||
def inner(command: Callable): | ||
if isinstance(command, ApplicationCommand): | ||
command.guild_only = True | ||
else: | ||
command.__guild_only__ = True | ||
|
||
return command | ||
|
||
return inner | ||
""" | ||
The MIT License (MIT) | ||
Copyright (c) 2015-2021 Rapptz | ||
Copyright (c) 2021-present Pycord Development | ||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. | ||
""" | ||
|
||
from typing import Callable | ||
|
||
from ..permissions import Permissions | ||
from .core import ApplicationCommand | ||
|
||
__all__ = ( | ||
"default_permissions", | ||
"guild_only", | ||
) | ||
|
||
|
||
def default_permissions(**perms: bool) -> Callable: | ||
"""A decorator that limits the usage of a slash command to members with certain | ||
permissions. | ||
The permissions passed in must be exactly like the properties shown under | ||
:class:`.discord.Permissions`. | ||
.. note:: | ||
These permissions can be updated by server administrators per-guild. As such, these are only "defaults", as the | ||
name suggests. If you want to make sure that a user **always** has the specified permissions regardless, you | ||
should use an internal check such as :func:`~.ext.commands.has_permissions`. | ||
Parameters | ||
---------- | ||
**perms: Dict[:class:`str`, :class:`bool`] | ||
An argument list of permissions to check for. | ||
Example | ||
------- | ||
.. code-block:: python3 | ||
from discord import default_permissions | ||
@bot.slash_command() | ||
@default_permissions(manage_messages=True) | ||
async def test(ctx): | ||
await ctx.respond('You can manage messages.') | ||
""" | ||
|
||
invalid = set(perms) - set(Permissions.VALID_FLAGS) | ||
if invalid: | ||
raise TypeError(f"Invalid permission(s): {', '.join(invalid)}") | ||
|
||
def inner(command: Callable): | ||
if isinstance(command, ApplicationCommand): | ||
if command.parent is not None: | ||
raise RuntimeError( | ||
"Permission restrictions can only be set on top-level commands" | ||
) | ||
command.default_member_permissions = Permissions(**perms) | ||
else: | ||
command.__default_member_permissions__ = Permissions(**perms) | ||
return command | ||
|
||
return inner | ||
|
||
|
||
def guild_only() -> Callable: | ||
"""A decorator that limits the usage of a slash command to guild contexts. | ||
The command won't be able to be used in private message channels. | ||
Example | ||
------- | ||
.. code-block:: python3 | ||
from discord import guild_only | ||
@bot.slash_command() | ||
@guild_only() | ||
async def test(ctx): | ||
await ctx.respond("You're in a guild.") | ||
""" | ||
|
||
def inner(command: Callable): | ||
if isinstance(command, ApplicationCommand): | ||
command.guild_only = True | ||
else: | ||
command.__guild_only__ = True | ||
|
||
return command | ||
|
||
return inner |
This file contains 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 |
---|---|---|
@@ -1,28 +1,28 @@ | ||
# This example requires the 'members' privileged intent to use the Member converter. | ||
|
||
import discord | ||
|
||
intents = discord.Intents.default() | ||
intents.members = True | ||
|
||
bot = discord.Bot(debug_guilds=[...], intents=intents) | ||
# Remove debug_guilds and set guild_ids in the slash command decorators | ||
# to restrict specific commands to the supplied guild IDs. | ||
|
||
|
||
@bot.user_command() # Create a global user command | ||
async def mention( | ||
ctx: discord.ApplicationContext, member: discord.Member | ||
): # User commands give a member param | ||
await ctx.respond(f"{ctx.author.name} just mentioned {member.mention}!") | ||
|
||
|
||
# User commands and message commands can have spaces in their names | ||
@bot.message_command(name="Show ID") # Creates a global message command | ||
async def show_id( | ||
ctx: discord.ApplicationContext, message: discord.Message | ||
): # Message commands give a message param | ||
await ctx.respond(f"{ctx.author.name}, here's the message id: {message.id}!") | ||
|
||
|
||
bot.run("TOKEN") | ||
# This example requires the 'members' privileged intent to use the Member converter. | ||
|
||
import discord | ||
|
||
intents = discord.Intents.default() | ||
intents.members = True | ||
|
||
bot = discord.Bot(debug_guilds=[...], intents=intents) | ||
# Remove debug_guilds and set guild_ids in the slash command decorators | ||
# to restrict specific commands to the supplied guild IDs. | ||
|
||
|
||
@bot.user_command() # Create a global user command | ||
async def mention( | ||
ctx: discord.ApplicationContext, member: discord.Member | ||
): # User commands give a member param | ||
await ctx.respond(f"{ctx.author.name} just mentioned {member.mention}!") | ||
|
||
|
||
# User commands and message commands can have spaces in their names | ||
@bot.message_command(name="Show ID") # Creates a global message command | ||
async def show_id( | ||
ctx: discord.ApplicationContext, message: discord.Message | ||
): # Message commands give a message param | ||
await ctx.respond(f"{ctx.author.name}, here's the message id: {message.id}!") | ||
|
||
|
||
bot.run("TOKEN") |
This file contains 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 |
---|---|---|
@@ -1,46 +1,46 @@ | ||
# This example requires the 'members' privileged intent to use the Member converter. | ||
|
||
import discord | ||
|
||
intents = discord.Intents.default() | ||
intents.members = True | ||
|
||
bot = discord.Bot(intents=intents) | ||
# The debug guilds parameter can be used to restrict slash command registration to only the supplied guild IDs. | ||
# This is done like so: discord.Bot(debug_guilds=[...]) | ||
# Without this, all commands are made global unless they have a guild_ids parameter in the command decorator. | ||
|
||
# Note: If you want you can use commands.Bot instead of discord.Bot. | ||
# Use discord.Bot if you don't want prefixed message commands. | ||
|
||
# With discord.Bot you can use @bot.command as an alias | ||
# of @bot.slash_command but this is overridden by commands.Bot. | ||
|
||
|
||
@bot.slash_command(guild_ids=[...]) # Create a slash command | ||
async def hello(ctx: discord.ApplicationContext): | ||
"""Say hello to the bot""" # The command description can be supplied as the docstring | ||
await ctx.respond(f"Hello {ctx.author}!") | ||
# Note: interactions must be responded to within 3 seconds, if they're not, an | ||
# "Unknown interaction" error will be raised, you can circumvent this by using "ctx.defer()". | ||
# Additional note: You cannot respond to the same interaction twice! | ||
|
||
|
||
@bot.slash_command(name="hi") | ||
async def global_command( | ||
ctx: discord.ApplicationContext, num: int | ||
): # Takes one integer parameter | ||
await ctx.respond(f"This is a global command, {num}!") | ||
|
||
|
||
@bot.slash_command(guild_ids=[...]) | ||
async def joined(ctx: discord.ApplicationContext, member: discord.Member = None): | ||
# Setting a default value for the member parameter makes it optional ^ | ||
user = member or ctx.author | ||
await ctx.respond( | ||
f"{user.name} joined at {discord.utils.format_dt(user.joined_at)}" | ||
) | ||
|
||
|
||
# To learn how to add descriptions and choices to options, check slash_options.py | ||
bot.run("TOKEN") | ||
# This example requires the 'members' privileged intent to use the Member converter. | ||
|
||
import discord | ||
|
||
intents = discord.Intents.default() | ||
intents.members = True | ||
|
||
bot = discord.Bot(intents=intents) | ||
# The debug guilds parameter can be used to restrict slash command registration to only the supplied guild IDs. | ||
# This is done like so: discord.Bot(debug_guilds=[...]) | ||
# Without this, all commands are made global unless they have a guild_ids parameter in the command decorator. | ||
|
||
# Note: If you want you can use commands.Bot instead of discord.Bot. | ||
# Use discord.Bot if you don't want prefixed message commands. | ||
|
||
# With discord.Bot you can use @bot.command as an alias | ||
# of @bot.slash_command but this is overridden by commands.Bot. | ||
|
||
|
||
@bot.slash_command(guild_ids=[...]) # Create a slash command | ||
async def hello(ctx: discord.ApplicationContext): | ||
"""Say hello to the bot""" # The command description can be supplied as the docstring | ||
await ctx.respond(f"Hello {ctx.author}!") | ||
# Note: interactions must be responded to within 3 seconds, if they're not, an | ||
# "Unknown interaction" error will be raised, you can circumvent this by using "ctx.defer()". | ||
# Additional note: You cannot respond to the same interaction twice! | ||
|
||
|
||
@bot.slash_command(name="hi") | ||
async def global_command( | ||
ctx: discord.ApplicationContext, num: int | ||
): # Takes one integer parameter | ||
await ctx.respond(f"This is a global command, {num}!") | ||
|
||
|
||
@bot.slash_command(guild_ids=[...]) | ||
async def joined(ctx: discord.ApplicationContext, member: discord.Member = None): | ||
# Setting a default value for the member parameter makes it optional ^ | ||
user = member or ctx.author | ||
await ctx.respond( | ||
f"{user.name} joined at {discord.utils.format_dt(user.joined_at)}" | ||
) | ||
|
||
|
||
# To learn how to add descriptions and choices to options, check slash_options.py | ||
bot.run("TOKEN") |
Oops, something went wrong.