~nova/fletcher

fletcher/sentinel.py -rw-r--r-- 13.2 KiB
ece10afa — Novalinium f-string issue 9 days ago
                                                                                
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from sys import exc_info
import text_manipulators
import logging
import messagefuncs
from sys import exc_info
from datetime import datetime, timedelta

# global conn set by reload_function

logger = logging.getLogger("fletcher")

# Corresponds to the listbanners command
# Callable from DMs and Channels
# Dumps records from the sentinel table less than 30 days old
def listbanners_function(message, client, args):
    try:
        global conn
        cur = conn.cursor()
        cur.execute(
            "SELECT id, name, description, created, triggered, lastmodified, subscribers, triggercount FROM sentinel WHERE lastmodified > NOW() - INTERVAL '30 days';"
        )
        sentuple = cur.fetchone()
        bannerMessage = "Banner List:\n"
        while sentuple:
            bannerName = text_manipulators.smallcaps(sentuple[1])
            if bannerName is None or bannerName.strip() == "" or " " in bannerName:
                bannerName = "Banner {}".format(str(sentuple[0]))
            bannerMessage = bannerMessage + "**{}** ".format(bannerName)
            if sentuple[2]:
                bannerMessage = bannerMessage + sentuple[2]
            supporterPluralized = "supporters"
            if len(sentuple[6]) == 1:
                supporterPluralized = "supporter"
            goalAchievedVerb = "is"
            if sentuple[4]:
                goalAchievedVerb = "was"
            bannerMessage = bannerMessage + "\n{} {} · goal {} {}\nMᴀᴅᴇ: {}\n".format(
                str(len(sentuple[6])),
                supporterPluralized,
                goalAchievedVerb,
                str(sentuple[7]),
                sentuple[3].strftime("%Y-%m-%d %H:%M")
                + " ("
                + text_manipulators.pretty_date(sentuple[3])
                + ")",
            )
            if sentuple[4]:
                bannerMessage = bannerMessage + "Mᴇᴛ: {}\n".format(
                    sentuple[4].strftime("%Y-%m-%d %H:%M")
                    + " ("
                    + text_manipulators.pretty_date(sentuple[4])
                    + ")"
                )
            sentuple = cur.fetchone()
            if sentuple:
                bannerMessage = bannerMessage + "──────────\n"
        conn.commit()
        bammerMessage = bannerMessage.rstrip()
        if bannerMessage:
            return bannerMessage
        else:
            return "No banners modified within the last 30 days. Raise a sentinel with `!assemble`"
    except Exception as e:
        if "cur" in locals() and "conn" in locals():
            conn.rollback()
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error("LBF[{}]: {} {}".format(exc_tb.tb_lineno, type(e).__name__, e))


# Corresponds to the assemble command
# Callable from DMs and Channels
# Builds a record in the sentinel table, fails if it already exists
async def assemble_function(message, client, args):
    try:
        global conn
        bannerId = None
        bannerName = None
        triggerCount = None
        bannerDescription = None
        nameAutoGenerated = True
        cur = conn.cursor()
        try:
            triggerCount = int(args[0])
            bannerName = " ".join(args[1:])
            bannerDescription = bannerName
        except ValueError:
            triggerCount = int(args[1])
            bannerName = args[0]
            nameAutoGenerated = False
            bannerDescription = " ".join(args[2:])
            pass
        cur.execute(
            "INSERT INTO sentinel (name, description, lastModified, triggerCount) VALUES (%s, %s, %s, %s);",
            [bannerName, bannerDescription, datetime.now(), triggerCount],
        )
        cur.execute("SELECT id FROM sentinel WHERE name = %s", [bannerName])
        bannerId = cur.fetchone()[0]
        conn.commit()
        if nameAutoGenerated:
            return await messagefuncs.sendWrappedMessage(
                "Banner created! `!pledge {bannerId}` to commit to this pledge.",
                message.channel,
            )
        else:
            return await messagefuncs.sendWrappedMessage(
                f"Banner created! `!pledge {bannerName}` to commit to this pledge.",
                message.channel,
            )
    except Exception as e:
        if "cur" in locals() and "conn" in locals():
            conn.rollback()
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error("ABF[{}]: {} {}".format(exc_tb.tb_lineno, type(e).__name__, e))


