~nova/fletcher

fletcher/main.py -rw-r--r-- 30.6 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# bot.py
from datetime import datetime
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from sys import exc_info
from systemd import journal
import asyncio
import cProfile
import discord
import importlib
import io
import logging
import math
import os
import psycopg2
import re
from asyncache import cached
import sentry_sdk
from sentry_sdk.integrations.aiohttp import AioHttpIntegration

sentry_sdk.init(
    dsn="https://7654ff657b6447d78c3eee40151c9414@sentry.io/1842241",
    integrations=[AioHttpIntegration()],
    before_send=lambda event, hint: None
    if type(event) in [discord.ConnectionClosed]
    else event,
)
import signal
import traceback

"""
fletcher=# \d attributions
                              Table "public.attributions"
    Column    |            Type             | Collation | Nullable |      Default
--------------+-----------------------------+-----------+----------+-------------------
 added        | timestamp without time zone |           |          | CURRENT_TIMESTAMP
 author_id    | bigint                      |           |          |
 from_message | bigint                      |           |          |
 from_channel | bigint                      |           |          |
 from_guild   | bigint                      |           |          |
 message      | bigint                      |           |          |
 channel      | bigint                      |           |          |
 guild        | bigint                      |           |          |

fletcher=# \d parlay
                                      Table "public.parlay"
    Column    |            Type             | Collation | Nullable |           Default            
--------------+-----------------------------+-----------+----------+------------------------------
 id           | integer                     |           | not null | generated always as identity
 name         | character varying(255)      |           |          | 
 description  | text                        |           |          | 
 lastmodified | timestamp without time zone |           |          | 
 members      | bigint[]                    |           |          | 
 channel      | bigint                      |           |          | 
 guild        | bigint                      |           |          | 
 created      | timestamp without time zone |           |          | CURRENT_TIMESTAMP
 ttl          | interval                    |           |          | 
Indexes:
    "parlay_name_must_be_guild_unique" UNIQUE CONSTRAINT, btree (name, guild)

fletcher=# \d sentinel
                                     Table "public.sentinel"
    Column    |            Type             | Collation | Nullable |           Default            
--------------+-----------------------------+-----------+----------+------------------------------
 id           | integer                     |           | not null | generated always as identity
 name         | character varying(255)      |           |          | 
 description  | text                        |           |          | 
 lastmodified | timestamp without time zone |           |          | 
 subscribers  | bigint[]                    |           |          | 
 triggercount | integer                     |           |          | 
 created      | timestamp without time zone |           |          | CURRENT_TIMESTAMP
 triggered    | timestamp without time zone |           |          | 
Indexes:
    "sentinel_name_must_be_globally_unique" UNIQUE CONSTRAINT, btree (name)

fletcher=# \d messagemap
               Table "public.messagemap"
   Column    |   Type   | Collation | Nullable | Default 
-------------+----------+-----------+----------+---------
 fromguild   | bigint   |           |          | 
 toguild     | bigint   |           |          | 
 fromchannel | bigint   |           |          | 
 tochannel   | bigint   |           |          | 
 frommessage | bigint   |           |          | 
 tomessage   | bigint   |           |          | 
 reactions   | bigint[] |           |          | 

Indexes:
    "messagemap_idx" btree (fromguild, fromchannel, frommessage)

fletcher=# \d permaRoles
                            Table "public.permaroles"
 Column  |            Type             | Collation | Nullable |      Default
---------+-----------------------------+-----------+----------+-------------------
 userid  | bigint                      |           | not null |
 guild   | bigint                      |           | not null |
 roles   | bigint[]                    |           |          |
 updated | timestamp without time zone |           |          | CURRENT_TIMESTAMP
Indexes:
    "permaroles_idx" btree (userid, guild)

fletcher=# \d reminders;
                                   Table "public.reminders"
  Column      |            Type             | Collation | Nullable |           Default           
--------------+-----------------------------+-----------+----------+-----------------------------
 userid       | bigint                      |           | not null | 
 guild        | bigint                      |           | not null | 
 channel      | bigint                      |           | not null | 
 message      | bigint                      |           | not null | 
 content      | text                        |           |          | 
 created      | timestamp without time zone |           |          | CURRENT_TIMESTAMP
 scheduled    | timestamp without time zone |           |          | (now() + '1 day'::interval)
 trigger_type | text                        |           |          | 'table'::text

fletcher=# \d qdb
                                Table "public.qdb"
  Column  |  Type   | Collation | Nullable |                Default                
----------+---------+-----------+----------+---------------------------------------
 user_id  | bigint  |           | not null | 
 guild_id | bigint  |           | not null | 
 quote_id | integer |           | not null | nextval('qdb_quote_id_seq'::regclass)
 key      | text    |           |          | 
 value    | text    |           | not null | 

"""

