minebot/bot.py
Joseph Montanaro a28bc5ffeb
All checks were successful
continuous-integration/drone/push Build is passing
actually we don't even need that
2020-12-25 18:57:45 -08:00

80 lines
2.3 KiB
Python

import asyncio
import inspect
import itertools
import logging
import os
import re
import discord
from discord.ext import commands
import lib
import heavynode
DISCORD_TOKEN = lib.getconfig('DISCORD_TOKEN')
HEAVYNODE_TOKEN = lib.getconfig('HEAVYNODE_TOKEN')
COOKIE_NAME = lib.getconfig('HEAVYNODE_COOKIE_NAME')
COOKIE_VALUE = lib.getconfig('HEAVYNODE_COOKIE_VALUE')
logging.basicConfig()
intents = discord.Intents.default()
intents.members = True
bot = lib.MineBot(command_prefix='!', intents=intents)
hn = heavynode.Client(HEAVYNODE_TOKEN, COOKIE_NAME, COOKIE_VALUE)
bot.add_cleanup(hn.shutdown)
async def is_admin(ctx):
user = ctx.message.author
member = bot.guilds[0].get_member(user.id)
if member is not None:
for role in member.roles:
if role.name == 'Admin' or role.name == 'Mod':
return True
return False
async def whitelist_cmd(action, player):
expr = re.compile(f'\[Server thread/INFO\]: .*({player}|player).*(whitelist(ed)?|exist)', re.IGNORECASE)
resp = await hn.cmd_with_response(f'whitelist {action} {player}', expr)
_, msg = resp.split('[Server thread/INFO]: ')
return msg
@bot.command()
@commands.check(is_admin)
async def add(ctx, player):
"""Add a player to the server whitelist. Must use exact Minecraft name."""
msg = await whitelist_cmd('add', player)
await ctx.send(msg)
@bot.command()
@commands.check(is_admin)
async def remove(ctx, player):
"""Remove a player from the server whitelist. Must use exact Minecraft name."""
msg = await whitelist_cmd('remove', player)
await ctx.send(msg + '.')
@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.')
elif isinstance(error.original, (heavynode.HttpError, heavynode.GenericError)):
await ctx.send('Failed to communicate with server.')
elif isinstance(error.original, asyncio.exceptions.TimeoutError):
await ctx.send('Command sent; response inconclusive. Check server output for more information.')
elif isinstance(error.original, ValueError):
await ctx.send('Command sent; could not interpret server response.')
else:
raise error
bot.run(DISCORD_TOKEN, reconnect=True)