# Corresponds to the pledge command
# Callable from DMs and Channels
# Requires sentinel record to exist
async def pledge_function(message, client, args):
    try:
        global conn
        bannerId = None
        sentuple = None
        cur = conn.cursor()
        try:
            cur.execute(
                "SELECT id FROM sentinel WHERE id = %s LIMIT 1;", [int(args[0])]
            )
            sentuple = cur.fetchone()
        except ValueError:
            pass
        if sentuple == None:
            cur.execute(
                "SELECT COUNT(id) FROM sentinel WHERE name ILIKE %s;", [" ".join(args)]
            )
            availableSentinels = cur.fetchone()[0]
            if availableSentinels == 0:
                conn.commit()
                return await messagefuncs.sendWrappedMessage(
                    f'No banner "{args[0]}" available. Try `!assemble [critical number of subscribers] {" ".join(args)}` to create one.',
                    message.channel,
                )
            elif availableSentinels == 1:
                cur.execute(
                    "SELECT id FROM sentinel WHERE name ILIKE %s;", [" ".join(args)]
                )
                sentuple = cur.fetchone()
            elif availableSentinels > 1:
                conn.commit()
                return await messagefuncs.sendWrappedMessage(
                    f"Not sure what you want to do: {availableSentinels} banners found containing {' '.join(args)} in the name.",
                    message.channel,
                )
        bannerId = sentuple[0]
        cur.execute(
            "SELECT COUNT(id) FROM sentinel WHERE id = %s AND %s = ANY (subscribers);",
            [bannerId, message.author.id],
        )
        if cur.fetchone()[0]:
            conn.commit()
            return await messagefuncs.sendWrappedMessage(
                f"You already committed to this banner. `!defect {' '.join(args)}` to defect.",
                message.channel,
            )
        else:
            cur.execute(
                "UPDATE sentinel SET subscribers = array_append(subscribers, %s), lastmodified = CURRENT_TIMESTAMP WHERE id = %s;",
                [message.author.id, bannerId],
            )
            cur.execute(
                "SELECT array_length(subscribers, 1), triggercount, name, subscribers, triggered FROM sentinel WHERE id = %s;",
                [bannerId],
            )
            bannerInfo = cur.fetchone()
            if bannerInfo[0] == bannerInfo[1]:  # Triggered banner! Yay!
                cur.execute(
                    "UPDATE sentinel SET triggered = CURRENT_TIMESTAMP WHERE id = %s;",
                    [bannerId],
                )
                conn.commit()
                return await messagefuncs.sendWrappedMessage(
                    "Critical mass reached for banner {}! Paging supporters: <@{}>. Now it's up to you to fulfill your goal :)".format(
                        bannerInfo[2],
                        ">, <@".join([str(userId) for userId in bannerInfo[3]]),
                    ),
                    message.channel,
                )
            elif bannerInfo[0] > bannerInfo[1]:
                conn.commit()
                return await messagefuncs.sendWrappedMessage(
                    "Critical mass was reached for banner {} at {}! You are the {} supporter.".format(
                        bannerInfo[2],
                        bannerInfo[4].strftime("%Y-%m-%d %H:%M:%S"),
                        text_manipulators.ordinal(bannerInfo[0]),
                    ),
                    message.channel,
                )
            else:
                conn.commit()
                return await messagefuncs.sendWrappedMessage(
                    "You pledged your support for banner {} (one of {} supporters). It needs {} more supporters to reach its goal.".format(
                        bannerInfo[2], bannerInfo[0], str(bannerInfo[1] - bannerInfo[0])
                    ),
                    message.channel,
                )
    except Exception as e:
        if "cur" in locals() and "conn" in locals():
            conn.rollback()
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error("PBF[{}]: {} {}".format(exc_tb.tb_lineno, type(e).__name__, e))


