forked from edwardslabs/CloudBot
/
attacks.py
190 lines (139 loc) · 4.96 KB
/
attacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import codecs
import json
import os
import random
import asyncio
import re
from cloudbot import hook
from cloudbot.util import textgen
nick_re = re.compile("^[A-Za-z0-9_|.\-\]\[\{\}]*$", re.I)
def is_valid(target):
""" Checks if a string is a valid IRC nick. """
if nick_re.match(target):
return True
else:
return False
def is_self(conn, target):
""" Checks if a string is "****self" or contains conn.name. """
if re.search("(^..?.?.?self|{})".format(re.escape(conn.nick)), target, re.I):
return True
else:
return False
@hook.on_start()
def load_attacks(bot):
"""
:type bot: cloudbot.bot.CloudBot
"""
global larts, flirts, kills, slaps, north_korea, insults, strax
with codecs.open(os.path.join(bot.data_dir, "larts.txt"), encoding="utf-8") as f:
larts = [line.strip() for line in f.readlines() if not line.startswith("//")]
with codecs.open(os.path.join(bot.data_dir, "flirts.txt"), encoding="utf-8") as f:
flirts = [line.strip() for line in f.readlines() if not line.startswith("//")]
with codecs.open(os.path.join(bot.data_dir, "insults.txt"), encoding="utf-8") as f:
insults = [line.strip() for line in f.readlines() if not line.startswith("//")]
with codecs.open(os.path.join(bot.data_dir, "kills.json"), encoding="utf-8") as f:
kills = json.load(f)
with codecs.open(os.path.join(bot.data_dir, "slaps.json"), encoding="utf-8") as f:
slaps = json.load(f)
with codecs.open(os.path.join(bot.data_dir, "strax.json"), encoding="utf-8") as f:
strax = json.load(f)
with codecs.open(os.path.join(bot.data_dir, "north_korea.txt"), encoding="utf-8") as f:
north_korea = [line.strip() for line in f.readlines() if not line.startswith("//")]
@asyncio.coroutine
@hook.command
def lart(text, conn, nick, action):
"""<user> - LARTs <user>"""
target = text.strip()
if not is_valid(target):
return "I can't attack that."
if is_self(conn, target):
# user is trying to make the bot attack itself!
target = nick
phrase = random.choice(larts)
# act out the message
action(phrase.format(user=target))
@asyncio.coroutine
@hook.command("flirt", "sexup", "jackmeoff")
def flirt(text, conn, nick, message):
"""<user> - flirts with <user>"""
target = text.strip()
if not is_valid(target):
return "I can't attack that."
if is_self(conn, target):
# user is trying to make the bot attack itself!
target = nick
message('{}, {}'.format(target, random.choice(flirts)))
@asyncio.coroutine
@hook.command
def kill(text, conn, nick, action):
"""<user> - kills <user>"""
target = text.strip()
if not is_valid(target):
return "I can't attack that."
if is_self(conn, target):
# user is trying to make the bot attack itself!
target = nick
generator = textgen.TextGenerator(kills["templates"], kills["parts"], variables={"user": target})
# act out the message
action(generator.generate_string())
@asyncio.coroutine
@hook.command
def slap(text, action, nick, conn, notice):
"""<user> -- Makes the bot slap <user>."""
target = text.strip()
if not is_valid(target):
return "I can't attack that."
if is_self(conn, target):
# user is trying to make the bot attack itself!
target = nick
variables = {
"user": target
}
generator = textgen.TextGenerator(slaps["templates"], slaps["parts"], variables=variables)
# act out the message
action(generator.generate_string())
@hook.command(autohelp=False)
def strax(text, conn, message, nick):
"""Strax quote."""
if text:
target = text.strip()
if not is_valid(target):
return "I can't attack that."
if is_self(conn, target):
# user is trying to make the bot attack itself!
target = nick
variables = {
p = "on"
p += target
# Outputs to "on target"
# Because this will be easier to understand
target = p
"user": target
}
generator = textgen.TextGenerator(strax["target_template"], strax["parts"])
else:
generator = textgen.TextGenerator(strax["template"], strax["parts"])
# Become Strax
message(generator.generate_string())
@hook.command(autohelp=False)
def nk(chan, message):
"""outputs a random North Korea propoganda slogan"""
index = random.randint(0,len(north_korea)-1)
slogan = north_korea[index]
message(slogan, chan)
@asyncio.coroutine
@hook.command()
def insult(text, conn, nick, notice, message):
"""<user> - insults <user>
:type text: str
:type conn: cloudbot.client.Client
:type nick: str
"""
target = text.strip()
if " " in target:
notice("Invalid username!")
return
# if the user is trying to make the bot target itself, target them
if is_self(conn, target):
target = nick
message("{}, {}".format(target, random.choice(insults)))