/
ducks.py
252 lines (207 loc) · 9.85 KB
/
ducks.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#--depends-on commands
#--depends-on config
#--depends-on coins
import random, re, time
import decimal
from src import EventManager, ModuleManager, utils
DUCK = "・゜゜・。。・゜゜\_o< QUACK!"
DEFAULT_MIN_MESSAGES = 50
@utils.export("channelset", utils.BoolSetting("ducks-enabled",
"Whether or not to spawn ducks"))
@utils.export("channelset", utils.IntRangeSetting(50, 200, "ducks-min-messages",
"Minimum messages between ducks spawning"))
@utils.export("channelset", utils.BoolSetting("ducks-kick",
"Whether or not to kick someone talking to non-existent ducks"))
@utils.export("channelset", utils.BoolSetting("ducks-prevent-highlight",
"Whether or not to prevent highlighting users with !friends/!enemies"))
@utils.export("channelset", utils.IntRangeSetting(0, 100, "ducks-fine-percentage", "The fine amount the users get fined when you shoot a duck"))
class Module(ModuleManager.BaseModule):
@utils.hook("new.channel")
def new_channel(self, event):
self.bootstrap_channel(event["channel"])
def bootstrap_channel(self, channel):
if not hasattr(channel, "duck_active"):
channel.duck_active = None
channel.duck_lines = 0
def _activity(self, channel):
self.bootstrap_channel(channel)
ducks_enabled = channel.get_setting("ducks-enabled", False)
if (ducks_enabled and
not channel.duck_active and
not channel.duck_lines == -1):
channel.duck_lines += 1
min_lines = channel.get_setting("ducks-min-messages",
DEFAULT_MIN_MESSAGES)
if channel.duck_lines >= min_lines:
show_duck = random.SystemRandom().randint(1, 20) == 1
if show_duck:
self._trigger_duck(channel)
@utils.hook("received.command.duckappear")
@utils.kwarg("help", "Create ducks")
@utils.kwarg("permission", "duck")
def ducks_magic_debug(self, event):
self._trigger_duck(event["target"])
@utils.hook("command.regex")
@utils.kwarg("expect_output", False)
@utils.kwarg("ignore_action", False)
@utils.kwarg("command", "duck-trigger")
@utils.kwarg("pattern", re.compile(".+"))
def channel_message(self, event):
self._activity(event["target"])
def _trigger_duck(self, channel):
channel.duck_lines = -1
delay = random.SystemRandom().randint(5, 20)
self.timers.add("duck", self._send_duck, delay, channel=channel)
def _send_duck(self, timer):
channel = timer.kwargs["channel"]
channel.duck_active = time.time()
channel.duck_lines = 0
channel.send_message(DUCK)
def _total_coins(self, event):
coins = self.bot.modules.modules['coins'].module
return decimal.Decimal(sum(coins._all_coins(event['server']).values()))
@utils.hook("received.command.totalcoins")
@utils.kwarg("help", "Show total coins")
def duck_total_coins_amount(self, event):
event["stdout"].write(f"There are a total of: {self._total_coins(event)} coins")
def _duck_action(self, channel, user, action, setting, event, fine_enabled=False):
if channel.get_setting("ducks-fine-percentage", 0) != 0 and fine_enabled:
coins = self.bot.modules.modules['coins'].module
interest_percentage = channel.get_setting("ducks-fine-percentage",1)
total_coins = self._total_coins(event)
fine_amount = total_coins*(decimal.Decimal(0.01)*interest_percentage)
fine_amount = fine_amount.quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_05UP) # Round number to appropriate units
user_coins = coins._get_user_coins(user)
if user_coins < fine_amount:
return f"There was a duck, but you didn't have coins to pay the fine of {fine_amount} coins so you can't shoot the duck."
else:
new_user_coins = user_coins - fine_amount
coins._set_user_coins(user, new_user_coins)
duck_timestamp = channel.duck_active
channel.set_setting("duck-last", time.time())
channel.duck_active = None
user_id = user.get_id()
action_count = channel.get_user_setting(user_id, setting, 0)
action_count += 1
channel.set_user_setting(user_id, setting, action_count)
seconds = round(time.time()-duck_timestamp, 2)
ducks_plural = "duck" if action_count == 1 else "ducks"
return "%s %s a duck in %s seconds! You've %s %d %s in %s!" % (
user.nickname, action, seconds, action, action_count, ducks_plural,
channel.name)
def _no_duck(self, channel, user, stderr):
message = "There was no duck!"
duck_timestamp = channel.get_setting("duck-last", None)
if not duck_timestamp == None:
seconds = round(time.time()-duck_timestamp, 2)
message += " missed by %s seconds" % seconds
if channel.get_setting("ducks-kick"):
channel.send_kick(user.nickname, message)
else:
stderr.write("%s: %s" % (user.nickname, message))
@utils.hook("received.command.bef", alias_of="befriend")
@utils.hook("received.command.befriend")
@utils.kwarg("help", "Befriend a duck")
@utils.spec("!-channelonly")
def befriend(self, event):
if event["target"].duck_active:
action = self._duck_action(event["target"], event["user"],
"befriended", event, "ducks-befriended")
event["stdout"].write(action)
else:
self._no_duck(event["target"], event["user"], event["stderr"])
@utils.hook("received.command.trap")
@utils.kwarg("help", "Trap a duck")
@utils.spec("!-channelonly")
def trap(self, event):
if event["target"].duck_active:
action = self._duck_action(event["target"], event["user"],
"trapped", "ducks-shot", event, fine_enabled=True)
event["stdout"].write(action)
else:
self._no_duck(event["target"], event["user"], event["stderr"])
@utils.hook("received.command.bang")
@utils.kwarg("help", "Shot a duck")
@utils.spec("!-channelonly")
def bang(self, event):
if event["target"].duck_active:
action = self._duck_action(event["target"], event["user"],
"shot", "ducks-shot", event, fine_enabled=True)
event["stdout"].write(action)
else:
self._no_duck(event["target"], event["user"], event["stderr"])
def _target(self, target, is_channel, query):
if query:
if not query == "*":
return query
elif is_channel:
return target.name
@utils.hook("received.command.friends")
@utils.kwarg("help", "Show top 10 duck friends")
@utils.spec("?<channel>word")
def friends(self, event):
query = self._target(event["target"], event["is_channel"],
event["spec"][0])
stats = self._top_duck_stats(event["server"], event["target"],
"ducks-befriended", "friends", query)
event["stdout"].write(stats)
@utils.hook("received.command.enemies")
@utils.kwarg("help", "Show top 10 duck enemies")
@utils.spec("?<channel>word")
def enemies(self, event):
query = self._target(event["target"], event["is_channel"],
event["spec"][0])
stats = self._top_duck_stats(event["server"], event["target"],
"ducks-shot", "enemies", query)
event["stdout"].write(stats)
def _top_duck_stats(self, server, target, setting, description,
channel_query):
channel_query_str = ""
if not channel_query == None:
channel_query = server.irc_lower(channel_query)
channel_query_str = " in %s" % channel_query
stats = server.find_all_user_channel_settings(setting)
user_stats = {}
for channel, nickname, value in stats:
if not channel_query or channel_query == channel:
if not nickname in user_stats:
user_stats[nickname] = 0
user_stats[nickname] += value
top_10 = utils.top_10(user_stats,
convert_key=lambda n: self._get_nickname(server, target, n))
return "Top duck %s%s: %s" % (description, channel_query_str,
", ".join(top_10))
def _get_nickname(self, server, target, nickname):
nickname = server.get_user(nickname).nickname
if target.get_setting("ducks-prevent-highlight", True):
nickname = utils.prevent_highlight(nickname)
return nickname
@utils.hook("received.command.duckstats")
@utils.kwarg("help", "Get yours, or someone else's, duck stats")
@utils.spec("?<nickname>ouser")
def duckstats(self, event):
target_user = event["spec"][0] or event["user"]
befs = target_user.get_channel_settings_per_setting(
"ducks-befriended")
traps = target_user.get_channel_settings_per_setting("ducks-shot")
all = [(chan, val, "bef") for chan, val in befs]
all += [(chan, val, "trap") for chan, val in traps]
current = {"bef": 0, "trap": 0, "shot": 0}
overall = {"bef": 0, "trap": 0, "shot": 0}
if event["is_channel"]:
for channel_name, value, action in all:
if not action in overall:
overall[action] = 0
overall[action] += value
if event["is_channel"]:
channel_name_lower = event["server"].irc_lower(channel_name)
if channel_name_lower == event["target"].name:
current[action] = value
current_str = ""
if current:
current_str = " (%d/%d in %s)" % (current["bef"],
current["trap"], event["target"].name)
event["stdout"].write(
"%s has befriended %d and shot %d ducks%s" %
(target_user.nickname, overall["bef"], overall["trap"],
current_str))