minebot/bot.py

64 lines
1.6 KiB
Python
Raw Normal View History

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-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 = os.environ['discord_token']
2020-05-13 06:16:35 +00:00
DISCORD_SERVER_ID = 530446700058509323
HEAVYNODE_TOKEN = os.environ['heavynode_token']
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
bot = lib.MineBot(command_prefix='!')
hn = heavynode.Client(HEAVYNODE_TOKEN)
2020-05-09 17:45:07 +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
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
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."""
await hn.send_command(f'whitelist add {player}')
2020-05-09 04:39:28 +00:00
await ctx.send(f'"{player}" added to whitelist.')
@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."""
await hn.send_command(f'whitelist remove {player}')
2020-05-09 04:39:28 +00:00
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.')
2020-05-09 04:39:28 +00:00
else:
raise error
bot.run(DISCORD_TOKEN, reconnect=True)