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?
NativeMinecraftDiscord/lib/bot.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
143 lines (122 sloc)
5.65 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 os | |
import discord | |
import status | |
from mcstatus import MinecraftServer | |
from datetime import datetime | |
from datetime import timedelta | |
from shutil import copyfile | |
from googletrans import Translator | |
import database | |
import settings | |
import bot_functions as functions | |
copyfile(".env", "lib/.env") | |
from dotenv import load_dotenv | |
load_dotenv() | |
token = os.environ['TOKEN'] | |
client = discord.Client() | |
@client.event | |
async def on_ready(): | |
print(f'{client.user} has connected to Discord!') | |
status = "{} different servers".format(database.get_total("*")) | |
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=status)) | |
@client.event | |
async def on_message(message): | |
guild_id = str(hash(message.guild)) | |
if database.guild_check(guild_id) == False: | |
guild_name = str(message.guild) | |
database.new_guild(guild_id, guild_name) | |
status = "{} different servers".format(database.get_total("*")) | |
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=status)) | |
prefix = database.read_database("command_prefix", guild_id) | |
if not message.content.startswith(prefix): | |
return | |
user_message = functions.language(message.content, guild_id) | |
message_command = user_message.split() | |
message_command = message_command[0] | |
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" | |
} | |
server = MinecraftServer(database.read_database("minecraft_server", guild_id), int(database.read_database("server_port", guild_id))) | |
'''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 = "" | |
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) | |
elif ('source') in message_command: | |
msg = "Self Hosted: https://github.coventry.ac.uk/hollan84/MinecraftDiscord\nHosted: https://github.coventry.ac.uk/hollan84/NativeMinecraftDiscord" | |
elif ('share') in message_command: | |
msg = "Add to other servers: http://minecraftbot.alastairserver.co.uk" | |
elif ('map') in message_command: | |
msg = database.read_database("map", guild_id) | |
elif ('server') in message_command: | |
msg = database.read_database("minecraft_server", guild_id) | |
elif ('download') in message_command: | |
msg = database.read_database("download", guild_id) | |
elif ('version') in message_command: | |
msg = "Minecraft Bot - Version {}".format(str(os.environ['VERSION'])) | |
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("*")) | |
#msg = "{}Total Minecraft Servers: {}\n".format(msg, database.get_total("minecraft_server")) | |
#msg = "{}Percentage of servers using maps: {}%\n".format(msg, str((int(database.get_total("map"))/int(database.get_total("minecraft_server"))*100))) | |
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: | |
try: | |
server.status() | |
msg = "{} is online".format(database.read_database("minecraft_server", guild_id)) | |
except: | |
msg = "{} doesn't seem to be repsonding; please message {} for help".format(database.read_database("minecraft_server", guild_id), "the admins") | |
checkNicknames = msg.split("\n - ") | |
for player in checkNicknames: | |
if len(database.read_player(player)) > 0: | |
nick = "{} [{}]".format(player, database.read_player(player)) | |
msg = msg.replace(player, nick) | |
elif ('settings') in message_command: | |
if not database.sql_guard(message.content): | |
return | |
if "Minecraft Bot" in str(message.author.roles): | |
msg = settings.settings(message.content, guild_id) | |
else: | |
msg = "You need to have the role 'Minecraft Bot' to change these settings" | |
elif ('minecraftnickname') in message_command: | |
splitMessage = message.content | |
splitMessage = splitMessage.split() | |
database.add_player(splitMessage[1], splitMessage[2]) | |
msg = "Set " + splitMessage[1] + "'s nickname to " + splitMessage[2] | |
elif message.content.startswith(prefix): | |
msg = "That is not a valid command, use {}help for help".format(prefix) | |
else: | |
return | |
if msg == None: | |
msg = "This setting doesn't appear to be currently set, use {}settings to change this".format(prefix) | |
channel = message.channel | |
await channel.send(msg) | |
client.run(token) |