Skip to content

Commit 2c079fa

Browse files
committed
Update examples to use Context Generic
1 parent 2879dc5 commit 2c079fa

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

examples/basic_bot/main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,39 @@ async def event_oauth_authorized(self, payload: authentication.UserTokenPayload)
5656

5757
class GeneralCommands(commands.Component):
5858
@commands.command()
59-
async def hi(self, ctx: commands.Context) -> None:
59+
async def hi(self, ctx: commands.Context[Bot]) -> None:
6060
"""Command that replys to the invoker with Hi <name>!
6161
6262
!hi
6363
"""
6464
await ctx.reply(f"Hi {ctx.chatter}!")
6565

6666
@commands.command()
67-
async def say(self, ctx: commands.Context, *, message: str) -> None:
67+
async def say(self, ctx: commands.Context[Bot], *, message: str) -> None:
6868
"""Command which repeats what the invoker sends.
6969
7070
!say <message>
7171
"""
7272
await ctx.send(message)
7373

7474
@commands.command()
75-
async def add(self, ctx: commands.Context, left: int, right: int) -> None:
75+
async def add(self, ctx: commands.Context[Bot], left: int, right: int) -> None:
7676
"""Command which adds to integers together.
7777
7878
!add <number> <number>
7979
"""
8080
await ctx.reply(f"{left} + {right} = {left + right}")
8181

8282
@commands.command()
83-
async def choice(self, ctx: commands.Context, *choices: str) -> None:
83+
async def choice(self, ctx: commands.Context[Bot], *choices: str) -> None:
8484
"""Command which takes in an arbitrary amount of choices and randomly chooses one.
8585
8686
!choice <choice_1> <choice_2> <choice_3> ...
8787
"""
8888
await ctx.reply(f"You provided {len(choices)} choices, I choose: {random.choice(choices)}")
8989

9090
@commands.command(aliases=["thanks", "thank"])
91-
async def give(self, ctx: commands.Context, user: twitchio.User, amount: int, *, message: str | None = None) -> None:
91+
async def give(self, ctx: commands.Context[Bot], user: twitchio.User, amount: int, *, message: str | None = None) -> None:
9292
"""A more advanced example of a command which has makes use of the powerful argument parsing, argument converters and
9393
aliases.
9494

examples/module_examples/standard/components/cmds.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
import twitchio
22
from twitchio.ext import commands
33

4+
from typing import TYPE_CHECKING
5+
6+
if TYPE_CHECKING:
7+
from main import Bot
8+
else:
9+
from twitchio.ext.commands import Bot
10+
411
class MyComponent(commands.Component):
512
def __init__(self, bot: commands.Bot) -> None:
613
# Passing args is not required...
714
# We pass bot here as an example...
815
self.bot = bot
916

1017
@commands.command(aliases=["hello", "howdy", "hey"])
11-
async def hi(self, ctx: commands.Context) -> None:
18+
async def hi(self, ctx: commands.Context[Bot]) -> None:
1219
"""Simple command that says hello!
1320
1421
!hi, !hello, !howdy, !hey
1522
"""
1623
await ctx.reply(f"Hello {ctx.chatter.mention}!")
1724

1825
@commands.group(invoke_fallback=True)
19-
async def socials(self, ctx: commands.Context) -> None:
26+
async def socials(self, ctx: commands.Context[Bot]) -> None:
2027
"""Group command for our social links.
2128
2229
!socials
2330
"""
2431
await ctx.send("discord.gg/..., youtube.com/..., twitch.tv/...")
2532

2633
@socials.command(name="discord")
27-
async def socials_discord(self, ctx: commands.Context) -> None:
34+
async def socials_discord(self, ctx: commands.Context[Bot]) -> None:
2835
"""Sub command of socials that sends only our discord invite.
2936
3037
!socials discord
@@ -33,7 +40,7 @@ async def socials_discord(self, ctx: commands.Context) -> None:
3340

3441
@commands.command(aliases=["repeat"])
3542
@commands.is_moderator()
36-
async def say(self, ctx: commands.Context, *, content: str) -> None:
43+
async def say(self, ctx: commands.Context[Bot], *, content: str) -> None:
3744
"""Moderator only command which repeats back what you say.
3845
3946
!say hello world, !repeat I am cool LUL

examples/module_examples/standard/components/owner_cmds.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
from twitchio.ext import commands
2+
from typing import TYPE_CHECKING
3+
4+
if TYPE_CHECKING:
5+
from main import Bot
6+
else:
7+
from twitchio.ext.commands import Bot
28

39
# Custom Exception for our component guard.
410
class NotOwnerError(commands.GuardFailure): ...
@@ -22,30 +28,30 @@ async def component_command_error(self, payload: commands.CommandErrorPayload) -
2228

2329
# Restrict all of the commands in this component to the owner.
2430
@commands.Component.guard()
25-
def is_owner(self, ctx: commands.Context) -> bool:
31+
def is_owner(self, ctx: commands.Context[Bot]) -> bool:
2632
if ctx.chatter.id != self.bot.owner_id:
2733
raise NotOwnerError
2834

2935
return True
3036

3137
# Manually load the cmds module.
3238
@commands.command()
33-
async def load_cmds(self, ctx: commands.Context) -> None:
39+
async def load_cmds(self, ctx: commands.Context[Bot]) -> None:
3440
await self.bot.load_module("components.cmds")
3541

3642
# Manually unload the cmds module.
3743
@commands.command()
38-
async def unload_cmds(self, ctx: commands.Context) -> None:
44+
async def unload_cmds(self, ctx: commands.Context[Bot]) -> None:
3945
await self.bot.unload_module("components.cmds")
4046

4147
# Hot reload the cmds module atomically.
4248
@commands.command()
43-
async def reload_cmds(self, ctx: commands.Context) -> None:
49+
async def reload_cmds(self, ctx: commands.Context[Bot]) -> None:
4450
await self.bot.reload_module("components.cmds")
4551

4652
# Check which modules are loaded.
4753
@commands.command()
48-
async def loaded_modules(self, ctx: commands.Context) -> None:
54+
async def loaded_modules(self, ctx: commands.Context[Bot]) -> None:
4955
print(self.bot.modules)
5056

5157
# This is our entry point for the module.

0 commit comments

Comments
 (0)