Skip to content

Commit 06b5601

Browse files
authored
Improve examples (#791)
1 parent 99b52e2 commit 06b5601

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
import discord
22
from discord.commands import Option
33

4-
bot = discord.Bot()
54

5+
bot = discord.Bot()
66
# If you use commands.Bot, @bot.slash_command should be used for
77
# slash commands. You can use @bot.slash_command with discord.Bot as well
88

99

1010
@bot.slash_command(guild_ids=[...])
1111
async def hello(
12-
ctx,
12+
ctx: discord.ApplicationContext,
1313
name: Option(str, "Enter your name"),
1414
gender: Option(str, "Choose your gender", choices=["Male", "Female", "Other"]),
15-
age: Option(int, "Enter your age", required=False, default=18),
15+
age: Option(int, "Enter your age", min_value=1, max_value=99, default=18)
16+
# passing the default value makes an argument optional
17+
# you also can create optional argument using:
18+
# age: Option(int, "Enter your age") = 18
1619
):
17-
await ctx.respond(f"Hello {name}")
20+
await ctx.respond(f"Hello {name}! Your gender is {gender} and you are {age} years old.")
21+
22+
23+
@bot.slash_command(guild_ids=[...])
24+
async def channel(
25+
ctx: discord.ApplicationContext,
26+
channel: Option([discord.TextChannel, discord.VoiceChannel], "Select a channel")
27+
# you can specify allowed channel types by passing a list of them like: [discord.TextChannel, discord.VoiceChannel]
28+
):
29+
await ctx.respond(f"Hi! You selected {channel.mention} channel.")
30+
1831

1932

2033
bot.run("TOKEN")

examples/custom_context.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from discord.ext import commands
55

66

7-
class MyContext(commands.Context):
7+
class MyContext(commands.Context): # custom context
88
async def tick(self, value):
99
# reacts to the message with an emoji
1010
# depending on whether value is True or False
@@ -21,13 +21,30 @@ async def tick(self, value):
2121
pass
2222

2323

24+
# you can subclass discord.ApplicationContext to create custom application context if needed
25+
class MyApplicationContext(discord.ApplicationContext): # custom application context
26+
async def success(self, message):
27+
try:
28+
await self.respond(embed=discord.Embed( # respond with a green embed with "Success" title
29+
title="Success",
30+
description=message,
31+
colour=discord.Colour.green()
32+
))
33+
except discord.HTTPException: # ignore exceptions
34+
pass
35+
36+
2437
class MyBot(commands.Bot):
2538
async def get_context(self, message, *, cls=MyContext):
2639
# when you override this method, you pass your new Context
2740
# subclass to the super() method, which tells the bot to
2841
# use the new MyContext class
2942
return await super().get_context(message, cls=cls)
3043

44+
async def get_application_context(self, interaction, cls=MyApplicationContext):
45+
# the same stuff for custom application context
46+
return await super().get_application_context(interaction, cls=cls)
47+
3148

3249
bot = MyBot(command_prefix="!")
3350

@@ -44,10 +61,19 @@ async def guess(ctx, number: int):
4461
await ctx.tick(number == value)
4562

4663

64+
@bot.slash_command(guild_ids=[...])
65+
async def slash_guess(ctx, number: int):
66+
"""Guess a random number from 1 to 6."""
67+
value = random.randint(1, 6)
68+
if number == value:
69+
await ctx.success("Congratulations! You guessed the number.") # use the new helper function
70+
else:
71+
await ctx.respond("You are wrong! Try again.")
72+
73+
4774
# IMPORTANT: You shouldn't hard code your token
4875
# these are very important, and leaking them can
4976
# let people do very malicious things with your
5077
# bot. Try to use a file or something to keep
5178
# them private, and don't commit it to GitHub
52-
token = "your token here"
53-
bot.run(token)
79+
bot.run("TOKEN")

0 commit comments

Comments
 (0)