Skip to content
Permalink
93faa04537
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 107 lines (95 sloc) 3.3 KB
import os
import discord
import status
from mcstatus import MinecraftServer
from datetime import datetime
from datetime import timedelta
server = MinecraftServer(os.environ['SERVER'], int(os.environ['PORT']))
status = server.status()
print("The server has {0} players and replied in {1} ms".format(status.players.online, status.latency))
query = server.query()
print("The server has the following players online: {0}".format(", ".join(query.players.names)))
token = os.environ['TOKEN']
client = discord.Client()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
global oldauthor
global oldcommand
global oldmsg
global messagetime
oldcommand = ""
oldauthor = ""
oldmsg = ""
messagetime = datetime.now().time()
@client.event
async def on_message(message):
server = MinecraftServer(os.environ['SERVER'], int(os.environ['PORT']))
print(message.content)
'''Anti Spam Variables'''
global oldauthor
global oldcommand
global oldmsg
global messagetime
msg = ""
'''Checking which command has been issued'''
if message.author == client.user: #Make sure that the bot doesn't reply to itself
return
elif message.content.startswith('?map'):
msg = os.environ['MAP']
elif message.content.startswith('?servername'):
msg = os.environ['SERVER']
elif message.content.startswith('?help'):
msg = "Commands: \n - ?help \n - ?servername \n - ?map\n - ?status\n - ?minecraftNickname [USERNAME] [NICKNAME]\n - ?download"
elif message.content.startswith('?download'):
msg = os.environ['DOWNLOAD']
elif message.content.startswith('?status'):
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:
msg = "Server doesn't seem to be repsonding; please message {} for help".format(os.environ["ADMINNAME"])
checkNicknames = msg.split("\n - ")
for i in checkNicknames:
path = "nicknames/" + i
try:
n = open(path, "r")
nick = i + " [" + n.read() + "]"
msg = msg.replace(i, nick)
n.close()
except:
msg = msg
elif message.content.startswith('?minecraftNickname'):
splitMessage = message.content
splitMessage = splitMessage.split()
path = "nicknames/" + splitMessage[1]
f = open(path, "w")
f.write(splitMessage[2])
f.close()
msg = "Set " + splitMessage[1] + "'s nickname to " + splitMessage[2]
elif message.content.startswith('?'):
msg = "That is not a valid command, use ?help for help"
else:
return
if (message.content == oldcommand) and (message.author == oldauthor):
if os.environ['ANTISPAM'] == "TRUE":
s1 = str(messagetime)
s2 =str(datetime.now().time())
FMT = '%H:%M:%S.%f'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
if tdelta.seconds < 60:
if "Please don't spam commands" in oldmsg:
return
else:
msg = "Please don't spam commands\nWait " + str(round(60-tdelta.seconds, 0)) + " seconds to use this command"
messagetime = datetime.now().time()
oldauthor = message.author
oldcommand = message.content
oldmsg = msg
channel = message.channel
await channel.send(msg)
client.run(token)