hnshosting-wp/discord-bot/bot.py

164 lines
6.8 KiB
Python
Raw Permalink Normal View History

2023-08-16 17:43:30 +10:00
import os
from dotenv import load_dotenv
import discord
from discord import app_commands
import requests
import asyncio
2023-08-16 17:43:30 +10:00
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
ADMINID = 0
Master_IP = os.getenv('MASTER_IP')
2023-08-17 13:27:57 +10:00
Master_Port = os.getenv('MASTER_PORT')
2023-08-24 11:37:13 +10:00
if Master_Port == None:
Master_Port = "5000"
2023-08-16 17:43:30 +10:00
2023-08-25 14:26:59 +10:00
FREE_LICENCE = os.getenv('FREE_MODE')
if FREE_LICENCE == None:
FREE_LICENCE = False
2023-08-25 14:20:58 +10:00
else:
if FREE_LICENCE.lower() == "true":
FREE_LICENCE = True
else:
FREE_LICENCE = False
2023-08-16 17:43:30 +10:00
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
@tree.command(name="addworker", description="Adds a worker to the master server")
async def addworker(ctx, ip: str,privateip: str, name: str):
2023-08-24 11:33:02 +10:00
if ctx.user.id == ADMINID:
r = requests.post(f"http://{Master_IP}:{Master_Port}/add-worker?worker={name}&ip={ip}&priv={privateip}",headers={"key":os.getenv('WORKER_KEY')})
2023-08-16 17:43:30 +10:00
if r.status_code == 200:
await ctx.response.send_message(f"Worker {name} added to the master server",ephemeral=True)
else:
await ctx.response.send_message(f"Error adding worker {name} to the master server\n" + r.text,ephemeral=True)
else:
await ctx.response.send_message("You do not have permission to use this command",ephemeral=True)
2023-08-17 13:27:57 +10:00
@tree.command(name="listworkers", description="Lists all workers on the master server")
async def listworkers(ctx):
2023-08-24 11:33:02 +10:00
if ctx.user.id == ADMINID:
2023-08-17 13:27:57 +10:00
r = requests.get(f"http://{Master_IP}:{Master_Port}/list-workers",headers={"key":os.getenv('WORKER_KEY')})
if r.status_code == 200:
json = r.json()
if json['success'] == "true":
await ctx.response.send_message(json['workers'],ephemeral=True)
else:
await ctx.response.send_message(f"Error listing workers\n" + json['error'],ephemeral=True)
2023-08-17 13:27:57 +10:00
else:
await ctx.response.send_message(f"Error listing workers\n" + r.text,ephemeral=True)
else:
await ctx.response.send_message("You do not have permission to use this command",ephemeral=True)
2023-09-18 14:47:14 +10:00
update_bot_status()
2023-08-16 17:43:30 +10:00
2023-08-17 16:34:58 +10:00
@tree.command(name="licence", description="Gets a licence key")
async def license(ctx):
if ctx.user.id != ADMINID:
2023-08-25 14:01:36 +10:00
await ctx.response.send_message("You do not have permission to use this command",ephemeral=True)
return
2023-08-24 13:34:35 +10:00
r = requests.post(f"http://{Master_IP}:{Master_Port}/add-licence",headers={"key":os.getenv('LICENCE_KEY')})
2023-08-17 16:34:58 +10:00
if r.status_code == 200:
2023-08-24 13:42:10 +10:00
json = r.json()
if json['success'] == "true":
2023-08-24 13:56:13 +10:00
await ctx.response.send_message("Licence: "+json['licence_key'])
2023-08-24 13:42:10 +10:00
else:
await ctx.response.send_message(f"Error getting license\n" + json['error'])
2023-08-17 16:34:58 +10:00
else:
await ctx.response.send_message(f"Error getting license\n" + r.text,ephemeral=True)
@tree.command(name="createsite", description="Create a new WordPress site")
2023-08-25 14:17:12 +10:00
async def createsite(ctx, domain: str, licence: str = None):
2023-09-21 11:32:34 +10:00
# Verify domain is valid
if domain == None:
await ctx.response.send_message("You must specify a domain",ephemeral=True)
return
if "http://" in domain or "https://" in domain:
await ctx.response.send_message("You must specify a domain without http:// or https://",ephemeral=True)
return
if FREE_LICENCE == True: # If free licences are enabled then auto generate a licence
2023-08-25 14:01:36 +10:00
r = requests.post(f"http://{Master_IP}:{Master_Port}/add-licence",headers={"key":os.getenv('LICENCE_KEY')})
if r.status_code == 200:
json = r.json()
if json['success'] == "true":
licence = json['licence_key']
else:
await ctx.response.send_message(f"Error getting license\n" + json['error'])
return
2023-09-21 11:32:34 +10:00
2023-08-24 14:06:02 +10:00
r = requests.post(f"http://{Master_IP}:{Master_Port}/new-site?domain={domain}",headers={"key":licence})
2023-08-17 16:34:58 +10:00
if r.status_code == 200:
2023-08-24 13:56:13 +10:00
json = r.json()
if json['success'] == "true":
2023-09-21 11:32:34 +10:00
await ctx.response.send_message(f"Site https://{domain} creating...\nI'll send you a message when it's ready")
ready = False
while ready == False:
ready = await check_site_ready(domain)
if ready == False:
2023-08-25 12:54:31 +10:00
await asyncio.sleep(5)
r = requests.get(f"http://{Master_IP}:{Master_Port}/site-info?domain={domain}")
json = r.json()
if json['success'] == "true":
2023-09-21 11:32:34 +10:00
await ctx.user.send(f"Site https://{domain} is ready!\nHere is the site info for {json['domain']}\nA: `{json['ip']}`\nTLSA: `{json['tlsa']}`\nMake sure you put the TLSA in either `_443._tcp.{domain}` or `*.{domain}`")
else:
await ctx.user.send(f"Error getting site info\n" + json['error'])
2023-08-24 13:56:13 +10:00
else:
await ctx.response.send_message(f"Error creating site\n" + json['error'])
2023-08-17 16:34:58 +10:00
else:
2023-08-24 13:56:13 +10:00
await ctx.response.send_message(f"Error creating site\n" + r.text)
2023-09-18 14:47:14 +10:00
update_bot_status()
2023-08-17 16:34:58 +10:00
2023-08-24 14:25:58 +10:00
@tree.command(name="siteinfo", description="Get info about a WordPress site")
async def siteinfo(ctx, domain: str):
r = requests.get(f"http://{Master_IP}:{Master_Port}/site-info?domain={domain}")
if r.status_code == 200:
json = r.json()
if json['success'] == "true":
2023-08-24 14:50:20 +10:00
await ctx.response.send_message(f"Here is the site info for {json['domain']}\nA: `{json['ip']}`\nTLSA: `{json['tlsa']}`\nMake sure you put the TLSA in either `_443._tcp.{domain}` or `*.{domain}`")
2023-08-24 14:25:58 +10:00
else:
await ctx.response.send_message(f"Error getting site info\n" + json['error'])
else:
await ctx.response.send_message(f"Error getting site info\n" + r.text)
async def check_site_ready(domain):
r = requests.get(f"http://{Master_IP}:{Master_Port}/site-info?domain={domain}")
if r.status_code == 200:
json = r.json()
if json['success'] == "true":
return True
else:
return False
else:
return False
2023-09-18 14:47:14 +10:00
2023-09-18 14:49:22 +10:00
def get_site_count():
2023-09-18 14:47:14 +10:00
r = requests.get(f"http://{Master_IP}:{Master_Port}/site-count")
if r.status_code == 200:
return r.text
else:
return "Error getting site count\n" + r.text
def update_bot_status():
2023-09-18 14:49:22 +10:00
site_count = get_site_count()
2023-09-18 14:47:14 +10:00
client.loop.create_task(client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="over " + site_count + " wordpress sites")))
2023-08-16 17:43:30 +10:00
# When the bot is ready
@client.event
async def on_ready():
global ADMINID
ADMINID = client.application.owner.id
await tree.sync()
2023-09-18 14:47:14 +10:00
# Get the number of sites
2023-09-18 14:49:22 +10:00
site_count = get_site_count()
2023-09-18 14:47:14 +10:00
await client.loop.create_task(client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="over " + site_count + " wordpress sites")))
2023-08-16 17:43:30 +10:00
2023-08-17 16:34:58 +10:00
client.run(TOKEN)