discord-pocketbot/pocketbot.py

374 lines
19 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3.4
import discord
import asyncio
import configparser
import datetime
import random
import xkcd
import praw
import os
import requests
import json
import re
import telnetlib
#Get script location
cwd = os.path.dirname(os.path.realpath(__file__))
#Import config file
config = configparser.ConfigParser()
config.read(cwd + "/config.cfg")
token = config.get("configuration", "token")
bot_channel_id = discord.Object(id=config.get("configuration", "bot_channel_id"))
public_channel_id = discord.Object(id=config.get("configuration", "public_channel_id"))
owner_id = discord.User(id=config.get("configuration", "owner_id"))
#Reddit Setup
reddit_client_id = config.get("configuration", "reddit_client_id")
reddit_client_secret = config.get("configuration", "reddit_client_secret")
reddit_user_agent = 'linux:com.pocketjawa.discord-pocketbot:v0.1 (by /u/pocketjava)'
reddit = praw.Reddit(client_id=reddit_client_id,client_secret=reddit_client_secret,user_agent=reddit_user_agent)
#Icecast stream info setup
metadataurl = config.get("configuration", "metadataurl")
iceurl = config.get("configuration", "iceurl")
#Dice Rolling Function
def rolldie(x):
sides = int(x[1:])
roll = random.randint(1,sides)
return roll
client = discord.Client()
#Subroutine to get UPS Status
@asyncio.coroutine
def ups_check():
yield from client.wait_until_ready()
while not client.is_closed:
yield from asyncio.create_subprocess_exec("/bin/bash", cwd + "/ups_status.sh")
yield from asyncio.sleep(5)
#Make temp status file if it doesn't exist
if not os.path.isfile(cwd + "/tmp/ups_status.txt"):
ups_temp = open(cwd + "/tmp/ups_status.txt", "w")
ups_temp.write("ONLINE")
ups_temp.close()
#Message and set status if on battery
if 'ONBATT' in open('/tmp/ups_status.txt').read() and 'ONLINE' in open(cwd + '/tmp/ups_status.txt').read():
yield from client.send_message(bot_channel_id, "<@"+owner_id.id+"> UPS is on battery!")
ups_status = open("/tmp/ups_status.txt","r")
yield from client.send_message(bot_channel_id, ups_status.read())
ups_temp = open(cwd + "/tmp/ups_status.txt", "w")
ups_temp.write("ONBATT")
ups_temp.close()
#Message and set status back to normal when power comes back on
elif 'ONLINE' in open('/tmp/ups_status.txt').read() and 'ONBATT' in open(cwd + '/tmp/ups_status.txt').read():
yield from client.send_message(bot_channel_id, "<@"+owner_id.id+"> UPS is back online!")
ups_status = open("/tmp/ups_status.txt","r")
yield from client.send_message(bot_channel_id, ups_status.read())
ups_temp = open(cwd + "/tmp/ups_status.txt", "w")
ups_temp.write("ONLINE")
ups_temp.close()
yield from asyncio.sleep(55)
@client.event
@asyncio.coroutine
def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
yield from client.send_message(bot_channel_id, "This isn't Tatooine...")
if os.path.isfile(cwd + "/tmp/status.txt"):
status_file = open(cwd + "/tmp/status.txt", "r")
yield from client.change_presence(game=discord.Game(name=status_file.read()))
else:
yield from client.change_presence(game=discord.Game(name='with some droids'))
@client.event
@asyncio.coroutine
def on_message(message):
#Ignore messages from self
if message.author == client.user:
return
#Maki Monday praise command
if (message.content.lower().startswith('!maki')) or (message.content.lower().startswith('!monday')):
yield from client.send_message(message.channel, 'https://maki.jawa.moe/monday.png')
#Posts help info
elif message.content.lower().startswith('!help'):
args = message.content.partition(' ')[2]
command = args.partition(' ')[0]
if (command is '' ):
helpfile = open(cwd + "/helpcommand.txt","r")
yield from client.send_message(message.channel, helpfile.read())
elif re.match("^[A-Za-z0-9_-]*$", command) and os.path.isfile(cwd + "/helptext/" + command):
commandhelp = open(cwd + "/helptext/" + command,"r")
yield from client.send_message(message.channel, commandhelp.read())
else:
yield from client.send_message(message.channel, 'Either that command does not exist, or it is too simple to deserve extra context <:tohrusmug:292110413129318401>')
#Sleeps for 5 seconds. Don't know why I still have this tbh
elif message.content.lower().startswith('!sleep'):
output = "Aren't you tired?\nLet's call it a day\nand get some sleep."
sleepemb = discord.Embed(description=output)
sleepemb.set_author(name="Morgana")
sleepemb.set_thumbnail(url="https://cdn.discordapp.com/emojis/396429379686629378.png")
yield from client.send_message(message.channel, embed=sleepemb)
yield from asyncio.sleep(5)
yield from client.send_message(message.channel, 'Ok, done sleeping')
#Posts link to my mix archive
elif message.content.lower().startswith('!mixes'):
yield from client.send_message(message.channel, 'You can download mp3s of any of my mixes at https://maki.jawa.moe/mixes/')
#Pokes the person who said the command
elif message.content.lower().startswith('!pokeme'):
yield from client.send_message(message.channel, "_pokes <@"+message.author.id+">_")
#Dump the navy seal copypasta
elif message.content.lower().startswith('!wtf'):
yield from client.send_typing(message.channel)
yield from asyncio.sleep(3)
yield from client.send_message(message.channel, 'What the fuck did you just fucking say about me, you little bitch? Ill have you know I graduated top of my class in the Navy Seals, and Ive been involved in numerous secret raids on Al-Quaeda, and I have over 300 confirmed kills. I am trained in gorilla warfare and Im the top sniper in the entire US armed forces. You are nothing to me but just another target. I will wipe you the fuck out with precision the likes of which has never been seen before on this Earth, mark my fucking words. You think you can get away with saying that shit to me over the Internet? Think again, fucker. As we speak I am contacting my secret network of spies across the USA and your IP is being traced right now so you better prepare for the storm, maggot. The storm that wipes out the pathetic little thing you call your life. Youre fucking dead, kid. I can be anywhere, anytime, and I can kill you in over seven hundred ways, and thats just with my bare hands. Not only am I extensively trained in unarmed combat, but I have access to the entire arsenal of the United States Marine Corps and I will use it to its full extent to wipe your miserable ass off the face of the continent, you little shit. If only you could have known what unholy retribution your little “clever” comment was about to bring down upon you, maybe you would have held your fucking tongue. But you couldnt, you didnt, and now youre paying the price, you goddamn idiot. I will shit fury all over you and you will drown in it. Youre fucking dead, kiddo.')
#Countdown until the Switch comes out!
elif message.content.lower().startswith('!switch'):
now = datetime.datetime.now()
switchdate = datetime.datetime(2017, 3, 3)
switchdelta = switchdate - now
if (switchdelta.total_seconds() > 0):
switchmsg = "<:switchl:283809380137435139>Only **%s** until the Switch comes out!<:switchr:283809391835349004>" % (switchdelta)
else:
switchmsg = "The <:switchl:283809380137435139>Switch<:switchr:283809391835349004> is already out!!! <:pjchamp:231619017599746048>"
yield from client.send_message(message.channel, switchmsg)
#Posts status of the UPS
elif message.content.lower().startswith('!ups'):
ups_status = open("/tmp/ups_status.txt","r")
yield from client.send_message(message.channel, ups_status.read())
#Posts random picture of Tohru
elif message.content.lower().startswith('!tohru'):
tohru_list_file = open(cwd + "/lists/tohru_images.txt", "r")
tohru_pics = tohru_list_file.read().split()
yield from client.send_message(message.channel, random.choice(tohru_pics))
#Sets the bot's Now Playing status
elif message.content.lower().startswith('!setplaying'):
args = message.content.partition(' ')[2]
yield from client.change_presence(game=discord.Game(name=args))
conf_file = open(cwd + "/tmp/status.txt", "w")
conf_file.write(args)
conf_file.close()
#Reset bot's Now Playing status
elif message.content.lower().startswith('!resetplaying'):
if os.path.isfile(cwd + "/tmp/status.txt"):
os.remove(cwd + "/tmp/status.txt")
yield from client.change_presence(game=discord.Game(name='with some droids'))
#xkcd comic fetcher
elif message.content.lower().startswith('!xkcd'):
args = message.content.partition(' ')[2]
if (args is ''):
comic = xkcd.getRandomComic()
comicnum = str (comic.number)
xkmessage = 'xkcd #' + comicnum + ': ' + comic.getTitle() + '\n' + comic.getImageLink() + '\n' + 'Alt Text: '+comic.getAltText()
yield from client.send_message(message.channel, xkmessage)
else:
if ('latest' in args):
comic = xkcd.getLatestComic()
comicnum = str (comic.number)
xkmessage = 'The most recent xkcd is #' + comicnum + ': ' + comic.getTitle() + '\n' + comic.getImageLink() + '\n' + 'Alt Text: '+comic.getAltText()
yield from client.send_message(message.channel, xkmessage)
elif (args.isdigit()):
comic = xkcd.getComic(args)
if (comic.number == -1):
yield from client.send_message(message.channel, 'There is no xkcd comic #' + args + '. Please try again.')
else:
comicnum = str (comic.number)
xkmessage = 'xkcd is #' + comicnum + ': ' + comic.getTitle() + '\n' + comic.getImageLink() + '\n' + 'Alt Text: '+comic.getAltText()
yield from client.send_message(message.channel, xkmessage)
else:
yield from client.send_message(message.channel, '`'+args+'`' + 'is not a valid option. Please leave blank for a random comic, say "latest" for the latest comic or put the number of the comic you want.' )
#Post random submisison from /r/EarthPorn's top this week
elif (message.content.lower().startswith('!earth')) or (message.content.lower().startswith('!earthporn')):
earthporn = reddit.subreddit('earthporn').top('week')
earthpornr = list(earthporn)
yield from client.send_message(message.channel, random.choice(earthpornr).url)
#Link some porn! (actually SFW)
elif message.content.lower().startswith('!porn'):
earthporn = reddit.subreddit('earthporn').hot(limit=10)
serverporn = reddit.subreddit('ServerPorn').hot(limit=10)
cableporn = reddit.subreddit('cableporn').hot(limit=10)
foodporn = reddit.subreddit('FoodPorn').hot(limit=10)
shittyfoodporn = reddit.subreddit('shittyfoodporn').hot(limit=10)
porn_multi_r = list(earthporn) + list(serverporn) + list(cableporn) + list(foodporn) + list(shittyfoodporn)
yield from client.send_message(message.channel, random.choice(porn_multi_r).url)
#Kill the bot so it can be reloaded
elif (message.content.lower().startswith('!kill')) or (message.content.lower().startswith('!restart')):
author_id = message.author
if author_id == owner_id:
yield from client.send_message(message.channel, 'Goodbye Onii-chan!!!')
yield from client.close()
else:
yield from client.send_message(message.channel, 'Booooo you\'re not Onii-chan. You\'re not allowed to use this command!')
#Random jokes from /r/jokes
elif message.content.lower().startswith('!joke'):
jokes = reddit.subreddit('jokes').top('month')
jokesr = random.choice(list(jokes))
jokemsg = jokesr.title + '\n\n' + jokesr.selftext
yield from client.send_message(message.channel, jokemsg)
#Magic 8-ball simulator 2017
elif message.content.lower().startswith('!8ball'):
ball_response_file = open(cwd + "/lists/8ball_responses.txt", "r")
ball_responses = ball_response_file.read().split('\n')
args = message.content.partition(' ')[2]
if (args is ''):
yield from client.send_message(message.channel, 'Please include a question for me to answer. I\'m not _that_ psychic...')
else:
yield from client.send_message(message.channel, 'The Magic 8-ball says... ' + random.choice(ball_responses))
#Magic Conch Shell simulator 2002 (way cooler than that lame "Magic" 8-ball)
elif message.content.lower().startswith('!conch') or message.content.lower().startswith('!magicconch'):
ball_response_file = open(cwd + "/lists/8ball_responses.txt", "r")
ball_responses = ball_response_file.read().split('\n')
args = message.content.partition(' ')[2]
if (args is ''):
yield from client.send_message(message.channel, 'Please include a question for me to answer. I\'m not _that_ psychic...')
else:
yield from client.send_message(message.channel, 'The Magic Conch Shell says... ' + random.choice(ball_responses))
#Create a message that will send the person who reacts correctly the contents of message. Use in either DMs or a channel the public cannot see.
elif message.content.lower().startswith('!giveaway'):
#Split off the text to send to the winner
args = message.content.partition(' ')[2]
if (args is ''):
yield from client.send_message(message.channel, 'Please include something to send to the winner!')
return
#Get the secret emoji
sourcemsg = yield from client.send_message(message.channel, 'React with the secret emoji :3')
sourceres = yield from client.wait_for_reaction(message=sourcemsg, timeout=30)
reactemoji = sourceres.reaction.emoji
#Send the poll to public
msg = yield from client.send_message(public_channel_id, 'React to this pls')
res = yield from client.wait_for_reaction(reactemoji, message=msg, timeout=30)
#Congratulate and message the winner
yield from client.send_message(public_channel_id, 'Congrats to {0.user}!'.format(res))
yield from client.send_message(res.user, args)
#Get currently playing stats from toukufm.com
elif message.content.lower().startswith('!np'):
#Load JSON from stream APIs
nprequest = requests.get(metadataurl)
npobject = json.loads(nprequest.text)
icerequest = requests.get(iceurl)
iceobject = json.loads(icerequest.text)
#Add up listeners
listenersum = int(0)
for stream in iceobject['icestats']['source']:
listenersum += stream["listeners"]
#Put together the message
output = ""
if 'comment' in npobject:
output += "[{}]".format(npobject['comment'])
if 'artist' in npobject:
output += "\n{} - ".format(npobject['artist'])
if 'title' in npobject:
if 'artist' in npobject:
output += "{} ".format(npobject['title'])
else:
output += "\n{} ".format(npobject['title'])
if 'WOAR' in npobject:
output += "\nDownload/Buy: {}".format(npobject['WOAR'])
output += "\nListeners: {} ".format(listenersum)
#Set embed and send the message
npem = discord.Embed(title="Now Playing on ToukuFM", description=output, url="https://toukufm.com/")
yield from client.send_message(message.channel, embed=npem)
#Post a random mix
elif message.content.lower().startswith('!rmix'):
mixes = json.loads(requests.get("https://maki.jawa.moe/mixesjson/").text)
yield from client.send_message(message.channel, "https://maki.jawa.moe/mixes/" + random.choice(mixes)['name'])
#Dice Roller (support rolling multiple dice at once)
elif message.content.lower().startswith('!roll'):
#Set starting Variables
args = message.content.partition(' ')[2].upper()
argsplit = args.split()
output = message.author.display_name + " rolled "
rolltotal = 0
#Go through each requested roll
for arg in argsplit:
#Roll once die multiple times (eg 20d20)
if re.match('^[0-9]+D[0-9]+$', arg):
dlocation = arg.index('D')
rolls = int(arg[:dlocation])
arg = arg[dlocation:]
if rolls <= 25:
while rolls > 0:
rollresult = rolldie(arg)
output = output + arg + ": **" + str(rollresult) + "**, "
rolltotal += rollresult
rolls -= 1
else:
rollminitotal = 0
output = output + str(rolls) + '\*' + arg + ": **"
while rolls > 0:
rollresult = rolldie(arg)
rollminitotal += rollresult
rolls -= 1
output = output + str(rollminitotal) + "**, "
rolltotal += rollminitotal
#Roll a single die (eg d20)
elif re.match('^D[0-9]+$', arg):
rollresult = rolldie(arg)
output = output + arg + ": **" + str(rollresult) + "**, "
rolltotal += rollresult
#Finish generating message
if rolltotal > 0:
output = output + "for a total of **" + str(rolltotal) + "**!"
#Print error if no dice were rolled (aka if the total is still zero)
else:
output = "Oops, looks like no dice were rolled! Try something like `!roll d20`, `!roll 2d6`, or `!roll 3d6 d20` instead."
#Send output to chat
yield from client.send_message(message.channel, output)
#Skip tracks/mixes on JawaFM
elif message.content.lower().startswith('!fmskip'):
args = message.content.partition(' ')[2]
station = args.partition(' ')[0]
if (station is ''):
yield from client.send_message(message.channel, 'Please provide a station to skip.')
else:
skipcommand = station + '(dot)ogg.skip\n'
tn = telnetlib.Telnet("localhost", 1234)
tn.write(skipcommand.encode('ascii'))
tn.close()
client.loop.create_task(ups_check())
client.run(token)