logger = logging.getLogger("fletcher")

import load_config

config = load_config.FletcherConfig()

# Enable logging to SystemD
logger.addHandler(
    journal.JournalHandler(
        SYSLOG_IDENTIFIER=config.get(section="discord", key="botLogName")
    )
)
logger.setLevel(logging.DEBUG)

client = discord.Client(intents=discord.Intents.all())

# token from https://discordapp.com/developers
token = config.get(section="discord", key="botToken")

# globals for database handle and CommandHandler
conn = None
ch = None
pr = None

# Submodules, loaded in reload_function so no initialization is done here
import commandhandler
import versionutils
import greeting
import sentinel
import janissary
import mathemagical
import messagefuncs
import text_manipulators
import schedule
import swag
import googlephotos
import pinterest
import danbooru
import github
import chronos

versioninfo = versionutils.VersionInfo()
sid = SentimentIntensityAnalyzer()

webhook_sync_registry = {
    "FromGuild:FromChannelName": {
        "fromChannelObject": None,
        "fromWebhook": None,
        "toChannelObject": None,
        "toWebhook": None,
    }
}


async def load_webhooks(ch=None):
    global config
    webhook_sync_registry = {}
    for guild in client.guilds:
        try:
            if config.get(guild=guild, key="synchronize"):
                logger.debug(f"LWH: Querying {guild.name}")
                for webhook in await guild.webhooks():
                    if webhook.name.startswith(
                        config.get(section="discord", key="botNavel") + " ("
                    ):
                        logger.debug(f"LWH: * {webhook.name}")
                        toChannelName = (
                            f"{guild.name}:{guild.get_channel(webhook.channel_id).name}"
                        )
                        fromTuple = webhook.name.split("(")[1].split(")")[0].split(":")
                        fromTuple[0] = messagefuncs.expand_guild_name(
                            fromTuple[0]
                        ).replace(":", "")
                        fromGuild = discord.utils.get(
                            client.guilds, name=fromTuple[0].replace("_", " ")
                        )
                        fromChannelName = (
                            fromTuple[0].replace("_", " ") + ":" + fromTuple[1]
                        )
                        try:
                            webhook_sync_registry[
                                f"{fromGuild.id}:{webhook.id}"
                            ] = fromChannelName
                        except AttributeError:
                            logger.debug(f"LWH: fromGuild.id not defined")
                            continue
                        webhook_sync_registry[fromChannelName] = {
                            "toChannelObject": guild.get_channel(webhook.channel_id),
                            "toWebhook": webhook,
                            "toChannelName": toChannelName,
                            "fromChannelObject": None,
                            "fromWebhook": None,
                        }
                        webhook_sync_registry[fromChannelName][
                            "fromChannelObject"
                        ] = discord.utils.get(
                            fromGuild.text_channels, name=fromTuple[1]
                        )
                        try:
                            # webhook_sync_registry[fromChannelName]['fromWebhook'] = discord.utils.get(await fromGuild.webhooks(), channel__name=fromTuple[1])
                            if webhook_sync_registry[fromChannelName][
                                "fromChannelObject"
                            ]:
                                webhook_sync_registry[fromChannelName][
                                    "fromWebhook"
                                ] = await webhook_sync_registry[fromChannelName][
                                    "fromChannelObject"
                                ].webhooks()
                                webhook_sync_registry[fromChannelName][
                                    "fromWebhook"
                                ] = (
                                    webhook_sync_registry[fromChannelName][
                                        "fromWebhook"
                                    ][0]
                                    if len(
                                        webhook_sync_registry[fromChannelName][
                                            "fromWebhook"
                                        ]
                                    )
                                    else None
                                )
                            else:
                                logger.warning(
                                    f"LWH: Could not find fromChannel {fromTuple[1]} in {fromGuild}"
                                )
                        except discord.Forbidden as e:
                            logger.warning(
                                f'LWH: Error getting fromWebhook for {webhook_sync_registry[fromChannelName]["fromChannelObject"]}'
                            )
                            pass
            elif "Guild " + str(guild.id) not in config:
                logger.warning(
                    f"LWH: Failed to find config for {guild.name} ({guild.id})"
                )
        except discord.Forbidden as e:
            exc_type, exc_obj, exc_tb = exc_info()
            logger.debug(f"LWH[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")
            logger.debug(traceback.format_exc())
            logger.warning(
                f"Couldn't load webhooks for {guild.name} ({guild.id}), ask an admin to grant additional permissions (https://novalinium.com/go/4/fletcher)"
            )
            pass
        except AttributeError as e:
            exc_type, exc_obj, exc_tb = exc_info()
            logger.debug(f"LWH[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")
            pass
    globals()["webhook_sync_registry"] = webhook_sync_registry
    if ch:
        ch.webhook_sync_registry = webhook_sync_registry
    logger.debug("Webhooks loaded:")
    logger.debug(
        "\n".join(
            [
                f'{key} to {webhook_sync_registry[key]["toChannelName"]} (Guild {webhook_sync_registry[key]["toChannelObject"].guild.id})'
                for key in list(webhook_sync_registry)
                if type(webhook_sync_registry[key]) is not str
            ]
        )
    )


