minebot/bot.py
2020-05-12 23:16:35 -07:00

64 lines
1.6 KiB
Python

import inspect
import itertools
import logging
import os
import discord
from discord.ext import commands
import lib
import heavynode
DISCORD_TOKEN = os.environ['discord_token']
DISCORD_SERVER_ID = 530446700058509323
HEAVYNODE_TOKEN = os.environ['heavynode_token']
logging.basicConfig()
bot = lib.MineBot(command_prefix='!')
hn = heavynode.Client(HEAVYNODE_TOKEN)
bot.add_cleanup(hn.shutdown)
async def is_admin(ctx):
user = ctx.message.author
guild = discord.utils.get(bot.guilds, id=DISCORD_SERVER_ID)
member = discord.utils.get(guild.members, id=user.id)
if member is not None:
for role in member.roles:
if role.name == 'Admin' or role.name == 'Mod':
return True
return False
@bot.command()
@commands.check(is_admin)
async def add(ctx, player):
"""Add a player to the server whitelist. Must use exact Minecraft name."""
await hn.send_command(f'whitelist add {player}')
await ctx.send(f'"{player}" added to whitelist.')
@bot.command()
@commands.check(is_admin)
async def remove(ctx, player):
"""Remove a player from the server whitelist. Must use exact Minecraft name."""
await hn.send_command(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.')
elif isinstance(error, heavynode.HttpError):
await ctx.send('Failed to communicate with server.')
else:
raise error
bot.run(DISCORD_TOKEN, reconnect=True)