""" dragon-bot Our discord bot. Many of its actions are handled by separate modules which are then imported into the main bot """ import os import requests import avatar import decide import define_word import discord import docker import eight_ball import excuse import get_from_reddit import help_methods import lewds import role_check import wallpaper import wolfram # Client object client = discord.Client() tokens = { 'test': 'MzQ1MjkwMTI5OTQ4Mjc4Nzg0.DG5IBw._9umb82PrL22bPe7GjmHClU-NtU', 'prod': 'MzM5NDQ2NTgwMTU3MzQ5ODg4.DG5K5Q.2kIonA_XHLXU4_Sq4O63OzCb0Jc' } dragon_environment = os.getenv('DRAGON_ENV') debug = dragon_environment == 'test' @client.event async def on_ready(): # Update the bot's status await client.change_presence( game=discord.Game(name='Type !help to see what I can do') ) if not debug: await client.edit_profile(avatar=avatar.change_avatar()) print("\n********************************") print("\nDRAGON BOT RUNNING IN {} MODE".format(dragon_environment.upper())) print("\n********************************") if debug: print("\nPress control+c to exit the bot") print("Followed by control+d or by typing") print("'exit' to exit the docker container\n\n") @client.event async def on_message(message): def is_me(m): """ is_me(m) Takes a message as an argument and checks that the author of the message is the same as the person who initiated the command """ return m.author == message.author if client.user.mentioned_in(message): await client.send_message( message.channel, "{} ```{}```".format( message.author.mention, wolfram.answer_question(message.content) ) ) if message.content.startswith('!8ball'): await client.send_message( message.channel, eight_ball.check_8ball(message.content) ) if message.content.startswith('!cleanup'): if not role_check.cleanup_permissions(message.author.roles): await client.send_message(message.channel, 'You cant do that') return def is_bot(m): return m.author == client.user await client.purge_from(message.channel, limit=10, check=is_bot) if message.content.startswith('!decide'): await client.send_message( message.channel, "{} {}".format( message.author.mention, decide.decide(message.content) ) ) if message.content.startswith('!define'): await client.send_message( message.channel, define_word.get_definition(message.content) ) if message.content.startswith('!excuse'): await client.send_message(message.channel, excuse.get_excuse()) if message.content.startswith('!help'): await client.send_message(message.channel, "ok {}, check your private messages".format(message.author.mention)) await client.send_message( message.author, help_methods.parse_message(message.content) ) if message.content.startswith('!lewd'): await client.send_message( message.channel, lewds.get_lewd(channel_name=message.channel.name) ) if message.content.startswith('!purge'): num = 20 if len(message.content.split()) > 1: try: num = int(message.content.split()[1]) except ValueError: await client.send_message( message.channel, "You need to give me a number, you entered {}".format( message.content.split()[1] ) ) return await client.purge_from(message.channel, limit=num, check=is_me) if message.content.startswith('!roles'): await client.send_message( message.channel, "ok {}. Check your PMs".format(message.author.mention) ) await client.send_message( message.author, "```{}```".format(' '.join(list(map(lambda x: x.name, message.author.roles)))) ) if message.content.startswith('!wallpaper'): await client.send_message( message.channel, wallpaper.get_wall(message.content) ) if message.content.startswith('!homepage'): await client.send_message( message.channel, wallpaper.get_picture(message.content) ) if message.content.startswith('!docker'): # Check permissions if not role_check.docker_permissions(message.author.roles): await client.send_message( message.channel, "You dont have permission to run docker commands" ) return if len(message.content.split()) == 1: actions = ['restart', 'status', 'logs'] await client.send_message( message.channel, "\nSupported actions:\n{}".format(", ".join(actions)) ) else: docker_client = docker.from_env() minecraft_container = docker_client.containers.get('skyfactory') # Figure out what action they want to take action = message.content.split()[1] if action == 'restart': await client.send_message( message.channel, "{}, restart the container? [!yes/!no]".format( message.author.mention) ) confirm_restart = await client.wait_for_message( author=message.author, channel=message.channel, content='!yes' ) if confirm_restart: await client.send_message( message.channel, "Sending restart action to {} container".format( minecraft_container.name ) ) minecraft_container.restart() if action == 'status': await client.send_message( message.channel, "{} container is {}".format( minecraft_container.name, minecraft_container.status ) ) if action == 'logs': if len(message.content.split()) == 3: num_lines = int(message.content.split()[2]) else: num_lines = 10 log_stream = minecraft_container.logs( tail=num_lines ).decode('utf-8') if len(log_stream) >= num_lines: await client.send_message( message.channel, "Pulling last {} lines from {} container".format( num_lines, minecraft_container.name ) ) await client.send_message( message.channel, "```{}```".format( minecraft_container.logs( tail=num_lines ).decode('utf-8') ) ) else: await client.send_message( message.channel, "There arent {} lines of output yet".format(num_lines) ) client.run(tokens[dragon_environment])