lostark-bot/bot.py

138 lines
4.4 KiB
Python

import json
import discord
from discord.ext import commands, tasks
import requests
from bs4 import BeautifulSoup
import datetime
# 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 = {}
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
def get_servers_status():
print("Getting servers", end = " ")
print(str(datetime.datetime.now()))
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
# Ignore if we're not mentioned
if client.user not in message.mentions:
return
# Remove all mentions
contents = message.content.split()
msg = ""
for fragment in contents:
if fragment.startswith("<") == False:
msg = fragment
# Subscribe
if msg.startswith("+"):
status = ""
for server in globalList["la_servers"]:
if server["Server"] == msg[1:]:
status = server["Status"]
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"])
print("Channel", end = " ")
print(sub["Channel"])
print("Server", end = " ")
print(sub["Server"])
if channel is not None:
await channel.send(curr["Server"] + " changed status to " + curr["Status"])
globalList["la_servers"] = curr_status
client.run(token)