minebot/bot.py

80 lines
2.3 KiB
Python
Raw Normal View History

2020-10-12 05:03:50 +00:00
import asyncio
2020-05-09 17:45:07 +00:00
import inspect
2020-05-13 06:16:35 +00:00
import itertools
2020-05-09 04:39:28 +00:00
import logging
import os
2020-10-12 05:03:50 +00:00
import re
2020-05-09 04:39:28 +00:00
2020-05-13 06:16:35 +00:00
import discord
2020-05-09 04:39:28 +00:00
from discord.ext import commands
import lib
2020-05-09 04:39:28 +00:00
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')
2020-05-09 05:58:27 +00:00
2020-05-09 04:39:28 +00:00
logging.basicConfig()
2020-05-09 05:58:27 +00:00
2020-10-12 05:03:50 +00:00
intents = discord.Intents.default()
intents.members = True
bot = lib.MineBot(command_prefix='!', intents=intents)
hn = heavynode.Client(HEAVYNODE_TOKEN, COOKIE_NAME, COOKIE_VALUE)
2020-10-12 05:03:50 +00:00
bot.add_cleanup(hn.shutdown)
2020-05-09 04:39:28 +00:00
2020-05-13 06:16:35 +00:00
async def is_admin(ctx):
user = ctx.message.author
2020-12-26 02:57:45 +00:00
member = bot.guilds[0].get_member(user.id)
2020-05-13 06:16:35 +00:00
if member is not None:
for role in member.roles:
if role.name == 'Admin' or role.name == 'Mod':
return True
return False
2020-10-12 05:03:50 +00:00
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
2020-05-09 04:39:28 +00:00
@bot.command()
2020-05-13 06:16:35 +00:00
@commands.check(is_admin)
2020-05-09 04:39:28 +00:00
async def add(ctx, player):
2020-05-09 06:32:30 +00:00
"""Add a player to the server whitelist. Must use exact Minecraft name."""
2020-10-12 05:03:50 +00:00
msg = await whitelist_cmd('add', player)
await ctx.send(msg)
2020-05-09 04:39:28 +00:00
@bot.command()
2020-05-13 06:16:35 +00:00
@commands.check(is_admin)
2020-05-09 04:39:28 +00:00
async def remove(ctx, player):
"""Remove a player from the server whitelist. Must use exact Minecraft name."""
2020-10-12 05:03:50 +00:00
msg = await whitelist_cmd('remove', player)
await ctx.send(msg + '.')
2020-05-09 04:39:28 +00:00
@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.')
2020-10-12 05:03:50 +00:00
elif isinstance(error.original, (heavynode.HttpError, heavynode.GenericError)):
await ctx.send('Failed to communicate with server.')
2020-10-12 05:03:50 +00:00
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.')
2020-05-09 04:39:28 +00:00
else:
raise error
bot.run(DISCORD_TOKEN, reconnect=True)