-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
67 lines (58 loc) · 2.49 KB
/
main.py
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
import nextcord
from nextcord.ext import commands
import sys
import traceback
import os
import util.settings as settings
import importlib.util
import logging
from util.errorhandling import setup_logging
setup_logging()
intents = nextcord.Intents.all()
bot = commands.Bot(command_prefix=settings.BOT_PREFIX, intents=intents, help_command=None)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user.name} (ID: {bot.user.id})")
print(f"Connected to {len(bot.guilds)} guilds and {len(set(bot.get_all_members()))} members.")
print(f"Invite URL: {nextcord.utils.oauth_url(bot.user.id)}")
activity = nextcord.Game(name=settings.BOT_STATUS)
await bot.change_presence(activity=activity)
@bot.event
async def on_guild_join(guild):
logging.info(f"Joined guild: {guild.name} (ID: {guild.id})")
@bot.event
async def on_guild_remove(guild):
logging.info(f"Left guild: {guild.name} (ID: {guild.id})")
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.send("Command not found.")
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f"Missing required argument: {error.param.name}")
elif isinstance(error, commands.CheckFailure):
await ctx.send("You don't have permission to use this command.")
elif isinstance(error, commands.CommandInvokeError) and isinstance(error.original, nextcord.Forbidden):
await ctx.send("I lack the necessary permissions to perform this action.")
else:
logging.info(f'Ignoring exception in command {ctx.command}:', file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
def has_setup_function(module_name):
module_spec = importlib.util.find_spec(module_name)
if module_spec is None:
return False
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
return hasattr(module, 'setup')
for entry in os.listdir("cogs"):
if entry.endswith('.py'):
module_name = f"cogs.{entry[:-3]}"
if has_setup_function(module_name):
bot.load_extension(module_name)
elif os.path.isdir(f"cogs/{entry}"):
for filename in os.listdir(f"cogs/{entry}"):
if filename.endswith('.py'):
module_name = f"cogs.{entry}.{filename[:-3]}"
if has_setup_function(module_name):
bot.load_extension(module_name)
if __name__ == "__main__":
bot.run(settings.BOT_TOKEN)