~nova/fletcher

fletcher/greeting.py -rw-r--r-- 15.8 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import asyncio
import discord
import io
import logging
import messagefuncs
import random
import re
from sys import exc_info
from datetime import datetime, timedelta

# global conn set by reload_function

logger = logging.getLogger("fletcher")


async def restorerole_function(member, client, config):
    try:
        global conn
        cur = conn.cursor()
        cur.execute(
            "SELECT nickname, roles FROM permaRoles WHERE userid = %s AND guild = %s;",
            [member.id, member.guild.id],
        )
        roles = cur.fetchone()
        if roles is not None:
            name = roles[0]
            roles = roles[1]
            cur.execute(
                "DELETE FROM permaRoles WHERE userid = %s AND guild = %s;",
                [member.id, member.guild.id],
            )
        conn.commit()
        if roles is None and config.get("default_join_role"):
            name = None
            roles = config.get("default_join_role")
        elif roles is None:
            return
        # Silently drop deleted roles
        roles = list(filter(None, [member.guild.get_role(role) for role in roles]))
        logger.info(
            f'RPR: Restoring roles {",".join([str(role) for role in roles])} for {member.id} in {member.guild.id}'
        )
        await member.edit(nick=name, roles=roles, reason="Restoring Previous Roles")
    except discord.Forbidden as e:
        await messagefuncs.sendWrappedMessage(
            f'Error Restoring roles {",".join([str(role) for role in roles])} and nick {name} for {member.name} ({member.id}): {e}',
            member.guild.owner,
        )
    except Exception as e:
        if "cur" in locals() and "conn" in locals():
            conn.rollback()
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error(f"RPR[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


async def saverole_function(member, client, config):
    try:
        global conn
        if len(member.roles):
            roles = [role.id for role in member.roles]
            logger.info(
                f'SRF: Storing roles {",".join([str(role) for role in roles])} for {member.id} in {member.guild.id}'
            )
            cur = conn.cursor()
            cur.execute(
                "INSERT INTO permaRoles (userid, guild, roles, updated, nickname) VALUES (%s, %s, %s, %s, %s);",
                [
                    member.id,
                    member.guild.id,
                    [role.id for role in member.roles],
                    datetime.now(),
                    member.display_name,
                ],
            )
            conn.commit()
    except Exception as e:
        if "cur" in locals() and "conn" in locals():
            conn.rollback()
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error(f"SRF[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


async def lockout_function(member, client, config):
    try:
        if type(member) is discord.Member:
            for category, channels in member.guild.by_category():
                if category is not None:
                    logger.debug(
                        "LOF: "
                        + str(member)
                        + " from category "
                        + str(category)
                        + " in "
                        + str(member.guild)
                    )
                    await category.set_permissions(
                        member,
                        read_messages=False,
                        read_message_history=False,
                        send_messages=False,
                        reason="Lockout policy for new members",
                    )
                else:
                    for channel in channels:
                        logger.debug(
                            "LOF: "
                            + str(member)
                            + " from non-category channel "
                            + str(channel)
                            + " in "
                            + str(member.guild)
                        )
                        await channel.set_permissions(
                            member,
                            read_messages=False,
                            read_message_history=False,
                            send_messages=False,
                            reason="Lockout policy for new members",
                        )
            await messagefuncs.sendWrappedMessage(config["lockout_message"], member)
            await messagefuncs.sendWrappedMessage(
                f"If you would like access to this guild, reply to this message with `I agree` to indicate that you have read the rules and conditions. If not, do nothing and you will be automatically removed in {config['lockout_timeout']} seconds.",
                member,
            )

            def check(m):
                return (
                    m.content == "I agree"
                    and type(m.channel) is discord.DMChannel
                    and m.channel.recipient == member
                )

            try:
                msg = await client.wait_for(
                    "message", timeout=float(config["lockout_timeout"]), check=check
                )
            except asyncio.TimeoutError:
                await messagefuncs.sendWrappedMessage(
                    "Timed out waiting for agreement to rules. You have been automatically kicked from the server.",
                    member,
                )
                await member.kick(reason="Failed to agree to rules in timely manner.")
            else:
                await memssagefuncs.sendWrappedMessage(
                    "Thank you for your cooperation! Granting you member permissions. Please note that this server may have additional roles that restrict channels.",
                    member,
                )
                for category, channels in member.guild.by_category():
                    if category is not None:
                        logger.debug(
                            f"LOF: {member} from category {category} in {member.guild}"
                        )
                        await category.set_permissions(
                            member,
                            None,
                            reason="Lockout policy for new members (agreed to rules)",
                        )
                    else:
                        for channel in channels:
                            logger.debug(
                                f"LOF: {member} from non-category channel {channel} in {member.guild}"
                            )
                            await channel.set_permissions(
                                member,
                                None,
                                reason="Lockout policy for new members (agreed to rules)",
                            )
    except Exception as e:
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error(f"LOF[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


async def randomize_role_function(member, client, config):
    try:
        if type(member) is discord.Member:
            role = member.guild.get_role(
                int(random.choice(config["randomize_role_list"]))
            )
            logger.info("RRF: adding role " + str(role) + " to " + str(member))
            await member.add_roles(
                role,
                reason="Per Randomize Role Guild setting (see randomize_role_list)",
                atomic=False,
            )
    except Exception as e:
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error(f"RRF[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


async def printhello_reload_function(guild, client, config):
    logger.debug(
        "PHRF: Hello to guild " + guild.name + " at " + str(datetime.utcnow()) + "!"
    )


async def chanban_join_function(member, client, config):
    logger.info(
        "CBJF: "
        + str(member)
        + " "
        + str(config["chanban_younger_than"])
        + " "
        + str(member.guild.get_channel(int(config["chanban_channel"])).name)
    )
    await member.guild.get_channel(int(config["chanban_channel"])).set_permissions(
        member, read_messages=False, send_messages=False, embed_links=False
    )


async def chanban_reload_function(guild, client, config):
    try:
        channel = guild.get_channel(int(config["chanban_channel"]))
        logger.debug(
            "CBRF: " + str(config["chanban_younger_than"]) + " " + channel.name
        )
        now = datetime.utcnow()
        age_of_consent = timedelta(seconds=int(config["chanban_younger_than"]))
        age_of_consent_expiry = timedelta(
            seconds=int(config["chanban_younger_than"]), hours=2
        )
        younglings = [
            member
            for member in guild.members
            if now - member.joined_at < age_of_consent
        ]
        member_overrides = [
            member
            for member, permissions in channel.overwrites.items()
            if isinstance(member, discord.Member)
        ]
        for member in younglings:
            if member not in member_overrides:
                logger.info("CBRF: Banning " + str(member))
                await channel.set_permissions(
                    member, read_messages=False, send_messages=False, embed_links=False
                )
        for member in member_overrides:
            permissions = channel.overwrites_for(member)
            if (
                now - member.joined_at > age_of_consent
                and now - member.joined_at < age_of_consent_expiry
                and permissions.read_messages == False
                and permissions.send_messages == False
                and permissions.embed_links == False
            ):
                logger.info("CBRF: Unbanning " + str(member))
                await channel.set_permissions(member, overwrite=None)
    except Exception as e:
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error(f"CBRF[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


async def regex_filter(message, client, config):
    try:
        if config.get("regex-listmode") == "whitelist":
            whitelist_mode = True
        else:
            whitelist_mode = False
        if config.get("regex-target") == "author":
            subject = str(message.author)
        else:
            subject = str(message.content)
        re_flags = 0
        if config.get("regex-ignorecase"):
            re_flags = re_flags | re.IGNORECASE
        matching = re.search(config["regex-pattern"], subject, re_flags)
        if matching and whitelist_mode:
            allowed = True
        elif not matching and whitelist_mode:
            allowed = False
        elif matching and not whitelist_mode:
            allowed = False
        elif not matching and not whitelist_mode:
            allowed = True
        if not allowed:
            if "regex-warn-reaction" in config:
                if len(config["regex-warn-reaction"]) > 1:
                    target_emoji = discord.utils.get(
                        guild.emojis, name=config["regex-warn-reaction"]
                    )
                    if target_emoji is None:
                        target_emoji = discord.utils.get(
                            client.get_all_emojis(), name=config["regex-warn-reaction"]
                        )
                else:
                    target_emoji = config["regex-warn-reaction"]
                await message.add_reaction(target_emoji)
            if "regex-warn" in config:
                if config.get("regex-warn-target") == "author":
                    target = message.author
                else:
                    target = message.channel
                if "regex-warn-timeout" in config:
                    if config["regex-warn-timeout"].isdigit():
                        timeout = int(config["regex-warn-timeout"])
                    else:
                        timeout = None
                else:
                    timeout = 60
                await messagefuncs.sendWrappedMessage(
                    config["regex-warn"].replace("\\n", "\n").format(**vars()),
                    target,
                    delete_after=timeout,
                )

            if config.get("regex-kill"):
                if message.channel.permissions_for(message.author).manage_messages:
                    emoji = "\U0001F5D9"
                    await message.add_reaction(emoji)
                    await asyncio.sleep(10)
                    await message.remove_reaction(emoji, client.user)
                try:
                    await message.delete()
                except discord.Forbidden as e:
                    logger.warning(
                        "MRF: Forbidden to delete message in " + str(message.channel)
                    )
                    pass
        return allowed
    except Exception as e:
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error(f"MRF[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


async def alphabetize_channels(guild, client, config):
    # In categories, don't order categories themselves
    try:
        runagain = True
        while runagain:
            position = 0
            runagain = False
            for category_tuple in guild.by_category():
                channels = (
                    category_tuple[1]
                    if not category_tuple[0]
                    else category_tuple[0].channels
                )
                if category_tuple[0] and category_tuple[0].name in config.get(
                    "azsort-exclude", ""
                ).split(","):
                    position += len(channels)
                    continue
                channels = list(
                    filter(
                        lambda channel: type(channel) == discord.TextChannel, channels
                    )
                )
                az_channels = sorted(channels, key=lambda channel: channel.name)
                logger.debug(
                    f'Alphabetizing {category_tuple[0].name if category_tuple[0] and category_tuple[0].name else "Unnamed Category"}',
                    extra={"FLETCHER_MODULE": "alphabetize_channels"},
                )
                moves = 0
                for channel in az_channels:
                    logger.debug(
                        f"#{channel.name} {channel.position} -> {position}",
                        extra={"FLETCHER_MODULE": "alphabetize_channels"},
                    )
                    if channel.position != position:
                        moves += 1
                        if not moves % 10:
                            await asyncio.sleep(60)
                        runagain = True
                        logger.info(
                            f"Moving {channel} to {position} from {channel.position}",
                            extra={"FLETCHER_MODULE": "alphabetize_channels"},
                        )
                        if channel.position != 0:
                            try:
                                await channel.edit(
                                    position=position, reason="Alphabetizing"
                                )
                            except discord.InvalidArgument as e:
                                # Ignore issues with position being too high for voice channels
                                pass
                    position += 1
    except Exception as e:
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error(f"ACF[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


# Register functions in client
def autoload(ch):
    ch.add_remove_handler("save_roles", saverole_function)
    ch.add_join_handler("restore_roles", restorerole_function)
    ch.add_join_handler("lockout", lockout_function)
    ch.add_join_handler("randomize_role", randomize_role_function)
    ch.add_reload_handler("printhello", printhello_reload_function)
    ch.add_join_handler("chanban", chanban_join_function)
    ch.add_reload_handler("chanban", chanban_reload_function)
    ch.add_reload_handler("azsort", alphabetize_channels)


async def autounload(ch):
    pass