Skip to content

Commit 30cb54f

Browse files
committed
Merge foods and newfoods, tidy code, fix remind.py
1 parent 2d5f7bc commit 30cb54f

4 files changed

Lines changed: 101 additions & 125 deletions

File tree

plugins/attacks.py

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,31 @@
88
from cloudbot import hook
99
from cloudbot.util import textgen
1010

11+
nick_re = re.compile("^[A-Za-z0-9_|.-\]\[]*$", re.I)
12+
13+
14+
def is_valid(target):
15+
""" Checks if a string is a valid IRC nick. """
16+
if nick_re.match(target):
17+
return True
18+
else:
19+
return False
20+
21+
22+
def is_self(conn, target):
23+
""" Checks if a string is "****self" or contains conn.name. """
24+
if re.search("(^..?.?.?self|{})".format(re.escape(conn.nick)), target, re.I):
25+
return True
26+
else:
27+
return False
28+
1129

1230
@hook.on_start()
1331
def load_attacks(bot):
1432
"""
1533
:type bot: cloudbot.bot.CloudBot
1634
"""
17-
global larts, insults, flirts, kills, slaps
35+
global larts, flirts, kills, slaps
1836

1937
with codecs.open(os.path.join(bot.data_dir, "larts.txt"), encoding="utf-8") as f:
2038
larts = [line.strip() for line in f.readlines() if not line.startswith("//")]
@@ -29,33 +47,17 @@ def load_attacks(bot):
2947
slaps = json.load(f)
3048

3149

32-
def is_self(conn, target):
33-
"""
34-
:type conn: cloudbot.client.Client
35-
:type target: str
36-
"""
37-
if re.search("(^..?.?.?self|{})".format(re.escape(conn.nick.lower())), target.lower()):
38-
return True
39-
else:
40-
return False
41-
42-
4350
@asyncio.coroutine
44-
@hook.command()
45-
def lart(text, conn, nick, notice, action):
46-
"""<user> - LARTs <user>
47-
:type text: str
48-
:type conn: cloudbot.client.Client
49-
:type nick: str
50-
"""
51+
@hook.command
52+
def lart(text, conn, nick, action):
53+
"""<user> - LARTs <user>"""
5154
target = text.strip()
5255

53-
if " " in target:
54-
notice("Invalid username!")
55-
return
56+
if not is_valid(target):
57+
return "I can't attack that."
5658

57-
# if the user is trying to make the bot target itself, target them
5859
if is_self(conn, target):
60+
# user is trying to make the bot attack itself!
5961
target = nick
6062

6163
phrase = random.choice(larts)
@@ -65,42 +67,32 @@ def lart(text, conn, nick, notice, action):
6567

6668

6769
@asyncio.coroutine
68-
@hook.command()
69-
def flirt(text, conn, nick, notice, message):
70-
"""<user> - flirts with <user>
71-
:type text: str
72-
:type conn: cloudbot.client.Client
73-
:type nick: str
74-
"""
70+
@hook.command
71+
def flirt(text, conn, nick, message):
72+
"""<user> - flirts with <user>"""
7573
target = text.strip()
7674

77-
# if the user is trying to make the bot target itself, target them
78-
if " " in target:
79-
notice("Invalid username!")
80-
return
75+
if not is_valid(target):
76+
return "I can't attack that."
8177

8278
if is_self(conn, target):
79+
# user is trying to make the bot attack itself!
8380
target = nick
8481

8582
message('{}, {}'.format(target, random.choice(flirts)))
8683

8784

8885
@asyncio.coroutine
89-
@hook.command()
90-
def kill(text, conn, nick, notice, action):
91-
"""<user> - kills <user>
92-
:type text: str
93-
:type conn: cloudbot.client.Client
94-
:type nick: str
95-
"""
86+
@hook.command
87+
def kill(text, conn, nick, action):
88+
"""<user> - kills <user>"""
9689
target = text.strip()
9790

98-
if " " in target:
99-
notice("Invalid username!")
100-
return
91+
if not is_valid(target):
92+
return "I can't attack that."
10193

102-
# if the user is trying to make the bot kill itself, kill them
10394
if is_self(conn, target):
95+
# user is trying to make the bot attack itself!
10496
target = nick
10597

10698
generator = textgen.TextGenerator(kills["templates"], kills["parts"], variables={"user": target})
@@ -109,17 +101,17 @@ def kill(text, conn, nick, notice, action):
109101
action(generator.generate_string())
110102

111103

104+
@asyncio.coroutine
112105
@hook.command
113106
def slap(text, action, nick, conn, notice):
114-
"""slap <user> -- Makes the bot slap <user>."""
107+
"""<user> -- Makes the bot slap <user>."""
115108
target = text.strip()
116109

117-
if " " in target:
118-
notice("Invalid username!")
119-
return
110+
if not is_valid(target):
111+
return "I can't attack that."
120112

121-
# if the user is trying to make the bot slap itself, slap them
122-
if target.lower() == conn.nick.lower() or target.lower() == "itself":
113+
if is_self(conn, target):
114+
# user is trying to make the bot attack itself!
123115
target = nick
124116

125117
variables = {

plugins/foods.py

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import re
2-
import asyncio
1+
import codecs
2+
import json
3+
import os
34
import random
5+
import asyncio
6+
import re
47

58
from cloudbot import hook
9+
from cloudbot.util import textgen
10+
11+
nick_re = re.compile("^[A-Za-z0-9_|.-\]\[]*$", re.I)
612

713
cakes = ['Chocolate', 'Ice Cream', 'Angel', 'Boston Cream', 'Birthday', 'Bundt', 'Carrot', 'Coffee', 'Devils', 'Fruit',
814
'Gingerbread', 'Pound', 'Red Velvet', 'Stack', 'Welsh', 'Yokan']
@@ -47,33 +53,51 @@
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
5276
def 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
7195
def 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
91115
def 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())

plugins/newfoods.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

plugins/remind.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def remind(text, nick, chan, db, conn, notice, async):
126126
notice(remind.__doc__)
127127
return
128128

129-
count = [x for x in reminder_cache if x[0] == conn.name and x[3] == nick.lower()].count()
129+
count = len([x for x in reminder_cache if x[0] == conn.name and x[3] == nick.lower()])
130130

131131
time_string = parts[0].strip()
132132
message = colors.strip_all(parts[1].strip())

0 commit comments

Comments
 (0)