-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
126 lines (96 loc) · 3.29 KB
/
bot.py
File metadata and controls
126 lines (96 loc) · 3.29 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
import subprocess
import asyncio
import discord
from decouple import config
from discord.ext import commands
from utils.embed_handler import simple_embed
from utils.manager import RuntimeManager, Database
from constants import system_log_channel_id, discord_invite_link, tortoise_guild_id, bot_repo_link
TOKEN = config("DISCORD_BOT_TOKEN")
DB_URL = config("DATABASE_URL")
class MyBot(commands.Bot):
def __init__(self):
intents = discord.Intents.none()
intents.guilds = True
intents.messages = True
intents.message_content = True
super().__init__(
command_prefix="!",
intents=intents,
max_messages=0,
member_cache_flags=discord.MemberCacheFlags.none(),
chunk_guilds_at_startup=False,
help_command=None
)
self.db = Database(DB_URL)
self.runtime: RuntimeManager | None = None
self.build_version = None
self.maintenance_mode = False
async def setup_hook(self):
await self.db.connect()
self.runtime = RuntimeManager(self.db)
await self.runtime.setup()
await self.runtime.load_cache()
# Cogs
await self.load_extension("cogs.hermes")
await self.load_extension("cogs.logger")
await self.load_extension("cogs.master")
# await self.load_extension("cogs.health")
await self.tree.sync()
try:
await self.tree.sync(guild=discord.Object(id=tortoise_guild_id))
except discord.errors.Forbidden:
print("⚙️ Development mode active")
print("✅ Synced application commands")
bot = MyBot()
async def send_restart_message(client: commands.Bot):
try:
commit_hash = subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
stderr=subprocess.DEVNULL,
).decode().strip()
except Exception:
commit_hash = config("BOT_BUILD_VERSION", "mystery-build")
channel = client.get_channel(system_log_channel_id)
client.build_version = commit_hash
if channel is None:
return
try:
commit = f"[{commit_hash}]({bot_repo_link}/commit/{commit_hash})"
embed = simple_embed(message=f"Build version: {commit}", title="")
embed.set_footer(text=f"🔄 Bot Restarted")
await channel.send(
embed=embed,
)
except discord.Forbidden:
pass
@bot.tree.error
async def on_app_command_error(interaction: discord.Interaction, error):
print("APP COMMAND ERROR:", error)
if not interaction.response.is_done():
await interaction.response.send_message(
"Command failed.",
ephemeral=True
)
@bot.event
async def on_ready():
print(f"✅ Logged in as {bot.user} (ID: {bot.user.id})")
await send_restart_message(bot)
@bot.event
async def on_message(message: discord.Message):
if message.author.bot:
return
if message.guild is None:
try:
await message.channel.send(
f"Need Support? Join 👉 {discord_invite_link}"
)
except discord.Forbidden:
pass
return
await bot.process_commands(message)
async def main():
async with bot:
await bot.start(TOKEN)
if __name__ == "__main__":
asyncio.run(main())