-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
146 lines (128 loc) · 5.12 KB
/
bot.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
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
138
139
140
141
142
143
144
145
146
# Main bot file, starts the bot and everything else from here
import os
import discord
from discord.ext import commands, tasks
from dotenv import load_dotenv
import server.database.database_sqlite as db
# The prefix of the commands that the bot uses
BOT_PREFIX = "."
client = commands.Bot(command_prefix=BOT_PREFIX)
client.remove_command("help")
HAS_STARTED_DATABASE = False
# The current directory the bot is sitting in.
CURRENT_DIR = os.getcwd().replace("\\", "/")
@client.event
async def on_ready():
"""
Whenever the bot is loaded and ready for action, it writes to the console.
Checks if the databases and tables inside of it exist, and if they don't, calls functions to create them.
:return:
"""
global HAS_STARTED_DATABASE
print("Bot is ready")
if not HAS_STARTED_DATABASE:
db.create_members_info_table()
db.create_activities_table()
db.create_statuses_table()
# create_perms_tables() 'It is not being used yet so it is not being created.'
HAS_STARTED_DATABASE = True
print("Database is ready")
delete_extra_db_logs.start()
@client.command(pass_context=True, aliases=["Help", "HELP"])
async def help(ctx, *cog):
"""
Help command: gathers all cogs and commands that the bot has and sends them via Private Message to the user using
the help command.
:param ctx: Context
"""
global halp
global BOT_PREFIX
try:
if not cog:
"""Cog listing."""
halp = discord.Embed(
title="Cog Listing and Uncatergorized Commands",
description=f"Use `{BOT_PREFIX}help *cog*` to find out more about them!\n(BTW, "
f"the Cog Name Must Be in Title Case, Just Like this Sentence.)",
)
cogs_desc = ""
for x in client.cogs:
cogs_desc += "{} - {}".format(x, client.cogs[x].__doc__) + "\n"
halp.add_field(
name="Cogs", value=cogs_desc[0: len(cogs_desc) - 1], inline=False)
cmds_desc = ""
for y in client.walk_commands():
if not y.cog_name and not y.hidden:
cmds_desc += "{} - {}".format(y.name, y.help) + "\n"
halp.add_field(
name="Uncatergorized Commands",
value=cmds_desc[0: len(cmds_desc) - 1],
inline=False,
)
await ctx.message.add_reaction(emoji="✉")
await ctx.message.author.send("", embed=halp)
else:
"""Helps me remind you if you pass too many args."""
if len(cog) > 1:
halp = discord.Embed(
title="Error!",
description="That is way too many cogs!",
color=discord.Color.red(),
)
await ctx.message.author.send("", embed=halp)
else:
"""Command listing within a cog."""
found = False
for x in client.cogs:
for y in cog:
if x == y:
halp = discord.Embed(
title=cog[0] + " Command Listing",
description=client.cogs[cog[0]].__doc__,
)
for c in client.get_cog(y).get_commands():
if not c.hidden:
halp.add_field(
name=c.name, value=c.help, inline=False)
found = True
if not found:
"""Reminds you if that cog doesn't exist."""
halp = discord.Embed(
title="Error!",
description='How do you even use "' + cog[0] + '"?',
color=discord.Color.red(),
)
else:
await ctx.message.add_reaction(emoji="✉")
await ctx.message.author.send("", embed=halp)
except:
await ctx.send("Excuse me, I can't send embeds.")
@tasks.loop(hours=24)
async def delete_extra_db_logs():
db.handle_database_overdraft()
def launch():
"""
Loads all of the cogs and starts the bot with the discord application.
"""
print()
print("-------------------------------")
print("Starting Analitica Discord Bot.")
print("-------------------------------")
# Load all the cogs from the files in the cogs folder on startup of bot.
for filename in os.listdir("./server/cogs"):
if filename.endswith(".py"):
try:
client.load_extension(f"server.cogs.{filename[:-3]}")
except Exception as e:
print(f"Was unable to load {filename} cog")
print("Error: " + str(e))
# Attempt to start the bot.
try:
load_dotenv()
client.run(
os.getenv("DISCORD_TOKEN")
) # This is the token that the bot will use to connect to discord.
except discord.errors.LoginFailure:
print("You have entered an improper token in the .env file.")
if __name__ == "__main__":
launch()