talkomatic.py
0.1.2
An easy-to-use Python versatile wrapper for the Talkomatic Bot interface ensuring best practices for making bots, and handling the REST API.
Loading...
Searching...
No Matches
echo.py
Go to the documentation of this file.
1
"""
2
echo.py
3
4
This example shows how to create a commands with an argument that repeats
5
whatever the user says.
6
"""
7
8
from
talkomatic
import
Bot, CommandParameter, RoomType, User
9
from
talkomatic.commands
import
INFINITE_ARGS
10
11
bot =
Bot
()
12
13
# with this decorator, the function will be called when the bot finishes connecting to the server
14
@bot.on_connect
15
async def
on_connect
() -> None:
16
# we find the first non-full public room, we join it, and say "Hello, world!"
17
for
room
in
bot.rooms:
18
if
not
room.is_full
and
room.room_type == RoomType.PUBLIC:
19
await bot.join_room(room)
20
return
21
print(
"We didn't find any non-full rooms to join. :("
)
# :(
22
await bot.disconnect()
23
24
user_messages = {}
25
async def
update_user_messages
():
26
# we concatenate all the user messages into one message to send
27
message =
""
28
for
user_message
in
user_messages.values():
29
message += f
"{user_message}\n"
30
await bot.send_message(message)
31
32
@bot.command
(
# this is a decorator that turns the echo function into a command
33
name =
"echo"
,
# the name of the command
34
description =
"This command repeats your message!"
,
# the description of the command
35
parameters = [
# the parameters of the command
36
CommandParameter
(
# we want a parameter that takes any number of words in the message
37
"message"
,
# the name of the parameter
38
"The message to repeat."
,
# the description of the parameter
39
str,
# the type of the parameter
40
positional =
True
,
# the parameter is positional
41
required =
True
,
# the parameter is required
42
number_of_args = INFINITE_ARGS
# the parameter can take any number of words
43
)
44
]
45
)
46
async def
echo
(user: User, message) ->
None
:
47
if
not
isinstance(message, list):
# is the user sending a message in the /echo
48
if
user.id
in
user_messages:
# if
49
del user_messages[user.id]
50
else
:
51
# for each user, we store the message that'll be sent
52
user_messages[user.id] = f
"{user.username} / {user.location} said: {' '.join(message)}"
53
54
# update the user messages
55
await
update_user_messages
()
56
57
@bot.on_user_leave
58
async def
on_user_leave
(user: User) ->
None
:
59
if
user.id
in
user_messages:
# if they're in one of our user messages dictionary
60
del user_messages[user.id]
# when a user leaves, we delete them from the user messages
61
62
# update the user messages
63
await
update_user_messages
()
64
65
# we can even make a "hidden" command that doesn't show up in the /help command
66
@bot.command(name = "ping", hidden = True)
67
async def
ping
(user: User) ->
None
:
68
# we can make little easter eggs with this hidden command feature!
69
await bot.send_message(
"Pong!"
)
70
71
72
bot.run(
"Echo Bot"
,
"do /help"
, create_help_command =
True
)
# we let the bot create the help command automatically
talkomatic.bot.Bot
Definition
bot.py:16
talkomatic.commands.command.CommandParameter
Definition
command.py:18
echo
Definition
echo.py:1
echo.on_connect
None on_connect()
Definition
echo.py:15
echo.on_user_leave
None on_user_leave(User user)
Definition
echo.py:58
echo.ping
None ping(User user)
Definition
echo.py:67
echo.update_user_messages
update_user_messages()
Definition
echo.py:25
talkomatic.commands
Definition
__init__.py:1
examples
echo.py
Generated by
1.13.2