-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbot.py
More file actions
137 lines (115 loc) · 5.26 KB
/
bot.py
File metadata and controls
137 lines (115 loc) · 5.26 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
137
from __future__ import annotations
import contextlib
from pathlib import Path
from typing import TYPE_CHECKING
import anyio
import discord
from discord.ext import commands
from loguru import logger
from tortoise import Tortoise
from tortoise.exceptions import IntegrityError
from tortoise.expressions import Subquery
from embed_fixer.core.command_tree import CommandTree
from embed_fixer.core.db_config import TORTOISE_ORM
from embed_fixer.utils.misc import get_project_version
from .core.translator import AppCommandTranslator, Translator
from .models import GuildSettings, GuildSettingsOld, GuildSettingsTable
if TYPE_CHECKING:
from aiohttp import ClientSession
__all__ = ("EmbedFixer", "Interaction")
type Interaction = discord.Interaction[EmbedFixer]
intents = discord.Intents(
guilds=True, emojis=True, messages=True, message_content=True, reactions=True
)
allowed_mentions = discord.AllowedMentions(everyone=False)
permissions = discord.Permissions(
manage_webhooks=True,
view_channel=True,
send_messages=True,
send_messages_in_threads=True,
manage_messages=True,
embed_links=True,
add_reactions=True,
)
allowed_installs = discord.app_commands.AppInstallationType(guild=True, user=True)
allowed_contexts = discord.app_commands.AppCommandContext(
guild=True, dm_channel=True, private_channel=True
)
class EmbedFixer(commands.AutoShardedBot):
def __init__(self, *, session: ClientSession, env: str) -> None:
super().__init__(
command_prefix=commands.when_mentioned,
intents=intents,
case_insensitive=True,
allowed_mentions=allowed_mentions,
help_command=None,
chunk_guilds_at_startup=False,
max_messages=None,
member_cache_flags=discord.MemberCacheFlags.none(),
allowed_installs=allowed_installs,
allowed_contexts=allowed_contexts,
activity=discord.CustomActivity(get_project_version()),
tree_cls=CommandTree,
)
self.session = session
self.env = env
self.user: discord.ClientUser
self.translator = Translator()
async def setup_hook(self) -> None:
async for filepath in anyio.Path("embed_fixer/cogs").glob("**/*.py"):
cog_name = Path(filepath).stem
if self.env == "dev" and cog_name == "health":
continue
try:
await self.load_extension(f"embed_fixer.cogs.{cog_name}")
logger.info(f"Loaded cog {cog_name}")
except Exception:
logger.exception(f"Failed to load cog {cog_name}")
await self.load_extension("jishaku")
await self.translator.load()
await self.tree.set_translator(AppCommandTranslator(self.translator))
logger.info(f"Invite: {discord.utils.oauth_url(self.user.id, permissions=permissions)}")
await Tortoise.init(TORTOISE_ORM)
await Tortoise.generate_schemas()
await self._migrate_guild_settings()
async def _migrate_guild_settings(self) -> None:
old_gs = await GuildSettingsOld.exclude(
id__in=Subquery(GuildSettingsTable.all().values("id"))
)
for old in old_gs:
new = GuildSettings(
id=old.id,
disable_webhook_reply=getattr(old, "disable_webhook_reply", False),
disabled_fixes=getattr(old, "disabled_fixes", []),
disabled_domains=getattr(old, "disabled_domains", []),
disable_fix_channels=getattr(old, "disable_fix_channels", []),
enable_fix_channels=getattr(old, "enable_fix_channels", []),
extract_media_channels=getattr(old, "extract_media_channels", []),
disable_image_spoilers=getattr(old, "disable_image_spoilers", []),
show_post_content_channels=getattr(old, "show_post_content_channels", []),
disable_delete_reaction=getattr(old, "disable_delete_reaction", False),
lang=getattr(old, "lang", None),
use_vxreddit=getattr(old, "use_vxreddit", False),
delete_msg_emoji=getattr(old, "delete_msg_emoji", "❌"),
bot_visibility=getattr(old, "bot_visibility", False),
funnel_target_channel=getattr(old, "funnel_target_channel", None),
whitelist_role_ids=getattr(old, "whitelist_role_ids", []),
translate_target_lang=getattr(old, "translate_target_lang", None),
show_original_link_btn=getattr(old, "show_original_link_btn", False),
)
await new.save()
logger.info(f"Migrated guild settings for guild {new.id}")
async def on_guild_join(self, guild: discord.Guild) -> None:
logger.info(f"Joined guild {guild.name} ({guild.id})")
with contextlib.suppress(IntegrityError):
await GuildSettings.create(id=guild.id)
async def on_command_error(
self, context: commands.Context, exception: commands.CommandError
) -> None:
if isinstance(exception, commands.CheckFailure):
return None
return await super().on_command_error(context, exception)
async def close(self) -> None:
logger.info("Bot shutting down...")
await Tortoise.close_connections()
await super().close()