-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
92 lines (72 loc) · 3.32 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
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
import os
import discord
from discord.ext import commands, tasks
from dotenv import load_dotenv
from main_resources.events import Events
from main_resources.functions import *
from main_resources.loops import Loops
# --------------------------------Variables--------------------------------#
load_dotenv() # Load env to initialize all dataset
# The url for updating server count.
total_guilds_api_url = os.getenv('TOTAL_GUILDS_API_URI')
database = create_database_connection(os.getenv('MONGODB_URL'))
guilds_data = database['guilds_data']
guild_prefix_storage = create_guild_store(guilds_data)
disabled_commands_store = create_disabled_commands_store(guilds_data)
# ---------------------------------Prefix----------------------------------#
DEFAULT_PREFIX = os.getenv('PREFIX')
# Get prefix per server
def get_prefix(bot, message: discord.Message):
guild_id = message.guild.id
if guild_id in guild_prefix_storage:
return guild_prefix_storage[guild_id]
else:
add_guild(guilds_data, guild_id, DEFAULT_PREFIX)
return DEFAULT_PREFIX
# -----------------------------------Bot-----------------------------------#
bot = commands.Bot(command_prefix=get_prefix, help_command=None, case_insensitive=True)
custom_statuses = [f'{DEFAULT_PREFIX}help', 'WhiteHatJr SEO', ' with wolf gupta', 'ChintuAI']
check_class = before_invoke(disabled_commands_store)
bot.before_invoke(check_class.check_before_invoke)
# --------------------------------Main startup event--------------------------------#
@bot.event
async def on_ready():
change_status.start()
clear_game.start()
print("Updating databases...")
update_guilds_data(bot, guilds_data, DEFAULT_PREFIX)
print(f'Logged in as {bot.user.name}#{bot.user.discriminator} ID = {bot.user.id}')
# --------------------------------Task loops--------------------------------#
loops = Loops(bot, custom_statuses, database)
change_status = tasks.loop(seconds=60)(loops.change_status)
clear_game = tasks.loop(seconds=10)(loops.clear_game)
# --------------------------------Events--------------------------------#
events = Events(bot, database, total_guilds_api_url, guild_prefix_storage, disabled_commands_store, DEFAULT_PREFIX,
["help", "kick", "ban", "warn", "warninfo", "warns", "mute", "unmute", "clear"],
ChintuAI=True)
bot.event(events.on_command_error)
bot.event(events.on_message)
bot.event(events.on_guild_join)
bot.event(events.on_command_completion)
bot.event(events.on_update_prefix)
bot.event(events.on_add_command)
bot.event(events.on_remove_command)
bot.event(events.on_reaction_add)
bot.event(events.on_user_data_update)
bot.event(events.on_guild_data_update)
# --------------------------------Load Extensions/cogs--------------------------------#
def load_extensions(fun_bot, unloaded_cogs: list):
"""Loads all extensions (Cogs) from the cogs directory"""
if unloaded_cogs is None:
unloaded_cogs = []
for filename in os.listdir('./cogs'):
if filename.endswith(".py"):
if filename not in unloaded_cogs:
fun_bot.load_extension(f'cogs.{filename[:-3]}')
if __name__ == '__main__':
print("Loading extensions...")
load_extensions(bot, ["Utilities.py", "Help.py"])
bot.load_extension("cogs.Utilities")
bot.load_extension("cogs.Help")
print("Logging in...")
bot.run(os.getenv("TOKEN"))