dragon-bot/app/dragon-bot.py

161 lines
6.1 KiB
Python

import random
import sys
import requests
import os
import decide
import define_word
import discord
import docker
import eight_ball
import excuse
import help_methods
import lewds
import wallpaper
# 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():
print("\n********************************")
print("\nDRAGON BOT RUNNING IN {} MODE".format(dragon_environment.upper()))
print("\n********************************")
await client.change_presence(game=discord.Game(name='Type !help to see how to use me'))
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")
@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
##### Looks like discord supports mentioning the bot.
#### Need to think of something to do here
if client.user.mentioned_in(message):
print('fuck u')
if message.content.startswith('!8ball'):
await client.send_message(
message.channel,
eight_ball.check_8ball(message.content)
)
if message.content.startswith('!cleanup') and message.author.id == '144986109804412928':
def is_bot(m):
return m.author == client.user
await client.purge_from(message.channel, limit=100, 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,
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:
# If they dont enter a number, show them an error and return out of the function
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('!wallpaper'):
await client.send_message(
message.channel,
wallpaper.get_wall(message.content)
)
###################################
###### +-------------------+ ######
###### | | ######
###### | Docker Functions | ######
###### | | ######
###### +-------------------+ ######
###################################
if message.content.startswith('!docker') and (message.author != client.user):
# Check permissions
roles = []
allowed_roles = ['MOD', 'Greasemonkey', 'Adminimodistrator']
for role in message.author.roles:
roles.append(role.name)
docker_privleges = not set(roles).isdisjoint(allowed_roles)
if not docker_privleges:
await client.send_message(message.channel, "Sorry {}, You dont have permission to run docker commands".format(message.author.mention))
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, "{}, Are you sure you want to 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, "{} 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])