-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathbot.py
More file actions
136 lines (113 loc) · 3.77 KB
/
bot.py
File metadata and controls
136 lines (113 loc) · 3.77 KB
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
# Cat Bot - A Discord bot about catching cats.
# Copyright (C) 2026 Lia Milenakos & Cat Bot Contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import asyncio
import importlib
import logging
import time
import discord
import sentry_sdk
import winuvloop
from discord.ext import commands
import catpg
import config
import database
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
logger.addHandler(handler)
log_level = logging.INFO
try:
# this is a messy closed source script which injects into logging module to do statistics
# inside discord.py, it only intercepts the amount of status codes and ratelimits
# everything else is from main.py logging.debug() statements
import stats # noqa: F401
log_level = logging.DEBUG
except ImportError:
pass
winuvloop.install()
filtered_errors = [
# inactionable/junk discord api errors
"Too Many Requests",
"You are being rate limited",
"Invalid Webhook Token",
"Unknown Interaction",
"Unknown Webhook",
"Failed to convert",
"CommandNotFound",
"CommandAlreadyRegistered",
"Cannot send an empty message",
"Missing Permissions",
# connection errors and warnings (why are there so many)
"ClientConnectorError",
"ClientConnectorDNSError",
"NameResolutionError",
"DiscordServerError",
"WSServerHandshakeError",
"ConnectionClosed",
"ConnectionResetError",
"TimeoutError",
"ServerDisconnectedError",
"ClientOSError",
"TransferEncodingError",
"Request Timeout",
"Session is closed",
"Unclosed connection",
"unable to perform operation on",
"Event loop is closed",
"503 Service Unavailable",
]
def before_send(event, hint):
if "exc_info" not in hint:
return event
for i in filtered_errors:
if i.lower() in str(hint["exc_info"][0]).lower() + str(hint["exc_info"][1]).lower():
return None
return event
if config.SENTRY_DSN:
sentry_sdk.init(dsn=config.SENTRY_DSN, before_send=before_send)
bot = commands.AutoShardedBot(
command_prefix="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
help_command=None,
chunk_guilds_at_startup=False,
allowed_contexts=discord.app_commands.AppCommandContext(guild=True, dm_channel=False, private_channel=False),
intents=discord.Intents(message_content=True, messages=True, guilds=True),
member_cache_flags=discord.MemberCacheFlags.none(),
allowed_mentions=discord.AllowedMentions.none(),
)
@bot.event
async def setup_hook():
await database.connect()
await bot.load_extension("main")
async def reload(reload_db):
try:
await bot.unload_extension("main")
except commands.ExtensionNotLoaded:
pass
if reload_db:
await database.close()
importlib.reload(database)
importlib.reload(catpg)
await database.connect()
await bot.load_extension("main")
config.cat_cought_rain = {}
config.rain_starter = {}
bot.cat_bot_reload_hook = reload # pyright: ignore
try:
config.HARD_RESTART_TIME = time.time()
bot.run(config.TOKEN, log_handler=handler, log_level=log_level)
finally:
asyncio.run(database.close())