aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/guessing_game.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/examples/guessing_game.py b/examples/guessing_game.py
new file mode 100644
index 00000000..0cbf4a7e
--- /dev/null
+++ b/examples/guessing_game.py
@@ -0,0 +1,37 @@
+import discord
+import random
+
+client = discord.Client()
+
+def on_message(message):
+ # we do not want the bot to reply to itself
+ if message.author == client.user:
+ return
+
+ if message.content.startswith('$guess'):
+ yield from client.send_message(message.channel, 'Guess a number between 1 to 10')
+
+ def guess_check(m):
+ return m.content.isdigit()
+
+ guess = yield from client.wait_for_message(timeout=5.0, author=message.author, check=guess_check)
+ answer = random.randint(1, 10)
+ if guess is None:
+ fmt = 'Sorry, you took too long. It was {}.'
+ yield from client.send_message(message.channel, fmt.format(answer))
+ return
+ if int(guess.content) == answer:
+ yield from client.send_message(message.channel, 'You are right!')
+ else:
+ yield from client.send_message(message.channel, 'Sorry. It is actually {}.'.format(answer))
+
+
+def on_ready():
+ print('Logged in as')
+ print(client.user.name)
+ print(client.user.id)
+ print('------')
+
+client.run('email', 'password')