canticum_message = None
doissetep_omega = None


async def autoload(module, choverride, config=None):
    if choverride:
        ch = choverride
    else:
        ch = globals()["ch"]
    global client
    global conn
    global sid
    global versioninfo
    try:
        await module.autounload(ch)
    except AttributeError as e:
        # ignore missing autounload
        logger.info(f"{module.__name__} missing autounload(ch), continuing.")
        pass
    importlib.reload(module)
    module.ch = ch
    module.client = client
    if config is None:
        module.config = globals()["config"]
    else:
        module.config = config
    module.conn = conn
    module.sid = sid
    module.versioninfo = versioninfo
    try:
        module.autoload(ch)
    except AttributeError as e:
        # ignore missing autoload
        logger.info(f"{module.__name__} missing autoload(ch), continuing.")
        exc_type, exc_obj, exc_tb = exc_info()
        logger.debug(f"al[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")
        logger.debug(traceback.format_exc())
        pass


async def animate_startup(emote, message=None):
    if message:
        await message.add_reaction(emote)
    logger.info(emote)


async def reload_function(message=None, client=client, args=[]):
    global config
    global conn
    global sid
    global versioninfo
    global doissetep_omega
    now = datetime.utcnow()
    try:
        await client.change_presence(activity=discord.Game(name="Reloading: The Game"))
        if config["discord"].get("profile"):
            global pr
            if pr:
                pr.disable()
                pr.print_stats()
        importlib.reload(load_config)
        config = load_config.FletcherConfig()
        load_config.client = client
        config.client = client
        await animate_startup("📝", message)
        conn = psycopg2.connect(
            host=config["database"]["host"],
            database=config["database"]["tablespace"],
            user=config["database"]["user"],
            password=config["database"]["password"],
        )
        await animate_startup("💾", message)
        # Command Handler (loaded twice to bootstrap)
        await autoload(commandhandler, None, config)
        await animate_startup("⌨", message)
        ch = commandhandler.CommandHandler(client)
        commandhandler.ch = ch
        ch.config = config
        await autoload(versionutils, ch)
        versioninfo = versionutils.VersionInfo()
        ch.add_command(
            {
                "trigger": ["!reload_f"],
                "function": reload_function,
                "async": True,
                "admin": "global",
                "args_num": 0,
                "args_name": [],
                "description": "Reload config (admin only)",
            }
        )
        ch.webhook_sync_registry = webhook_sync_registry
        # Utility text manipulators Module
        await autoload(text_manipulators, ch)
        await animate_startup("🔧", message)
        # Schedule Module
        await autoload(schedule, ch)
        await animate_startup("📅", message)
        # Greeting module
        await autoload(greeting, ch)
        await animate_startup("👋", message)
        # Sentinel Module
        await autoload(sentinel, ch)
        await animate_startup("🎏", message)
        # Messages Module
        await autoload(messagefuncs, ch)
        await animate_startup("🔭", message)
        # Math Module
        await autoload(mathemagical, ch)
        await animate_startup("➕", message)
        await autoload(janissary, ch)
        # Super Waifu Animated Girlfriend (SWAG)
        await autoload(swag, ch)
        await animate_startup("🙉", message)
        # Photos Connectors (for !twilestia et al)
        await autoload(pinterest, ch)
        await autoload(googlephotos, ch)
        await autoload(danbooru, ch)
        await animate_startup("📷", message)
        # GitHub Connector
        await autoload(github, ch)
        await animate_startup("🐙", message)
        # Time Module
        await autoload(chronos, ch)
        await animate_startup("🕰️", message)
        # Play it again, Sam
        asyncio.create_task(doissetep_omega_autoconnect())
        # Trigger reload handlers
        await ch.reload_handler()
        # FIXME there should be some way to defer this, or maybe autoload another time
        await autoload(commandhandler, ch)
        await animate_startup("🔁", message)
        globals()["ch"] = ch
        await load_webhooks(ch)
        if message:
            await message.add_reaction("↔")
        await animate_startup("✅", message)
        await client.change_presence(
            activity=discord.Game(name="fletcher.fun | !help", start=now)
        )
    except Exception as e:
        exc_type, exc_obj, exc_tb = exc_info()
        logger.critical(f"RM[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")
        logger.debug(traceback.format_exc())
        await animate_startup("🚫", message)
        await client.change_presence(
            activity=discord.Game(
                name=f"Error Reloading: RM[{exc_tb.tb_lineno}]: {e}", start=now
            )
        )


