Added all functionality and empty subs file
This commit is contained in:
parent
6013c08fd0
commit
8688ce8c9e
117
bot.py
117
bot.py
@ -1,25 +1,126 @@
|
||||
import json
|
||||
import discord
|
||||
from discord.ext import commands, tasks
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
# Read token from file
|
||||
f = open("token.txt", "r")
|
||||
token = f.readline()
|
||||
f.close()
|
||||
|
||||
# Read channel/name pairs from file
|
||||
f = open("subs.json", "r")
|
||||
data = f.read()
|
||||
f.close()
|
||||
subs = json.loads(data)
|
||||
|
||||
la_servers = {}
|
||||
|
||||
client = discord.Client()
|
||||
|
||||
def get_servers_status():
|
||||
r = requests.get("https://www.playlostark.com/en-us/support/server-status")
|
||||
soup = BeautifulSoup(r.content, features = "html.parser")
|
||||
|
||||
servers = soup.find_all("div", class_="ags-ServerStatus-content-responses-response-server")
|
||||
server_objs = []
|
||||
for server in servers:
|
||||
obj = {}
|
||||
obj["Server"] = server.text.strip()
|
||||
if len(server.find_all("div", class_="ags-ServerStatus-content-responses-response-server-status--good")) != 0:
|
||||
obj["Status"] = "Good"
|
||||
elif len(server.find_all("div", class_="ags-ServerStatus-content-responses-response-server-status--busy")) != 0:
|
||||
obj["Status"] = "Busy"
|
||||
elif len(server.find_all("div", class_="ags-ServerStatus-content-responses-response-server-status--full")) != 0:
|
||||
obj["Status"] = "Full"
|
||||
elif len(server.find_all("div", class_="ags-ServerStatus-content-responses-response-server-status--maintenance")) != 0:
|
||||
obj["Status"] = "Maintenance"
|
||||
else:
|
||||
obj["Status"] = "Unknown"
|
||||
server_objs.append(obj)
|
||||
return server_objs
|
||||
|
||||
def save_subs():
|
||||
global subs
|
||||
f = open("subs.json", "w")
|
||||
txt = json.dumps(subs)
|
||||
f.write(txt)
|
||||
f.close()
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
check_status.start()
|
||||
print("We have logged in as {0.user}".format(client))
|
||||
globals()["la_servers"] = get_servers_status()
|
||||
|
||||
@client.event
|
||||
async def on_message(message):
|
||||
globalList = globals()
|
||||
global subs
|
||||
|
||||
# Ignore messages from us
|
||||
if message.author == client.user:
|
||||
return
|
||||
|
||||
if message.channel.id != message.author.dm_channel.id:
|
||||
# Ignore if we're not mentioned
|
||||
if client.user not in message.mentions:
|
||||
return
|
||||
|
||||
if message.content.startswith("+"):
|
||||
# SUB
|
||||
await message.channel.send("Subscribed to " + message.content[1:])
|
||||
# Remove all mentions
|
||||
contents = message.content.split()
|
||||
msg = ""
|
||||
for fragment in contents:
|
||||
if fragment.startswith("<") == False:
|
||||
msg = fragment
|
||||
|
||||
if message.content.startswith("-"):
|
||||
#UNSUB
|
||||
await message.channel.send("Unsubscribed from " + message.content[1:])
|
||||
# Subscribe
|
||||
if msg.startswith("+"):
|
||||
status = ""
|
||||
for server in globalList["la_servers"]:
|
||||
if server["Server"] == msg[1:]:
|
||||
status = server["Status"]
|
||||
|
||||
client.run("OTQyNDk2ODcwMzQ3OTk3MjQ1.YglWnA.XWmDCAuqtaLYKArJPxgyKscxB_8")
|
||||
if status == "":
|
||||
await message.channel.send("Unknown server - " + msg[1:])
|
||||
return
|
||||
|
||||
for sub in subs:
|
||||
if sub["Channel"] == message.channel.id:
|
||||
if sub["Server"] == msg[1:]:
|
||||
await message.channel.send("Already subbed to " + msg[1:] + "\nStatus - " + status)
|
||||
return
|
||||
sub = {}
|
||||
sub["Channel"] = message.channel.id
|
||||
sub["Server"] = msg[1:]
|
||||
globalList["subs"].append(sub)
|
||||
save_subs()
|
||||
await message.channel.send("Subscribed to " + msg[1:] + "\nStatus - " + status)
|
||||
return
|
||||
|
||||
# Unsubscribe
|
||||
if msg.startswith("-"):
|
||||
for idx in range(len(subs)):
|
||||
if subs[idx]["Channel"] == message.channel.id:
|
||||
if subs[idx]["Server"] == msg[1:]:
|
||||
globalList["subs"].pop(idx)
|
||||
save_subs()
|
||||
await message.channel.send("Unsubscribed from " + msg[1:])
|
||||
return
|
||||
await message.channel.send("Already unsubscribed from " + msg[1:])
|
||||
return
|
||||
|
||||
@tasks.loop(seconds = 60)
|
||||
async def check_status():
|
||||
globalList = globals()
|
||||
curr_status = get_servers_status()
|
||||
for curr in curr_status:
|
||||
for saved in globalList["la_servers"]:
|
||||
if curr["Server"] == saved["Server"] and curr["Status"] != saved["Status"]:
|
||||
for sub in globalList["subs"]:
|
||||
if sub["Server"] == curr["Server"]:
|
||||
channel = client.get_channel(sub["Channel"])
|
||||
await channel.send(curr["Server"] + " changed status to " + curr["Status"])
|
||||
globalList["la_servers"] = curr_status
|
||||
|
||||
client.run(token)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user