From 1abeadcb584143226ff00e412bedd61962cebccf Mon Sep 17 00:00:00 2001 From: Alastair Holland Date: Thu, 21 May 2020 23:56:32 +0100 Subject: [PATCH] Cleaned up larger variables --- lib/bot.py | 70 ++++++++++++-------------------------------- lib/bot_functions.py | 42 ++++++++++---------------- lib/database.py | 4 +-- lib/databaseSetup.py | 4 ++- lib/variables.py | 25 ++++++++++++++++ 5 files changed, 64 insertions(+), 81 deletions(-) create mode 100755 lib/variables.py diff --git a/lib/bot.py b/lib/bot.py index 7cc6b62..69630d3 100755 --- a/lib/bot.py +++ b/lib/bot.py @@ -13,6 +13,8 @@ import settings import developer import bot_functions as functions +from variables import * + copyfile(".env", "lib/.env") #Copies the env file to the lib directory from dotenv import load_dotenv @@ -47,50 +49,13 @@ async def on_message(message): message_command = user_message.split() message_command = message_command[0] #Gets the command to be checked which increases speed of the checks - sites = { #Sets the sites that may be used later in the program - "support" : "http://devchat.alastairserver.co.uk/", - "paypal" : "http://donate.alastairserver.co.uk/", - "custom_github" : "http://customminecraftbotgithub.alastairserver.co.uk/", - "hosted_github" : "http://minecraftbotgithub.alastairserver.co.uk/", - "normal_bot" : "http://minecraftbot.alastairserver.co.uk", - "developer_bot" : "http://minecraftbotdev.alastairserver.co.uk/" - } - - commands = { #Sets the commands for the help - "server" : "Displays the saved server address", - "status" : "Gets the status of the server along with players (players only display if the server allows it)", - "minecraftNickname" : "Sets the nickname of a username to allow easy recognition when using the status command", - "download" : "Gives the world download link if there is one available", - "map": "Gives the map link if there is one available (usually using dynmap)", - "settings" : "Sets the server settings of this bot. Requires the role 'Minecraft Bot' to change", - "source" : "Gives the bot source code from Github", - "share" : "Add the bot to other servers", - "botinfo" : "Get information on the bot", - "version" : "Gives the bot release version", - "donate" : "Allows you to donate to the development of this project" - } - server = MinecraftServer(database.read_database("minecraft_server", guild_id), int(database.read_database("server_port", guild_id))) #Read the minecraft database '''Checking which command has been issued''' if message.author == client.user: #Make sure that the bot doesn't reply to itself return elif ('help') in message_command: - msg = "" #Sets a blank message to add to - for command in commands: #Iterates through the list of commands - if command in user_message: #Checks to see if the user is checking a specific command instead of the command list - msg = "{}{} - {}".format(prefix, command, commands[command]) #Sets msg to the help for that specific command - if len(msg) == 0: #Checks to see if msg has aleady been added to - msg = "Commands:\n" - for command in commands: #Iterates over the commands - if command == "map" or command == "download": #Checks to see if the command to be listed is 'map' or 'download' (Removes 'map'/'download' if not set in settings) - if database.read_database(command, guild_id) is not None: #If there is a value assocated with the value 'map' or 'download' in the guild record - msg = "{}- {}{}\n".format(msg, prefix, command) #Add map or download to the msg - else: - msg = "{}- {}{}\n".format(msg, prefix, command) #Add command to the message - msg = "{}If you want more information on a specific command use '{}help [command name]'".format(msg, prefix) #Additional help - - '''Simple one line bot responses''' + msg = functions.help(user_message, prefix, guild_id) elif ('source') in message_command: msg = "Self Hosted: {}\nHosted: {}".format(sites["custom_github"], sites["hosted_github"]) elif ('share') in message_command: @@ -104,11 +69,9 @@ async def on_message(message): elif ('version') in message_command: msg = "Minecraft Bot - Version {}".format(str(os.environ['VERSION'])) elif ('donate') in message_command: - msg = "Donate to the development of this bot here:\n{}".format(sites["paypal"]) + msg = "Donate to the development of this bot here:\n{}\nIf you want to be added to the donator list, add your Discord ID as a reference".format(sites["paypal"]) elif ('support') in message_command: msg = "Join our support server here:\n{}".format(sites["support"]) - - '''More complex responses''' elif ('botinfo') in message_command: msg = "Minecraft Bot - Version {}\n".format(str(os.environ['VERSION'])) msg = "{}Total Discord Servers: {}\n".format(msg, database.get_total("*")) @@ -117,19 +80,22 @@ async def on_message(message): elif ('developer') in message_command: msg = developer.developer_check(user_message, message.author, guild_id) elif ('status') in message_command: - try: - status = server.status() - query = server.query() - if status.players.online > 0: - msg = str("The server has {0} players and replied in {1} ms\nPlayers:\n - {2}".format(status.players.online, status.latency, "\n - ".join(query.players.names))) - else: - msg = str("The server has {0} players and replied in {1} ms".format(status.players.online, status.latency)) - except: + if not database.read_database("minecraft_server", guild_id) == None or not database.read_database("minecraft_server", guild_id) == "None": try: - server.status() - msg = "{} is online".format(database.read_database("minecraft_server", guild_id)) + status = server.status() + query = server.query() + if status.players.online > 0: + msg = str("The server has {0} players and replied in {1} ms\nPlayers:\n - {2}".format(status.players.online, status.latency, "\n - ".join(query.players.names))) + else: + msg = str("The server has {0} players and replied in {1} ms".format(status.players.online, status.latency)) except: - msg = "{} doesn't seem to be repsonding; please message {} for help".format(database.read_database("minecraft_server", guild_id), "the admins") + try: + server.status() + msg = "{} is online".format(database.read_database("minecraft_server", guild_id)) + except: + msg = "{} doesn't seem to be responding; please message {} for help".format(database.read_database("minecraft_server", guild_id), "the admins") + else: + msg = "The Minecraft server is not set, use '{}settings help' to see how to change this".format(prefix) checkNicknames = msg.split("\n - ") for player in checkNicknames: if len(database.read_player(player)) > 0: diff --git a/lib/bot_functions.py b/lib/bot_functions.py index 6a509c5..3887772 100755 --- a/lib/bot_functions.py +++ b/lib/bot_functions.py @@ -2,34 +2,24 @@ import database from googletrans import Translator -def help(user_message, prefix, guild_id): +from variables import * - commands = { - "server" : "Displays the saved server address", - "status" : "Gets the status of the server along with players (players only display if the server allows it)", - "minecraftNickname" : "Sets the nickname of a username to allow easy recognition when using the status command", - "download" : "Gives the world download link if there is one available", - "map": "Gives the map link if there is one available (usually using dynmap)", - "settings" : "Sets the server settings of this bot. Requires the role 'Minecraft Bot' to change", - "source" : "Gives the bot source code from Github", - "share" : "Add the bot to other servers", - "botinfo" : "Get information on the bot", - "version" : "Gives the bot release version" - } +def help(user_message, prefix, guild_id): - msg = "" - for command in commands: - if command in user_message: - msg = "{}{} - {}".format(prefix, command, commands[command]) - if len(msg) == 0: - msg = "Commands:\n" - for command in commands: - if command == "map" or command == "download": - if database.read_database(command, guild_id) is not None: - msg = "{}- {}{}\n".format(msg, prefix, command) - else: - msg = "{}- {}{}\n".format(msg, prefix, command) - msg = "{}If you want more information on a specific command use '{}help [command name]'".format(msg, prefix) + msg = "" #Sets a blank message to add to + for command in commands: #Iterates through the list of commands + if command in user_message: #Checks to see if the user is checking a specific command instead of the command list + msg = "{}{} - {}".format(prefix, command, commands[command]) #Sets msg to the help for that specific command + if len(msg) == 0: #Checks to see if msg has aleady been added to + msg = "Commands:\n" + for command in commands: #Iterates over the commands + if command == "map" or command == "download": #Checks to see if the command to be listed is 'map' or 'download' (Removes 'map'/'download' if not set in settings) + if database.read_database(command, guild_id) is not None: #If there is a value assocated with the value 'map' or 'download' in the guild record + msg = "{}- {}{}\n".format(msg, prefix, command) #Add map or download to the msg + else: + msg = "{}- {}{}\n".format(msg, prefix, command) #Add command to the message + msg = "{}If you want more information on a specific command use '{}help [command name]'".format(msg, prefix) #Additional help + return msg def language(user_message, guild_id): if database.read_database("language", guild_id) != "en": #If the server is not set as english diff --git a/lib/database.py b/lib/database.py index 8327f98..b4bc0ba 100755 --- a/lib/database.py +++ b/lib/database.py @@ -105,7 +105,7 @@ def read_database(header_name, guild_id): sql = '''SELECT {} FROM servers WHERE guild_id == {}'''.format(header_name, guild_id) - database_read(sql) + data = database_read(sql) if data: data = data[0] if data[0]: @@ -115,7 +115,7 @@ def read_database(header_name, guild_id): def get_total(header_name): sql = '''SELECT {} FROM servers'''.format(header_name) - return len(read_database(sql)) + return len(database_read(sql)) def insert_new_guild(conn, sql_vars): sql = '''INSERT INTO servers(guild_id, guild_name, server_port, command_prefix, antispam, language) diff --git a/lib/databaseSetup.py b/lib/databaseSetup.py index 9f32489..28bb632 100755 --- a/lib/databaseSetup.py +++ b/lib/databaseSetup.py @@ -2,6 +2,8 @@ import sqlite3 from sqlite3 import Error import os +from variables import * + def create_initial_connection(db_file): conn = None try: @@ -81,6 +83,6 @@ def main(): create_table(conn, create_player_table_sql) create_table(conn, create_dev_table_sql) - add_primary_developer(conn, "AlastairHolland#6538") + add_primary_developer(conn, PrimaryDeveloper) conn.commit() \ No newline at end of file diff --git a/lib/variables.py b/lib/variables.py new file mode 100755 index 0000000..09ab203 --- /dev/null +++ b/lib/variables.py @@ -0,0 +1,25 @@ +sites = { #Sets the sites + "support" : "http://devchat.alastairserver.co.uk/", + "paypal" : "http://donate.alastairserver.co.uk/", + "custom_github" : "http://customminecraftbotgithub.alastairserver.co.uk/", + "hosted_github" : "http://minecraftbotgithub.alastairserver.co.uk/", + "normal_bot" : "http://minecraftbot.alastairserver.co.uk", + "developer_bot" : "http://minecraftbotdev.alastairserver.co.uk/" + } + +commands = { #Sets the commands for the help + "server" : "Displays the saved server address", + "status" : "Gets the status of the server along with players (players only display if the server allows it)", + "minecraftNickname" : "Sets the nickname of a username to allow easy recognition when using the status command", + "download" : "Gives the world download link if there is one available", + "map": "Gives the map link if there is one available (usually using dynmap)", + "settings" : "Sets the server settings of this bot. Requires the role 'Minecraft Bot' to change", + "source" : "Gives the bot source code from Github", + "share" : "Add the bot to other servers", + "support" : "Join our support server", + "botinfo" : "Get information on the bot", + "version" : "Gives the bot release version", + "donate" : "Allows you to donate to the development of this project" + } + +PrimaryDeveloper = "AlastairHolland#6538" \ No newline at end of file