# bot is ready
@client.event
async def on_ready():
    try:
        global doissetep_omega
        global client
        global ch
        # print bot information
        await client.change_presence(activity=discord.Game(name="Reloading: The Game"))
        logger.info(
            f"Discord.py Version {discord.__version__}, connected as {client.user.name} ({client.user.id})"
        )
        loop = asyncio.get_running_loop()
        loop.remove_signal_handler(signal.SIGHUP)
        await reload_function()
        loop.add_signal_handler(
            signal.SIGHUP, lambda: asyncio.ensure_future(reload_function())
        )
        loop.add_signal_handler(
            signal.SIGINT, lambda: asyncio.ensure_future(shutdown_function())
        )
    except Exception as e:
        logger.exception(e)


async def shutdown_function():
    global client
    await client.change_presence(
        activity=discord.Game(name="Shutting Down", start=now),
        status=discord.Status.do_not_disturb,
    )
    sys.exit(0)


# on new message
@client.event
async def on_message(message):
    global config
    with sentry_sdk.configure_scope() as scope:
        user = message.author
        scope.user = {"id": user.id, "username": str(user)}
        scope.set_tag("message", str(message.id))
        try:
            # try to evaluate with the command handler
            while ch is None:
                await asyncio.sleep(1)
            while 1:
                try:
                    ch.config
                except AttributeError:
                    await asyncio.sleep(1)
                break
            await ch.command_handler(message)

        except Exception as e:
            exc_type, exc_obj, exc_tb = exc_info()
            logger.debug(traceback.format_exc())
            logger.error(
                f"OM[{exc_tb.tb_lineno}]: {type(e).__name__} {e}",
                extra={"MESSAGE_ID": str(message.id)},
            )


# on message update (for webhooks only for now)
@client.event
async def on_raw_message_edit(payload):
    global webhook_sync_registry
    try:
        # This is tricky with self-modifying bot message synchronization, TODO
        message_id = payload.message_id
        message = payload.data
        if "guild_id" in message:
            fromGuild = client.get_guild(int(message["guild_id"]))
            fromChannel = fromGuild.get_channel(int(message["channel_id"]))
        else:
            fromChannel = client.get_channel(int(message["channel_id"]))
        try:
            fromMessage = await fromChannel.fetch_message(message_id)
        except discord.NotFound as e:
            exc_type, exc_obj, exc_tb = exc_info()
            extra = {"MESSAGE_ID": str(message_id), "payload": str(payload)}
            if type(fromChannel) is discord.DMChannel:
                extra["CHANNEL_IDENTIFIER"] = fromChannel.recipient
            else:
                extra["CHANNEL_IDENTIFIER"] = fromChannel.name
            if fromGuild:
                extra["GUILD_IDENTIFIER"] = fromGuild.name
            logger.debug(traceback.format_exc())
            logger.warning(
                f"ORMU[{exc_tb.tb_lineno}]: {type(e).__name__} {e}", extra=extra
            )
            return
        while ch is None:
            await asyncio.sleep(1)
        while 1:
            try:
                ch.config
            except AttributeError:
                await asyncio.sleep(1)
            break
        with sentry_sdk.configure_scope() as scope:
            user = fromMessage.author
            scope.user = {"id": user.id, "username": str(user)}
            await ch.edit_handler(fromMessage)

    except discord.Forbidden as e:
        logger.error(
            "Forbidden to edit synced message from "
            + str(fromGuild.name)
            + ":"
            + str(fromChannel.name)
        )
    # except KeyError as e:
    #     # Eat keyerrors from non-synced channels
    #     pass
    # except AttributeError as e:
    #     # Eat from PMs
    #     pass
    # generic python error
    except Exception as e:
        if "cur" in locals() and "conn" in locals():
            conn.rollback()
        exc_type, exc_obj, exc_tb = exc_info()
        logger.debug(traceback.format_exc())
        logger.error(
            f"ORMU[{exc_tb.tb_lineno}]: {type(e).__name__} {e}",
            extra={"MESSAGE_ID": str(message_id)},
        )


