-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (85 loc) · 3.41 KB
/
main.py
File metadata and controls
111 lines (85 loc) · 3.41 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
import logging.config
import os
from pathlib import Path
import discord
from cogwatch import watch
from discord.app_commands import ContextMenu
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
BOT_TOKEN = os.getenv("TOKEN")
server = os.getenv("SERVER")
MY_GUILD = discord.Object(id=server)
intents = discord.Intents.all()
allowed_installs = discord.app_commands.AppInstallationType(guild=True)
if not Path.exists(Path("logs")):
Path.mkdir(Path("logs"))
logging.config.fileConfig("logging.conf")
logger = logging.getLogger("bot")
class InfoFilter(logging.Filter):
"""Filter to change INFO logs to DEBUG."""
def filter(self, record: logging.LogRecord) -> bool:
"""Change log level in the record."""
if record.levelno == logging.INFO:
record.levelno = logging.DEBUG
record.levelname = "DEBUG"
return True
cogwatcher = logging.getLogger("cogwatch")
cogwatcher.addFilter(InfoFilter())
class Bot(commands.Bot):
"""Bot class."""
def __init__(self) -> None:
"""Bot Initialization."""
super().__init__(
command_prefix="!",
case_insensitive=True,
strip_after_prefix=True,
intents=intents,
allowed_installs=allowed_installs,
)
async def setup_hook(self) -> None:
"""Setups hook for the bot."""
# This copies the global commands over to your guild.
await self.load_extensions()
await self.tree.sync()
@watch(path="cogs", default_logger=False)
async def on_ready(self) -> None:
"""Call when bot is logged in."""
from utils.database import db
await db.clear_command_cache()
await bot.change_presence(activity=discord.Game(name="/help"))
logger.info("Logged in as %s (ID: %s)", bot.user, bot.user.id)
async def load_extensions(self) -> None:
"""Load all extensions in the cogs directory."""
extension_path = "cogs"
for filename in os.listdir(extension_path):
if filename.endswith(".py") and filename != "__init__.py":
await bot.load_extension(f"{extension_path}.{filename[:-3]}")
logger.info("extension %s loaded.", filename)
bot = Bot()
@bot.tree.command(name="help", description="List of commands and their functions")
async def help(interaction: discord.Interaction) -> None:
"""Return a list of commands and their functions."""
embed = discord.Embed(
title="Help",
description="List of commands and their functions",
color=discord.Color.from_str("#bb8b3b"),
)
commands = bot.tree.get_commands()
for command in commands:
if command.name != "help" and not isinstance(command, ContextMenu):
desc = command.description
params = ", ".join(
[parameters.name for parameters in command.parameters],
)
embed.add_field(
name=f"/{command.name}",
value=f"**Description:**\n*{desc}*{f'\n**Parameters**: *{params}*' if params else ''}",
inline=True,
)
await interaction.response.send_message(embed=embed)
if __name__ == "__main__":
try:
bot.run(BOT_TOKEN)
except KeyboardInterrupt:
print("\nKeyboardInterrupt is raised. Exiting.".upper())