Skip to content

Commit 4d709e4

Browse files
committed
Add custom prefix FAQ
1 parent 9e51ddb commit 4d709e4

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

docs/getting-started/faq.rst

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,37 @@ If you do not know your user ID you can quickly fetch it using the :meth:`~twitc
107107
print(f"User: {u.name} - ID: {u.id}")
108108
109109
if __name__ == "__main__":
110-
asyncio.run(main())
110+
asyncio.run(main())
111+
112+
113+
How do I create a custom prefix(es) for Bot/AutoBot
114+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115+
116+
:class:`~twitchio.ext.commands.Bot` and :class:`~twitchio.ext.commands.AutoBot` both allow a custom co-routine to be used
117+
to determine the prefix for Chat Commands. This coroutine can be used to assign prefixes based on channel, chatter or other
118+
variables. A small example is shown below:
119+
120+
.. code:: python3
121+
122+
from typing import Self
123+
124+
import twitchio
125+
from twitchio.ext import commands
126+
127+
class Bot(commands.Bot):
128+
def __init__(self) -> None:
129+
super().__init__(..., prefix=self.custom_prefix)
130+
131+
async def custom_prefix(self, bot: Self, message: twitchio.ChatMessage) -> None:
132+
# The prefix will be ? if the chatters name startswith "cool"
133+
# Otherwise it will default to "!"
134+
# This coroutine can be used to connect to a cache or database etc to provide
135+
# custom settable prefixes for example...
136+
137+
if message.chatter.name.startswith("cool"):
138+
return "?"
139+
140+
return "!"
141+
142+
143+
The prefix can also be passed as or returned from this function as a list of :class:`str` to allow multiple prefixes to be used.

0 commit comments

Comments
 (0)