-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
155 lines (127 loc) · 4.2 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
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
147
148
149
150
151
152
153
154
155
##############################################
# Package Imports
##############################################
import discord
import discord.ext
import os
import sys
sys.path.insert(1, './cogs/support')
from discord import Guild, Message
from discord_components import DiscordComponents, ComponentsBot, Button
from discord.ext import commands
from discord.ext.commands import Context
import database
from log import ConsoleLog
from timer import Timer
##############################################
# Constants and Setup
##############################################
MODULE = "MAIN"
# Initialize Bot with cmd prefix
bot = commands.Bot(
command_prefix = '!',
owner = os.environ.get("OWNER_ID"))
bot.remove_command( 'help' )
DiscordComponents(bot)
DB_SETUP_PATH = "scripts/travelerdb-setup.sql"
loadTimer = Timer()
loadTimer.start()
# Connect to MySQL DB
db = database.DB()
db.start()
db.executeScriptFromFile(DB_SETUP_PATH)
db.stop()
logging = ConsoleLog()
logging.printSpacer()
logging.send( MODULE, "Starting up the bot!")
# Import Cogs from /cogs directory
logging.printSpacer()
logging.send(MODULE,
"Attempting load of extensions in '/cogs' directory..." )
logging.printSpacer()
if __name__ == "__main__":
extCount = 0
for file in os.listdir( "./cogs" ):
if file.endswith( ".py" ):
extension = file[:-3]
logging.send( MODULE, f"> {extension} ")
extCount += 1
logging.send( MODULE , f"{extCount} extensions found in '/cogs'. Now loading...")
logging.printSpacer()
allExtensionsLoaded = True
if __name__ == "__main__":
for file in os.listdir( "./cogs" ):
if file.endswith( ".py" ):
extension = file[:-3]
# Attempt to load extension
# Some python files might not have a properly
# configured setup() method, need to account for that.
try:
bot.load_extension( f"cogs.{extension}" )
logging.send( MODULE, f"Loaded extension '{extension}'" )
except Exception as e:
exception = f"{type(e).__name__}: {e}"
logging.send( MODULE, f"Failed to load extension {extension}\n{exception}" )
allExtensionsLoaded = False
logging.printSpacer()
# Display success / failure on console
if allExtensionsLoaded:
logging.send( MODULE, "SUCCESS: All extensions in '/cogs' directory loaded successfully!" )
else:
logging.send( MODULE, "WARNING: One or more extensions could not be loaded. See above for error output." )
logging.printSpacer()
timeStr = loadTimer.stop()
logging.send( MODULE, f"LOAD TIME: {timeStr} seconds" )
logging.printSpacer()
##############################################
# Events
##############################################
@bot.event
async def on_guild_join( guild: Guild ) -> None:
guildID = str(guild.id)
return
@bot.event
async def on_message( message: Message ) -> None:
"""
Defines behavior for bot on receiving message in chat
"""
# Ignore messages from self or other bots
if message.author == bot.user or message.author.bot:
return
await bot.process_commands(message)
return
@bot.event
async def on_ready():
"""
Defines behavior for bot when ready to execute commands
"""
# Change status on Discord
await bot.change_presence(
activity = discord.Activity(
type = discord.ActivityType.watching,
name = 'the sands of time... | !s help' ) )
# Console output for debugging
logging.printSpacer()
logging.send( MODULE, "Logged in as" )
logging.send( MODULE, bot.user.name )
logging.send( MODULE, bot.user.id )
logging.printSpacer()
return
@bot.event
async def on_command_completion( ctx: Context ) -> None:
"""
Defines behavior for bot whenever a command is completed successfully
"""
# Console output to note whenever the bot successfully
# runs a command
fullCommandName = ctx.command.qualified_name
split = fullCommandName.split( " " )
executedCommand = str(split[0])
logging.send( MODULE,
f"Executed {executedCommand} command in {ctx.guild.name} (ID: {ctx.message.guild.id}) by {ctx.message.author} (ID: {ctx.message.author.id})" )
return
##############################################
# Bot / Server Initialization
##############################################
# Runs the bot for use on Discord
bot.run(os.environ.get("SOC_DISCORD_TOKEN"))