initial commit

This commit is contained in:
Joseph Montanaro 2020-05-08 21:39:28 -07:00
commit 4ca13061d2
3 changed files with 76 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__*

39
bot.py Normal file
View File

@ -0,0 +1,39 @@
import logging
import os
from discord.ext import commands
import heavynode
logging.basicConfig()
hn = heavynode.Client(os.environ['heavynode_token'])
bot = commands.Bot(command_prefix='!')
@bot.command()
@commands.has_role('Server admin')
async def add(ctx, player):
"""Add a player to the server whitelist.Must use exact Minecraft name."""
await hn.send_command('d030737c', f'whitelist add {player}')
await ctx.send(f'"{player}" added to whitelist.')
@bot.command()
@commands.has_role('Server admin')
async def remove(ctx, player):
"""Remove a player from the server whitelist. Must use exact Minecraft name."""
await hn.send_command('d030737c', f'whitelist remove {player}')
await ctx.send(f'"{player}" removed from whitelist.')
@add.error
@remove.error
async def whitelist_error(ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send('You must be a server admin to use this command.')
else:
raise error
bot.run(os.environ['discord_token'], reconnect=True)

36
heavynode.py Normal file
View File

@ -0,0 +1,36 @@
import urllib.parse
import aiohttp
class HttpError(Exception):
def __init__(self, msg, response):
self.response = response
super().__init__(msg)
class Client:
def __init__(self, token):
self.token = token
self.session = aiohttp.ClientSession()
self.baseurl = 'https://control.heavynode.com/api'
async def make_request(self, method, path, *args, **kwargs):
h = {'Authorization': f'Bearer {self.token}'}
if 'headers' in kwargs:
kwargs['headers'].update(h)
else:
kwargs['headers'] = h
if path[0] != '/':
path = '/' + path
url = self.baseurl + path
r = await self.session.request(method, url, *args, **kwargs)
if r.status >= 400:
raise HttpError(f'Request failed with status code {r.status}', r)
return r
async def send_command(self, serverid, cmd):
payload = {'command': cmd}
return await self.make_request('POST', f'/client/servers/{serverid}/command', json=payload)