-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial example updates * More typehinting + comment changes * Cleanup view examples + touchup previous examples * Update custom_context.py * TicTacToe example + import syntax consistency * Prefixes + debug_guilds * Use new embed/modal parameters + trailing commas * Paginator + comment updates/additions * Update slash_cog_groups.py * Update bridge_commands.py * Update slash_options.py Co-authored-by: Lala Sabathil <lala@pycord.dev> Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
- Loading branch information
1 parent
a2dd302
commit e441641
Showing
37 changed files
with
683 additions
and
521 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
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,45 +1,46 @@ | ||
import discord | ||
from discord.ext import commands | ||
# This example requires the 'members' privileged intent to use the Member converter. | ||
|
||
# imports | ||
import discord | ||
|
||
intents = discord.Intents( | ||
guilds=True, | ||
members=True, | ||
messages=True, | ||
) | ||
intents = discord.Intents.default() | ||
intents.members = True | ||
|
||
bot = commands.Bot( | ||
command_prefix="/", | ||
description="An example to showcase how to extract info about users", | ||
bot = discord.Bot( | ||
debug_guilds=[...], | ||
description="An example to showcase how to extract info about users.", | ||
intents=intents, | ||
) | ||
|
||
|
||
@bot.slash_command(name="userinfo", description="gets the info of a user") | ||
async def info(ctx, user: discord.Member = None): | ||
user = user or ctx.author # if no user is provided it'll use the the author of the message | ||
e = discord.Embed() | ||
e.set_author(name=user.name) | ||
e.add_field(name="ID", value=user.id, inline=False) # user ID | ||
e.add_field( | ||
name="Joined", | ||
value=discord.utils.format_dt(user.joined_at, "F"), | ||
inline=False, | ||
) # When the user joined the server | ||
e.add_field( | ||
name="Created", | ||
value=discord.utils.format_dt(user.created_at, "F"), | ||
inline=False, | ||
) # When the user's account was created | ||
colour = user.colour | ||
if colour.value: # if user has a role with a color | ||
e.colour = colour | ||
|
||
if isinstance(user, discord.User): # checks if the user in the server | ||
e.set_footer(text="This member is not in this server.") | ||
|
||
await ctx.respond(embed=e) # sends the embed | ||
|
||
|
||
bot.run("your token") | ||
@bot.slash_command(name="userinfo", description="Gets info about a user.") | ||
async def info(ctx: discord.ApplicationContext, user: discord.Member = None): | ||
user = user or ctx.author # If no user is provided it'll use the author of the message | ||
embed = discord.Embed( | ||
fields=[ | ||
discord.EmbedField(name="ID", value=str(user.id), inline=False), # User ID | ||
discord.EmbedField( | ||
name="Created", | ||
value=discord.utils.format_dt(user.created_at, "F"), | ||
inline=False, | ||
), # When the user's account was created | ||
], | ||
) | ||
embed.set_author(name=user.name) | ||
embed.set_thumbnail(url=user.display_avatar.url) | ||
|
||
if user.colour.value: # If user has a role with a color | ||
embed.colour = user.colour | ||
|
||
if isinstance(user, discord.User): # Checks if the user in the server | ||
embed.set_footer(text="This user is not in this server.") | ||
else: # We end up here if the user is a discord.Member object | ||
embed.add_field( | ||
name="Joined", | ||
value=discord.utils.format_dt(user.joined_at, "F"), | ||
inline=False, | ||
) # When the user joined the server | ||
|
||
await ctx.respond(embeds=[embed]) # Sends the embed | ||
|
||
|
||
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
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
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,21 +1,27 @@ | ||
from discord.commands import ( # Importing the decorator that makes slash commands. | ||
slash_command, | ||
) | ||
# This example demonstrates a standalone cog file with the bot instance in a separate file. | ||
|
||
import discord | ||
from discord.ext import commands | ||
|
||
|
||
class Example(commands.Cog): | ||
def __init__(self, bot): | ||
self.bot = bot | ||
|
||
@slash_command(guild_ids=[...]) # Create a slash command for the supplied guilds. | ||
async def hello(self, ctx): | ||
@commands.slash_command(guild_ids=[...]) # Create a slash command for the supplied guilds. | ||
async def hello(self, ctx: discord.ApplicationContext): | ||
await ctx.respond("Hi, this is a slash command from a cog!") | ||
|
||
@slash_command() # Not passing in guild_ids creates a global slash command (might take an hour to register). | ||
async def hi(self, ctx): | ||
@commands.slash_command() # Not passing in guild_ids creates a global slash command. | ||
async def hi(self, ctx: discord.ApplicationContext): | ||
await ctx.respond("Hi, this is a global slash command from a cog!") | ||
|
||
|
||
def setup(bot): | ||
bot.add_cog(Example(bot)) | ||
|
||
|
||
# The basic bot instance in a separate file should look something like this: | ||
# bot = commands.Bot(command_prefix=commands.when_mentioned_or("!")) | ||
# bot.load_extension("slash_cog") | ||
# 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
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,31 +1,22 @@ | ||
import discord | ||
|
||
bot = discord.Bot() | ||
bot = discord.Bot(debug_guilds=[...]) | ||
|
||
# If you use commands.Bot, @bot.slash_command should be used for | ||
# slash commands. You can use @bot.slash_command with discord.Bot as well | ||
# slash commands. You can use @bot.slash_command with discord.Bot as well. | ||
|
||
math = bot.create_group("math", "Commands related to mathematics.") # create a slash command group | ||
math = bot.create_group("math", "Commands related to mathematics.") # Create a slash command group | ||
|
||
# Another way, creating the class manually: | ||
|
||
@math.command(guild_ids=[...]) # create a slash command | ||
async def add(ctx, num1: int, num2: int): | ||
"""Get the sum of 2 integers.""" | ||
await ctx.respond(f"The sum of these numbers is **{num1+num2}**") | ||
|
||
|
||
# another way, creating the class manually | ||
|
||
from discord.commands import SlashCommandGroup | ||
math = discord.SlashCommandGroup("math", "Commands related to mathematics.") | ||
|
||
math = SlashCommandGroup("math", "Commands related to mathematics.") | ||
|
||
|
||
@math.command(guild_ids=[...]) | ||
async def add(ctx, num1: int, num2: int): | ||
... | ||
@math.command() # Create a slash command under the math group | ||
async def add(ctx: discord.ApplicationContext, num1: int, num2: int): | ||
"""Get the sum of 2 integers.""" | ||
await ctx.respond(f"The sum of these numbers is **{num1+num2}**") | ||
|
||
|
||
bot.add_application_command(math) | ||
|
||
bot.run("TOKEN") |
Oops, something went wrong.