Skip to content

Commit ca5fc04

Browse files
committed
Update docs and add examples
Update typos in docs Add examples folder and exmaples
1 parent f32830d commit ca5fc04

File tree

7 files changed

+140
-5
lines changed

7 files changed

+140
-5
lines changed

docs/exts/eventsub.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,52 @@ A Quick Example
3939
bot.loop.create_task(bot.start())
4040
bot.loop.run_forever()
4141
42+
43+
Running Eventsub Inside a Commands Bot
44+
---------------------------------------
45+
46+
.. code-block:: python3
47+
48+
import twitchio
49+
from twitchio.ext import commands, eventsub
50+
51+
esbot = commands.Bot.from_client_credentials(client_id='...',
52+
client_secret='...')
53+
esclient = eventsub.EventSubClient(esbot,
54+
webhook_secret='...',
55+
callback_route='https://your-url.here/callback')
56+
57+
58+
class Bot(commands.Bot):
59+
60+
def __init__(self):
61+
super().__init__(token='...', prefix='!', initial_channels=['channel'])
62+
63+
async def __ainit__(self) -> None:
64+
self.loop.create_task(esclient.listen(port=4000))
65+
66+
try:
67+
await esclient.subscribe_channel_follows(broadcaster=channel_ID)
68+
except twitchio.HTTPException:
69+
pass
70+
71+
async def event_ready(self):
72+
print('Bot is ready!')
73+
74+
75+
bot = Bot()
76+
bot.loop.run_until_complete(bot.__ainit__())
77+
78+
79+
@esbot.event()
80+
async def event_eventsub_notification_follow(payload: eventsub.ChannelFollowData) -> None:
81+
print('Received event!')
82+
channel = bot.get_channel('channel')
83+
await channel.send(f'{payload.user.name} followed woohoo!')
84+
85+
bot.run()
86+
87+
4288
Event Reference
4389
----------------
4490
This is a list of events dispatched by the eventsub ext.

docs/exts/routines.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ routine.
4242
4343
@routines.routine(hours=1)
4444
async def hello():
45-
print('Hello World!)
45+
print('Hello World!')
4646
4747
@hello.before_routine
4848
async def hello_before():
4949
print('I am run first!')
5050
5151
52-
@hello.start()
52+
hello.start()
5353
5454
5555
**Routine with an error handler:**
@@ -86,7 +86,7 @@ It will then be ran every 24 hours after the initial date, until stopped.
8686

8787
If the **date** has already passed, the routine will run at the next specified time.
8888
For example: If today was the **2nd, June 2021 8:30am** and your datetime was scheduled to run on the
89-
**1st, June 2021 at 9:30am**, you routine will first run on **2nd, June 2021 at 9:30am**.
89+
**1st, June 2021 at 9:30am**, your routine will first run on **2nd, June 2021 at 9:30am**.
9090

9191
In simpler terms, datetimes in the past only care about the time, not the date. This can be useful when scheduling
9292
routines that don't need to be started on a specific date.

docs/exts/sounds.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Sounds Ext
99
Sounds is an extension to easily play sounds on your local machine plugged directly into your bot.
1010
Sounds is currently a Beta release, and as such should be treated so.
1111

12-
Currently sounds supports local files, and YouTube searches. See below for more details.
12+
Currently sounds supports local files and YouTube searches. See below for more details.
1313

1414
Sounds requires a few extra steps to get started, below is a short guide on how to get started with sounds:
1515

examples/basic_bot.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from twitchio.ext import commands
2+
3+
4+
class Bot(commands.Bot):
5+
6+
def __init__(self):
7+
# Initialise our Bot with our access token, prefix and a list of channels to join on boot...
8+
# prefix can be a callable, which returns a list of strings or a string...
9+
# initial_channels can also be a callable which returns a list of strings...
10+
super().__init__(token='ACCESS_TOKEN', prefix='?', initial_channels=['...'])
11+
12+
async def event_ready(self):
13+
# Notify us when everything is ready!
14+
# We are logged in and ready to chat and use commands...
15+
print(f'Logged in as | {self.nick}')
16+
print(f'User id is | {self.user_id}')
17+
18+
async def event_message(self, message):
19+
# Messages with echo set to True are messages sent by the bot...
20+
# For now we just want to ignore them...
21+
if message.echo:
22+
return
23+
24+
# Print the contents of our message to console...
25+
print(message.content)
26+
27+
# Since we have commands and are overriding the default `event_message`
28+
# We must let the bot know we want to handle and invoke our commands...
29+
await self.handle_commands(message)
30+
31+
@commands.command()
32+
async def hello(self, ctx: commands.Context):
33+
# Here we have a command hello, we can invoke our command with our prefix and command name
34+
# e.g ?hello
35+
# We can also give our commands aliases (different names) to invoke with.
36+
37+
# Send a hello back!
38+
# Sending a reply back to the channel is easy... Below is an example.
39+
await ctx.send(f'Hello {ctx.author.name}!')
40+
41+
42+
bot = Bot()
43+
bot.run()
44+
# bot.run() is blocking and will stop execution of any below code here until stopped or closed.

examples/basic_routine.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from twitchio.ext import routines
2+
3+
# This routine will run every 5 seconds for 5 iterations.
4+
@routines.routine(seconds=5.0, iterations=5)
5+
async def hello(arg: str):
6+
print(f'Hello {arg}!')
7+
8+
9+
hello.start('World')

examples/eventsub.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import twitchio
2+
from twitchio.ext import commands, eventsub
3+
4+
esbot = commands.Bot.from_client_credentials(client_id="...", client_secret="...")
5+
esclient = eventsub.EventSubClient(esbot, webhook_secret="...", callback_route="https://your-url.here/callback")
6+
7+
8+
class Bot(commands.Bot):
9+
def __init__(self):
10+
super().__init__(token="...", prefix="!", initial_channels=["channel"])
11+
12+
async def __ainit__(self) -> None:
13+
self.loop.create_task(esclient.listen(port=4000))
14+
try:
15+
await esclient.subscribe_channel_follows(broadcaster=channel_ID)
16+
except twitchio.HTTPException:
17+
pass
18+
19+
async def event_ready(self):
20+
print("Bot is ready!")
21+
22+
23+
bot = Bot()
24+
bot.loop.run_until_complete(bot.__ainit__())
25+
26+
27+
@esbot.event()
28+
async def event_eventsub_notification_follow(payload: eventsub.ChannelFollowData) -> None:
29+
print("Received event!")
30+
channel = bot.get_channel("channel")
31+
await channel.send(f"{payload.user.name} followed woohoo!")
32+
33+
34+
bot.run()

twitchio/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,9 @@ async def fetch_clips(self, ids: List[str]):
432432
return [models.Clip(self._http, d) for d in data]
433433

434434
async def fetch_channel(self, broadcaster: str):
435-
"""Retrieve channel information from the API.
435+
"""|coro|
436+
437+
Retrieve channel information from the API.
436438
437439
Parameters
438440
-----------

0 commit comments

Comments
 (0)