1- import re
2- import asyncio
1+ import codecs
2+ import json
3+ import os
34import random
5+ import asyncio
6+ import re
47
58from cloudbot import hook
9+ from cloudbot .util import textgen
10+
11+ nick_re = re .compile ("^[A-Za-z0-9_|.-\]\[]*$" , re .I )
612
713cakes = ['Chocolate' , 'Ice Cream' , 'Angel' , 'Boston Cream' , 'Birthday' , 'Bundt' , 'Carrot' , 'Coffee' , 'Devils' , 'Fruit' ,
814 'Gingerbread' , 'Pound' , 'Red Velvet' , 'Stack' , 'Welsh' , 'Yokan' ]
4753 'Yukon Gold' ]
4854
4955
56+ def is_valid (target ):
57+ """ Checks if a string is a valid IRC nick. """
58+ if nick_re .match (target ):
59+ return True
60+ else :
61+ return False
62+
63+ @hook .on_start ()
64+ def load_foods (bot ):
65+ """
66+ :type bot: cloudbot.bot.CloudBot
67+ """
68+ global sandwich_data
69+
70+ with codecs .open (os .path .join (bot .data_dir , "sandwich.json" ), encoding = "utf-8" ) as f :
71+ sandwich_data = json .load (f )
72+
73+
5074@asyncio .coroutine
51- @hook .command ()
75+ @hook .command
5276def potato (text , action ):
5377 """<user> - makes <user> a tasty little potato"""
54- text = text .strip ()
78+ user = text .strip ()
5579
56- if not re . match ( "^[A-Za-z0-9_|.-\]\[]*$" , text . lower () ):
57- return "I cant make a tasty potato for that user! "
80+ if not is_valid ( user ):
81+ return "I can't give a potato to that user. "
5882
5983 potato_type = random .choice (potatoes )
6084 size = random .choice (['small' , 'little' , 'mid-sized' , 'medium-sized' , 'large' , 'gigantic' ])
6185 flavor = random .choice (['tasty' , 'delectable' , 'delicious' , 'yummy' , 'toothsome' , 'scrumptious' , 'luscious' ])
6286 method = random .choice (['bakes' , 'fries' , 'boils' , 'roasts' ])
6387 side_dish = random .choice (['side salad' , 'dollop of sour cream' , 'piece of chicken' , 'bowl of shredded bacon' ])
6488
65- action ("{} a {} {} {} potato for {} and serves it with a small {}!" .format (method , flavor , size , potato_type , text ,
89+ action ("{} a {} {} {} potato for {} and serves it with a small {}!" .format (method , flavor , size , potato_type , user ,
6690 side_dish ))
6791
6892
6993@asyncio .coroutine
70- @hook .command ()
94+ @hook .command
7195def cake (text , action ):
7296 """<user> - gives <user> an awesome cake"""
73- text = text .strip ()
97+ user = text .strip ()
7498
75- if not re . match ( "^[A-Za-z0-9_|.-\]\[]*$" , text . lower () ):
76- return "I can't give an awesome cake to that user! "
99+ if not is_valid ( user ):
100+ return "I can't give a cake to that user. "
77101
78102 cake_type = random .choice (cakes )
79103 size = random .choice (['small' , 'little' , 'mid-sized' , 'medium-sized' , 'large' , 'gigantic' ])
@@ -82,24 +106,40 @@ def cake(text, action):
82106 side_dish = random .choice (['glass of chocolate milk' , 'bowl of ice cream' , 'jar of cookies' ,
83107 'some chocolate sauce' ])
84108
85- action ("{} {} a {} {} {} cake and serves it with a small {}!" .format (method , text , flavor , size , cake_type ,
109+ action ("{} {} a {} {} {} cake and serves it with a small {}!" .format (method , user , flavor , size , cake_type ,
86110 side_dish ))
87111
88112
89113@asyncio .coroutine
90- @hook .command ()
114+ @hook .command
91115def cookie (text , action ):
92116 """<user> - gives <user> a cookie"""
93- text = text .strip ()
117+ user = text .strip ()
94118
95- if not re . match ( "^[A-Za-z0-9_|.-\]\[]*$" , text . lower () ):
96- return "I can't give a cookie to that user! "
119+ if not is_valid ( user ):
120+ return "I can't give a cookie to that user. "
97121
98122 cookie_type = random .choice (cookies )
99123 size = random .choice (['small' , 'little' , 'medium-sized' , 'large' , 'gigantic' ])
100124 flavor = random .choice (['tasty' , 'delectable' , 'delicious' , 'yummy' , 'toothsome' , 'scrumptious' , 'luscious' ])
101125 method = random .choice (['makes' , 'gives' , 'gets' , 'buys' ])
102126 side_dish = random .choice (['glass of milk' , 'bowl of ice cream' , 'bowl of chocolate sauce' ])
103127
104- action ("{} {} a {} {} {} cookie and serves it with a {}!" .format (method , text , flavor , size , cookie_type ,
128+ action ("{} {} a {} {} {} cookie and serves it with a {}!" .format (method , user , flavor , size , cookie_type ,
105129 side_dish ))
130+
131+
132+ @asyncio .coroutine
133+ @hook .command
134+ def sandwich (text , action ):
135+ """<user> - give a tasty sandwich to <user>"""
136+ user = text .strip ()
137+
138+ if not is_valid (user ):
139+ return "I can't give a cookie to that user."
140+
141+ generator = textgen .TextGenerator (sandwich_data ["templates" ], sandwich_data ["parts" ],
142+ variables = {"user" : user })
143+
144+ # act out the message
145+ action (generator .generate_string ())
0 commit comments