# Corresponds to the defect command
# Callable from DMs and Channels
# Requires sentinel record to exist
async def defect_function(message, client, args):
    try:
        global conn
        bannerId = None
        sentuple = None
        cur = conn.cursor()
        try:
            cur.execute(
                "SELECT id FROM sentinel WHERE id = %s LIMIT 1;", [int(args[0])]
            )
            sentuple = cur.fetchone()
        except ValueError:
            pass
        if sentuple == None:
            cur.execute(
                "SELECT COUNT(id) FROM sentinel WHERE name ILIKE %s;", [" ".join(args)]
            )
            availableSentinels = cur.fetchone()[0]
            if availableSentinels == 0:
                conn.commit()
                return await messagefuncs.sendWrappedMessage(
                    'No banner "{}" available. Try `!assemble [critical number of subscribers] {}` to create one.'.format(
                        args[0], " ".join(args)
                    ),
                    message.channel,
                )
            elif availableSentinels == 1:
                cur.execute(
                    "SELECT id FROM sentinel WHERE name ILIKE %s;", [" ".join(args)]
                )
                sentuple = cur.fetchone()
            elif availableSentinels > 1:
                conn.commit()
                return await messagefuncs.sendWrappedMessage(
                    "Not sure what you want to do: {} banners found containing {} in the name.".format(
                        str(availableSentinels), " ".join(args)
                    ),
                    message.channel,
                )
        bannerId = sentuple[0]
        cur.execute(
            "SELECT COUNT(id) FROM sentinel WHERE id = %s AND %s = ANY (subscribers);",
            [bannerId, message.author.id],
        )
        if cur.fetchone()[0]:
            cur.execute(
                "SELECT array_length(subscribers, 1), triggercount, name, subscribers FROM sentinel WHERE id = %s;",
                [bannerId],
            )
            bannerInfo = cur.fetchone()
            if bannerInfo[0] >= bannerInfo[1]:  # Triggered banner, can't go back now
                conn.commit()
                return await messagefuncs.sendWrappedMessage(
                    f"Critical mass reached for banner {bannerInfo[2]}! You can't defect anymore.",
                    message.channel,
                )
            else:
                cur.execute(
                    "UPDATE sentinel SET subscribers = array_remove(subscribers, %s), lastmodified = CURRENT_TIMESTAMP WHERE id = %s;",
                    [message.author.id, bannerId],
                )
                conn.commit()
                return await messagefuncs.sendWrappedMessage(
                    "You defected from banner {}. It now needs {} more supporters to reach its goal.".format(
                        bannerInfo[2], str(bannerInfo[1] - bannerInfo[0] + 1)
                    ),
                    message.channel,
                )
        else:
            conn.commit()
            return await messagefuncs.sendWrappedMessage(
                "You have not committed to this banner. `!pledge {}` to pledge support.".format(
                    " ".join(args)
                ),
                message.channel,
            )
    except Exception as e:
        if "cur" in locals() and "conn" in locals():
            conn.rollback()
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error("DBF[{}]: {} {}".format(exc_tb.tb_lineno, type(e).__name__, e))


# Register functions in client
def autoload(ch):
    ch.add_command(
        {
            "trigger": ["!assemble", "!canvas"],
            "function": assemble_function,
            "async": True,
            "args_num": 2,
            "args_name": ["int", "string"],
            "description": "Create a sentinel for assembling groups",
        }
    )
    ch.add_command(
        {
            "trigger": ["!pledge", "!join"],
            "function": pledge_function,
            "async": True,
            "args_num": 1,
            "args_name": ["int"],
            "description": "Salute a sentinel",
        }
    )
    ch.add_command(
        {
            "trigger": ["!defect", "!leave"],
            "function": defect_function,
            "async": True,
            "args_num": 1,
            "args_name": ["int"],
            "description": "Turn away from a sentinel",
        }
    )
    ch.add_command(
        {
            "trigger": ["!banners"],
            "function": listbanners_function,
            "async": False,
            "args_num": 0,
            "args_name": [],
            "description": "List recent sentinels",
        }
    )


async def autounload(ch):
    pass