2017-02-21 04:27:12 +00:00
#!/usr/bin/env python3.4
import discord
import asyncio
import configparser
2017-02-22 02:47:03 +00:00
import datetime
2017-02-25 05:00:47 +00:00
import random
2017-02-27 02:00:09 +00:00
import xkcd
2017-03-01 04:08:35 +00:00
import praw
2017-03-20 23:01:04 +00:00
import os
2017-05-25 00:13:36 +00:00
import requests
import json
2017-10-22 20:46:29 +00:00
import re
2017-03-20 23:01:04 +00:00
#Get script location
cwd = os . path . dirname ( os . path . realpath ( __file__ ) )
2017-02-21 04:27:12 +00:00
#Import config file
config = configparser . ConfigParser ( )
2017-03-20 23:01:04 +00:00
config . read ( cwd + " /config.cfg " )
2017-02-21 04:27:12 +00:00
token = config . get ( " configuration " , " token " )
2017-02-21 05:21:00 +00:00
bot_channel_id = discord . Object ( id = config . get ( " configuration " , " bot_channel_id " ) )
2017-03-19 06:18:50 +00:00
public_channel_id = discord . Object ( id = config . get ( " configuration " , " public_channel_id " ) )
2017-03-02 00:18:02 +00:00
owner_id = discord . User ( id = config . get ( " configuration " , " owner_id " ) )
2017-02-21 04:27:12 +00:00
2017-03-01 04:08:35 +00:00
#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 )
2017-05-25 00:13:36 +00:00
#Icecast stream info setup
metadataurl = config . get ( " configuration " , " metadataurl " )
iceurl = config . get ( " configuration " , " iceurl " )
2017-10-22 20:46:29 +00:00
#Dice Rolling Function
def rolldie ( x ) :
sides = int ( x [ 1 : ] )
roll = random . randint ( 1 , sides )
return roll
2017-02-21 04:27:12 +00:00
client = discord . Client ( )
2017-02-25 01:22:55 +00:00
#Subroutine to get UPS Status
@asyncio.coroutine
def ups_check ( ) :
while not client . is_closed :
2017-03-20 23:01:04 +00:00
yield from asyncio . create_subprocess_exec ( " /bin/bash " , cwd + " /ups_status.sh " )
2017-02-25 01:22:55 +00:00
yield from asyncio . sleep ( 60 )
2017-02-21 04:27:12 +00:00
@client.event
@asyncio.coroutine
def on_ready ( ) :
print ( ' Logged in as ' )
print ( client . user . name )
print ( client . user . id )
print ( ' ------ ' )
2017-02-21 05:21:00 +00:00
yield from client . send_message ( bot_channel_id , " This isn ' t Tatooine... " )
2017-08-12 01:04:58 +00:00
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 ' ) )
2017-02-21 04:27:12 +00:00
@client.event
@asyncio.coroutine
def on_message ( message ) :
2017-02-27 02:31:15 +00:00
2017-03-20 22:36:36 +00:00
#Ignore messages from self
if message . author == client . user :
return
2017-02-27 02:31:15 +00:00
#Maki Monday praise command
2017-02-27 02:24:40 +00:00
if ( message . content . lower ( ) . startswith ( ' !maki ' ) ) or ( message . content . lower ( ) . startswith ( ' !monday ' ) ) :
2017-09-12 14:24:55 +00:00
yield from client . send_message ( message . channel , ' https://maki.jawa.moe/monday.png ' )
2017-02-27 02:31:15 +00:00
#Posts help info
2017-02-22 04:15:15 +00:00
elif message . content . lower ( ) . startswith ( ' !help ' ) :
2018-03-03 02:48:40 +00:00
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 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> ' )
2017-02-27 02:31:15 +00:00
#Sleeps for 5 seconds. Don't know why I still have this tbh
2017-02-21 05:33:11 +00:00
elif message . content . lower ( ) . startswith ( ' !sleep ' ) :
2017-02-21 04:27:12 +00:00
yield from asyncio . sleep ( 5 )
yield from client . send_message ( message . channel , ' Done sleeping ' )
2017-02-27 02:31:15 +00:00
#Posts link to my mix archive
2017-02-21 05:33:11 +00:00
elif message . content . lower ( ) . startswith ( ' !mixes ' ) :
2017-09-12 14:24:55 +00:00
yield from client . send_message ( message . channel , ' You can download mp3s of any of my mixes at https://maki.jawa.moe/mixes/ ' )
2017-02-27 02:31:15 +00:00
#Pokes the person who said the command
2017-02-21 14:28:18 +00:00
elif message . content . lower ( ) . startswith ( ' !pokeme ' ) :
yield from client . send_message ( message . channel , " _pokes <@ " + message . author . id + " >_ " )
2017-02-27 02:31:15 +00:00
#Dump the navy seal copypasta
2017-02-21 14:28:18 +00:00
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? I’ ll have you know I graduated top of my class in the Navy Seals, and I’ ve been involved in numerous secret raids on Al-Quaeda, and I have over 300 confirmed kills. I am trained in gorilla warfare and I’ m 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. You’ re fucking dead, kid. I can be anywhere, anytime, and I can kill you in over seven hundred ways, and that’ s 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 couldn’ t, you didn’ t, and now you’ re paying the price, you goddamn idiot. I will shit fury all over you and you will drown in it. You’ re fucking dead, kiddo. ' )
2017-02-27 02:31:15 +00:00
#Countdown until the Switch comes out!
2017-02-22 02:47:03 +00:00
elif message . content . lower ( ) . startswith ( ' !switch ' ) :
now = datetime . datetime . now ( )
switchdate = datetime . datetime ( 2017 , 3 , 3 )
switchdelta = switchdate - now
2017-02-27 02:44:57 +00:00
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> "
2017-02-22 02:47:03 +00:00
yield from client . send_message ( message . channel , switchmsg )
2017-02-27 02:31:15 +00:00
#Posts status of the UPS
2017-02-25 01:22:55 +00:00
elif message . content . lower ( ) . startswith ( ' !ups ' ) :
ups_status = open ( " /tmp/ups_status.txt " , " r " )
yield from client . send_message ( message . channel , ups_status . read ( ) )
2017-02-27 02:31:15 +00:00
#Posts random picture of Tohru
2017-02-25 05:00:47 +00:00
elif message . content . lower ( ) . startswith ( ' !tohru ' ) :
2017-03-20 23:01:04 +00:00
tohru_list_file = open ( cwd + " /lists/tohru_images.txt " , " r " )
2017-02-25 05:00:47 +00:00
tohru_pics = tohru_list_file . read ( ) . split ( )
yield from client . send_message ( message . channel , random . choice ( tohru_pics ) )
2017-02-27 02:31:15 +00:00
#Sets the bot's Now Playing status
2017-02-26 19:54:22 +00:00
elif message . content . lower ( ) . startswith ( ' !setplaying ' ) :
2017-08-12 01:04:58 +00:00
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 ' ) )
2017-02-21 14:28:18 +00:00
2017-02-27 02:00:09 +00:00
#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. ' )
2017-03-01 04:16:44 +00:00
#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 )
2017-03-01 05:01:46 +00:00
#Link some porn! (actually SFW)
elif message . content . lower ( ) . startswith ( ' !porn ' ) :
2017-10-22 20:49:12 +00:00
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 )
2017-03-01 05:01:46 +00:00
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 )
2017-03-02 00:26:29 +00:00
#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! ' )
2017-03-02 16:48:42 +00:00
#Random jokes from /r/jokes
elif message . content . lower ( ) . startswith ( ' !joke ' ) :
2017-10-22 20:50:05 +00:00
jokes = reddit . subreddit ( ' jokes ' ) . top ( ' month ' )
2017-03-02 16:48:42 +00:00
jokesr = random . choice ( list ( jokes ) )
jokemsg = jokesr . title + ' \n \n ' + jokesr . selftext
yield from client . send_message ( message . channel , jokemsg )
2017-03-02 17:21:56 +00:00
#Magic 8-ball simulator 2017
elif message . content . lower ( ) . startswith ( ' !8ball ' ) :
2017-03-20 23:01:04 +00:00
ball_response_file = open ( cwd + " /lists/8ball_responses.txt " , " r " )
2017-03-02 17:21:56 +00:00
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 ' ) :
2017-03-20 23:01:04 +00:00
ball_response_file = open ( cwd + " /lists/8ball_responses.txt " , " r " )
2017-03-02 17:21:56 +00:00
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 ) )
2017-03-19 06:47:09 +00:00
#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 )
2017-10-22 20:47:30 +00:00
#Congratulate and message the winner
2017-03-19 06:47:09 +00:00
yield from client . send_message ( public_channel_id , ' Congrats to {0.user} ! ' . format ( res ) )
yield from client . send_message ( res . user , args )
2017-05-25 00:13:36 +00:00
#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 + = " \n Download/Buy: {} " . format ( npobject [ ' WOAR ' ] )
output + = " \n Listeners: {} " . 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 )
2017-03-19 06:47:09 +00:00
2017-05-25 04:55:33 +00:00
#Post a random mix
elif message . content . lower ( ) . startswith ( ' !rmix ' ) :
2017-09-12 14:24:55 +00:00
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 ' ] )
2017-05-25 04:55:33 +00:00
2017-10-22 20:46:29 +00:00
#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 )
2017-05-25 04:55:33 +00:00
2017-02-21 04:27:12 +00:00
2017-02-25 01:22:55 +00:00
client . loop . create_task ( ups_check ( ) )
2017-08-02 15:31:34 +00:00
client . run ( token )