# on message deletion (for webhooks only for now)
@client.event
async def on_raw_message_delete(message):
    global webhook_sync_registry
    try:
        while ch is None:
            await asyncio.sleep(1)
        while 1:
            try:
                ch.config
            except AttributeError:
                await asyncio.sleep(1)
            break
        fromGuild = client.get_guild(message.guild_id)
        fromChannel = fromGuild.get_channel(message.channel_id)
        if type(fromChannel) is discord.TextChannel:
            logger.info(
                str(message.message_id)
                + " #"
                + fromGuild.name
                + ":"
                + fromChannel.name
                + " [Deleted]",
                extra={
                    "GUILD_IDENTIFIER": fromGuild.name,
                    "CHANNEL_IDENTIFIER": fromChannel.name,
                    "MESSAGE_ID": str(message.message_id),
                },
            )
        elif type(fromChannel) is discord.DMChannel:
            logger.info(
                str(message.message_id)
                + " @"
                + fromChannel.recipient.name
                + " [Deleted]",
                extra={
                    "GUILD_IDENTIFIER": "@",
                    "CHANNEL_IDENTIFIER": fromChannel.name,
                    "MESSAGE_ID": str(message.message_id),
                },
            )
        else:
            # Group Channels don't support bots so neither will we
            pass
        guild_config = ch.scope_config(guild=message.guild)
        if type(fromChannel) is discord.TextChannel and (
            fromGuild.name + ":" + fromChannel.name in webhook_sync_registry.keys()
        ):
            # Give messages time to be added to the database
            await asyncio.sleep(1)
            cur = conn.cursor()
            cur.execute(
                "SELECT toguild, tochannel, tomessage FROM messagemap WHERE fromguild = %s AND fromchannel = %s AND frommessage = %s LIMIT 1;",
                [message.guild_id, message.channel_id, message.message_id],
            )
            metuple = cur.fetchone()
            if metuple is not None:
                cur.execute(
                    "DELETE FROM messageMap WHERE fromGuild = %s AND fromChannel = %s AND fromMessage = %s",
                    [fromGuild.id, fromChannel.id, message.message_id],
                )
            conn.commit()
            if metuple is not None:
                toGuild = client.get_guild(metuple[0])
                toChannel = toGuild.get_channel(metuple[1])
                if not ch.config.get(
                    key="sync-deletions", guild=toGuild, channel=toChannel
                ):
                    logger.debug(
                        f"ORMD: Demurring to delete edited message at client guild request"
                    )
                    return
                toMessage = None
                while not toMessage:
                    try:
                        toMessage = await toChannel.fetch_message(metuple[2])
                    except discord.NotFound as e:
                        exc_type, exc_obj, exc_tb = exc_info()
                        logger.error(
                            f"ORMD[{exc_tb.tb_lineno}]: {type(e).__name__} {e}"
                        )
                        logger.error(
                            f"ORMD[{exc_tb.tb_lineno}]: {metuple[0]}:{metuple[1]}:{metuple[2]}"
                        )
                        toMessage = None
                        await asyncio.sleep(1)
                        pass
                logger.debug(
                    f"ORMD: Deleting synced message {metuple[0]}:{metuple[1]}:{metuple[2]}"
                )
                await toMessage.delete()
    except discord.Forbidden as e:
        logger.error(
            f"Forbidden to delete synced message from {fromGuild.name}:{fromChannel.name}"
        )
    except KeyError as e:
        # Eat keyerrors from non-synced channels
        pass
    except AttributeError as e:
        # Eat from PMs
        pass
    # generic python error
    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"ORMD[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


