Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatbot/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
58 lines (48 sloc)
1.86 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
import sys, traceback | |
import mysql.connector | |
from botClient import client, botToken, dbuser, dbpasswd | |
"""Select the modules to load""" | |
extensions = ['basicFunctionsFile', 'adminFunctionsFile', 'helpFile', 'gameFunctionsFile', | |
'apiFunctionsFile', 'imageSearchFunctionsFile', | |
'musicPlayerFile', 'commandErrorHandlerFile'] | |
"""Attempt connection to the MySQL database""" | |
try: #try to connect to the MySQL database | |
mydb= mysql.connector.connect( | |
host ="localhost", | |
user = dbuser, | |
passwd = dbpasswd, | |
database = "testdatabase" | |
) | |
print('Successfully connected to the MySQL database.') | |
mycursor = mydb.cursor() | |
# mycursor.execute("SHOW DATABASES") | |
for x in mycursor: | |
print(x) | |
except mysql.connector.errors.InterfaceError as error: #if bot can't connect to db (e.g it's offline) | |
print(error) | |
"""Confirms the bot is ready""" | |
@client.event | |
async def on_ready(): | |
print('-----------------------') | |
print('Logged in as') | |
print(client.user.name) | |
print('CID: ' + str(client.user.id)) | |
print('-----------------------') | |
"""Sets the bot status message on Discord""" | |
text = discord.Game(">comhelp | Waiting for rA9") | |
await client.change_presence(status=discord.Status.online, activity=text) | |
"""Load the modules selected above in the extensions""" | |
if __name__ == '__main__': | |
for extension in extensions: | |
try: | |
client.load_extension(extension) | |
except Exception as error: | |
print(f'{extension} cannot be loaded [{error}]') | |
@client.event | |
async def on_message(message): | |
"""So that the bot doesn't reply to itself.""" | |
if message.author.id == client.user.id: | |
return | |
await client.process_commands(message) #So that events don't disable commands | |
client.run(botToken) |