# on new rxn
@client.event
async def on_raw_reaction_add(reaction):
    # if the reaction is from the bot itself ignore it
    if reaction.user_id == client.user.id:
        return
    # try to evaluate with the command handler
    try:
        while ch is None:
            await asyncio.sleep(1)
        while 1:
            try:
                ch.config
            except AttributeError:
                await asyncio.sleep(1)
            break
        await ch.reaction_handler(reaction)

    # generic python error
    except Exception as e:
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error(f"ORRA[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


# on new rxn
@client.event
async def on_raw_reaction_remove(reaction):
    # if the reaction is from the bot itself ignore it
    if reaction.user_id == client.user.id:
        return
    # try to evaluate with the command handler
    try:
        while ch is None:
            await asyncio.sleep(1)
        while 1:
            try:
                ch.config
            except AttributeError:
                await asyncio.sleep(1)
            break
        await ch.reaction_remove_handler(reaction)

    # generic python error
    except Exception as e:
        exc_type, exc_obj, exc_tb = exc_info()
        logger.error(f"ORRR[{exc_tb.tb_lineno}]: {type(e).__name__} {e}")


# on vox change
@client.event
async def on_voice_state_update(member, before, after):
    global canticum_message
    # TODO refactor to CommandHandler
    logger.debug(
        f"Vox update in {member.guild}, {member} {before.channel} -> {after.channel}"
    )
    # Notify only if:
    # Doissetep
    # New joins only, no transfers
    # Not me
    if (
        member.guild.id == int(config["audio"]["guild"])
        and after.channel is not None
        and before.channel is None
        and member.id != client.user.id
        and str(after.channel.id) not in config["audio"]["channel-ban"].split(",")
    ):
        canticum = client.get_channel(int(config["audio"]["notificationchannel"]))
        if canticum_message is not None:
            await canticum_message.delete()
        canticum_message = await canticum.send(
            "<@&"
            + str(config["audio"]["notificationrole"])
            + ">: "
            + str(member.name)
            + " is in voice ("
            + str(after.channel.name)
            + ") in "
            + str(member.guild.name)
        )


# on new member
@client.event
async def on_member_join(member):
    while ch is None:
        await asyncio.sleep(1)
    while 1:
        try:
            ch.config
        except AttributeError:
            await asyncio.sleep(1)
        break
    await ch.join_handler(member)


# on departing member
@client.event
async def on_member_remove(member):
    while ch is None:
        await asyncio.sleep(1)
    while 1:
        try:
            ch.config
        except AttributeError:
            await asyncio.sleep(1)
        break
    await ch.remove_handler(member)


# on channel change
@client.event
async def on_guild_channel_update(before, after):
    while ch is None:
        await asyncio.sleep(1)
    while 1:
        try:
            ch.config
        except AttributeError:
            await asyncio.sleep(1)
        break
    await ch.channel_update_handler(before, after)


async def doissetep_omega_autoconnect():
    global doissetep_omega
    global config
    if doissetep_omega and doissetep_omega.is_connected():
        if not doissetep_omega.is_playing():
            doissetep_omega.play(discord.FFmpegPCMAudio(config["audio"]["instreamurl"]))
        # Reset canticum_message when reloaded [workaround for https://todo.sr.ht/~nova/fletcher/6]
        canticum_message = None
        return doissetep_omega
    else:
        try:
            doissetep_omega = (
                await client.get_guild(int(config["audio"]["guild"]))
                .get_channel(int(config["audio"]["channel"]))
                .connect()
            )
            if not doissetep_omega.is_playing():
                doissetep_omega.play(
                    discord.FFmpegPCMAudio(config["audio"]["instreamurl"])
                )
            # Reset canticum_message when reloaded [workaround for https://todo.sr.ht/~nova/fletcher/6]
            canticum_message = None
            return doissetep_omega
        except discord.ClientException as e:
            logger.debug("Omega connected already")
            pass
        except asyncio.exceptions.TimeoutError as e:
            logger.debug("Omega timeout")
            pass
        except AttributeError as e:
            logger.exception(e)


loop = asyncio.get_event_loop()

# start bot
loop.run_until_complete(client.start(token))