2016-03-30 16:28:59 -04:00
|
|
|
# Maki
|
|
|
|
# ----
|
|
|
|
# Discord bot by MrDetonia
|
|
|
|
#
|
2017-04-22 11:03:11 -04:00
|
|
|
# Copyright 2017 Zac Herd
|
2016-03-30 16:28:59 -04:00
|
|
|
# Licensed under BSD 3-clause License, see LICENSE.md for more info
|
|
|
|
|
2016-05-05 16:27:00 -04:00
|
|
|
|
2016-03-30 16:28:59 -04:00
|
|
|
# IMPORTS
|
|
|
|
import discord
|
|
|
|
import asyncio
|
2016-03-30 18:33:53 -04:00
|
|
|
import os
|
2016-04-29 05:20:42 -04:00
|
|
|
import io
|
2016-12-01 16:11:11 -05:00
|
|
|
import requests
|
2016-04-29 05:20:42 -04:00
|
|
|
import sys
|
2016-05-25 08:10:24 -04:00
|
|
|
import shlex
|
|
|
|
import subprocess
|
2016-03-30 16:28:59 -04:00
|
|
|
import time
|
|
|
|
import datetime
|
2016-04-03 17:19:10 -04:00
|
|
|
import random
|
2016-05-05 16:27:00 -04:00
|
|
|
import re
|
2016-03-30 18:33:53 -04:00
|
|
|
import json
|
2016-04-19 10:14:11 -04:00
|
|
|
import logging
|
|
|
|
|
2016-04-03 11:03:40 -04:00
|
|
|
import markov
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
# file in this directory called "secret.py" should contain these variables
|
2017-04-08 09:32:48 -04:00
|
|
|
from secret import token, lfmkey, steamkey
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
# CONFIGURATION
|
|
|
|
|
|
|
|
# bot version
|
2017-04-22 10:37:51 -04:00
|
|
|
version = "v0.20.1"
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
# text shown by .help command
|
2016-10-12 07:17:39 -04:00
|
|
|
helptext = """I am a Discord bot written in Python
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
My commands are:
|
|
|
|
```
|
|
|
|
.help - displays this text
|
2016-04-08 14:16:31 -04:00
|
|
|
.info - prints bot info
|
2016-04-01 07:14:50 -04:00
|
|
|
.upskirt - show a link to my source
|
2016-03-30 16:28:59 -04:00
|
|
|
.whoami - displays your user info
|
2016-04-01 09:27:00 -04:00
|
|
|
.whois <user> - displays another user's info
|
2016-03-30 16:28:59 -04:00
|
|
|
.seen <user> - prints when user was last seen
|
2016-04-01 04:52:38 -04:00
|
|
|
.say <msg> - say something
|
2016-04-08 14:15:03 -04:00
|
|
|
.sayy <msg> - say something a e s t h e t i c a l l y
|
2016-12-01 14:29:58 -05:00
|
|
|
.markov [<user>] - generate markov chain over chat history for you or another user
|
2016-05-05 16:27:00 -04:00
|
|
|
.roll <x>d<y> - roll x number of y sided dice
|
2016-05-25 08:10:24 -04:00
|
|
|
.qr <msg> - generate a QR code
|
2016-12-01 14:29:58 -05:00
|
|
|
.np [<user>] - fetch now playing from last.fm for you or a specific username
|
2017-04-08 09:32:48 -04:00
|
|
|
.steam [<user>] - fetch steam status for you or a specific vanityname
|
2016-03-30 16:28:59 -04:00
|
|
|
```"""
|
|
|
|
|
|
|
|
# IDs of admin users
|
|
|
|
admins = ['116883900688629761']
|
|
|
|
|
|
|
|
|
2016-05-05 16:27:00 -04:00
|
|
|
# GLOBALS
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
# log of users' last messages and timestamps
|
2016-04-01 09:27:00 -04:00
|
|
|
history = {}
|
2016-03-30 18:33:53 -04:00
|
|
|
if os.path.isfile('hist.json'):
|
|
|
|
with open('hist.json', 'r') as fp:
|
|
|
|
history = json.load(fp)
|
2016-03-30 16:28:59 -04:00
|
|
|
|
2016-12-02 19:55:49 -05:00
|
|
|
# quiet modes
|
|
|
|
quiet = {}
|
|
|
|
|
2016-04-01 04:52:38 -04:00
|
|
|
# this instance of the Discord client
|
2016-03-30 16:28:59 -04:00
|
|
|
client = discord.Client()
|
|
|
|
|
2016-04-19 10:14:11 -04:00
|
|
|
# logging setup
|
|
|
|
logger = logging.getLogger('discord')
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
|
|
|
|
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
|
|
|
|
logger.addHandler(handler)
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
# FUNCTIONS
|
|
|
|
|
|
|
|
# converts a datetime to a string
|
|
|
|
def strfromdt(dt):
|
|
|
|
return dt.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
|
|
|
# converts a timestamp to a datetime
|
|
|
|
def dtfromts(ts):
|
|
|
|
return datetime.datetime.fromtimestamp(ts)
|
|
|
|
|
2016-12-01 14:29:58 -05:00
|
|
|
# gets now playing information from last.fm
|
|
|
|
def lastfm_np(username):
|
2016-12-01 14:40:24 -05:00
|
|
|
# sanitise username
|
|
|
|
cleanusername = re.sub(r'[^a-zA-Z0-9_-]', '', username, 0)
|
|
|
|
|
2016-12-01 16:11:11 -05:00
|
|
|
# fetch JSON from last.fm
|
|
|
|
payload = {'format': 'json', 'method': 'user.getRecentTracks', 'user': cleanusername, 'limit': '1', 'api_key': lfmkey}
|
|
|
|
r = requests.get("http://ws.audioscrobbler.com/2.0/", params=payload)
|
2016-12-01 14:29:58 -05:00
|
|
|
|
2016-12-01 16:11:11 -05:00
|
|
|
# read json data
|
|
|
|
np = r.json()
|
2016-12-01 14:29:58 -05:00
|
|
|
|
2016-12-01 16:11:11 -05:00
|
|
|
# check we got a valid response
|
|
|
|
if 'error' in np:
|
2016-12-05 11:05:59 -05:00
|
|
|
return "I couldn't get last.fm data for " + username
|
2016-12-01 14:29:58 -05:00
|
|
|
|
2016-12-01 16:11:11 -05:00
|
|
|
# get fields
|
2016-12-05 11:05:59 -05:00
|
|
|
try:
|
|
|
|
username = np['recenttracks']['@attr']['user']
|
|
|
|
track = np['recenttracks']['track'][0]
|
|
|
|
album = track['album']['#text']
|
|
|
|
artist = track['artist']['#text']
|
|
|
|
song = track['name']
|
|
|
|
nowplaying = '@attr' in track
|
|
|
|
except IndexError:
|
|
|
|
return "It looks like " + username + " hasn't played anything recently."
|
2016-12-01 14:29:58 -05:00
|
|
|
|
|
|
|
# grammar
|
|
|
|
if album != "":
|
|
|
|
albumtext = "` from the album `" + album + "`"
|
|
|
|
else:
|
|
|
|
albumtext = "`"
|
|
|
|
|
2016-12-01 16:11:11 -05:00
|
|
|
if nowplaying == True:
|
2016-12-01 14:29:58 -05:00
|
|
|
nowplaying = " is listening"
|
2016-12-01 16:11:11 -05:00
|
|
|
else:
|
|
|
|
nowplaying = " last listened"
|
2016-12-01 14:29:58 -05:00
|
|
|
|
|
|
|
# construct string
|
|
|
|
return username + nowplaying + " to `" + song + "` by `" + artist + albumtext
|
|
|
|
|
2017-04-08 09:32:48 -04:00
|
|
|
# gets general steam user info from a vanityurl name
|
|
|
|
def steamdata(vanityname):
|
|
|
|
# sanitise username
|
|
|
|
cleanvanityname = re.sub(r'[^a-zA-Z0-9_-]', '', vanityname, 0)
|
|
|
|
|
|
|
|
resolveurl = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key='
|
|
|
|
dataurl = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='
|
|
|
|
|
|
|
|
# fetch json from steam
|
|
|
|
try:
|
|
|
|
idresponse = requests.get(resolveurl + steamkey + '&vanityurl=' + vanityname).json()['response']
|
|
|
|
except:
|
|
|
|
return 'I can\'t connect to Steam'
|
|
|
|
|
|
|
|
# check if user was found and extract steamid
|
|
|
|
if idresponse['success'] is not 1:
|
|
|
|
return ' I couldn\'t find ' + vanityname
|
|
|
|
else:
|
|
|
|
steamid = idresponse['steamid']
|
|
|
|
|
|
|
|
# fetch steam user info
|
|
|
|
try:
|
|
|
|
dataresponse = requests.get(dataurl + steamkey + '&steamids=' + steamid).json()['response']['players'][0]
|
|
|
|
except:
|
|
|
|
return 'Can\'t find info on ' + vanityname
|
|
|
|
|
|
|
|
personastates = ['Offline', 'Online', 'Busy', 'Away', 'Snoozed', 'Looking to trade', 'Looking to play']
|
|
|
|
|
|
|
|
if 'personaname' in dataresponse:
|
|
|
|
namestr = dataresponse['personaname']
|
|
|
|
else: namestr = ''
|
|
|
|
if 'personastate' in dataresponse:
|
2017-04-08 09:48:12 -04:00
|
|
|
statestr = '`' + personastates[dataresponse['personastate']] + '`'
|
2017-04-08 09:32:48 -04:00
|
|
|
else: statestr = ''
|
|
|
|
if 'gameextrainfo' in dataresponse:
|
2017-04-08 09:48:12 -04:00
|
|
|
gamestr = ' playing `' + dataresponse['gameextrainfo'] + '`'
|
2017-04-08 09:32:48 -04:00
|
|
|
else: gamestr = ''
|
|
|
|
|
|
|
|
responsetext = [(namestr + ' is ' + statestr + gamestr).replace(' ', ' ')]
|
|
|
|
|
|
|
|
return '\n'.join(responsetext)
|
|
|
|
|
2016-04-01 09:32:18 -04:00
|
|
|
|
2016-03-30 16:28:59 -04:00
|
|
|
# EVENT HANDLERS
|
|
|
|
|
|
|
|
# called when client ready
|
|
|
|
@client.event
|
|
|
|
@asyncio.coroutine
|
|
|
|
def on_ready():
|
|
|
|
# info on terminal
|
|
|
|
print('Connected')
|
|
|
|
print('User: ' + client.user.name)
|
|
|
|
print('ID: ' + client.user.id)
|
|
|
|
|
|
|
|
# set "Now Playing" to print version
|
|
|
|
game = discord.Game(name = version)
|
2016-10-12 05:34:45 -04:00
|
|
|
yield from client.change_presence(game=game)
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
# called when message received
|
|
|
|
@client.event
|
|
|
|
@asyncio.coroutine
|
|
|
|
def on_message(message):
|
2016-11-07 16:56:54 -05:00
|
|
|
|
2016-03-30 16:28:59 -04:00
|
|
|
# print messages to terminal for info
|
2016-05-06 04:27:52 -04:00
|
|
|
timestr = time.strftime('%Y-%m-%d-%H:%M:%S: ')
|
2016-05-05 16:27:00 -04:00
|
|
|
try:
|
2016-05-06 04:27:52 -04:00
|
|
|
print(timestr + message.server.name + ' ' + message.channel.name + ' ' + message.author.name + ': ' + message.content)
|
2016-05-05 16:27:00 -04:00
|
|
|
except AttributeError:
|
2016-05-06 04:27:52 -04:00
|
|
|
print(timestr + 'PRIV ' + message.author.name + ': ' + message.content)
|
2016-04-01 09:32:18 -04:00
|
|
|
|
2016-04-02 08:31:28 -04:00
|
|
|
# do not parse own messages or private messages
|
|
|
|
if message.author != client.user and type(message.channel) is not discord.PrivateChannel:
|
2016-04-03 11:54:00 -04:00
|
|
|
# response to send to channel
|
|
|
|
response = ''
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
# parse messages for commands
|
2016-04-08 14:16:31 -04:00
|
|
|
if message.content.startswith('.info'):
|
2016-03-30 16:28:59 -04:00
|
|
|
# print bot info
|
2016-05-05 16:27:00 -04:00
|
|
|
pyver = str(sys.version_info[0]) + '.' + str(sys.version_info[1]) + '.' + str(sys.version_info[2])
|
2016-10-12 07:17:39 -04:00
|
|
|
appinfo = yield from client.application_info()
|
|
|
|
response = 'I am ' + appinfo.name + ', a Discord bot by ' + appinfo.owner.name + ' | ' + version + ' | Python ' + pyver + ' | discord.py ' + discord.__version__
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
elif message.content.startswith('.help'):
|
|
|
|
# print command list
|
2016-04-03 11:54:00 -04:00
|
|
|
response = helptext
|
2016-03-30 16:28:59 -04:00
|
|
|
|
2016-04-01 07:14:50 -04:00
|
|
|
elif message.content.startswith('.upskirt'):
|
2016-03-30 16:28:59 -04:00
|
|
|
# link to source code
|
2016-12-02 06:47:22 -05:00
|
|
|
response = 'No, don\'t look at my pantsu! Baka! <https://gitla.in/MrDetonia/maki> | <https://github.com/MrDetonia/Maki>'
|
2016-03-30 16:28:59 -04:00
|
|
|
|
2016-04-01 04:59:43 -04:00
|
|
|
elif message.content.startswith('.die'):
|
|
|
|
if message.author.id in admins:
|
|
|
|
# exit discord and kill bot
|
2016-04-19 11:28:01 -04:00
|
|
|
print('INFO: Accepting .die from ' + message.author.name)
|
2016-04-21 17:55:41 -04:00
|
|
|
run = False
|
2016-04-01 04:59:43 -04:00
|
|
|
yield from client.send_message(message.channel, 'But will I dream? ;_;')
|
|
|
|
yield from client.logout()
|
2016-12-01 18:31:18 -05:00
|
|
|
|
|
|
|
if message.content[5:] == 'reload':
|
|
|
|
# touch a file called 'reload' which signals we should restart
|
|
|
|
with open('reload', 'a'):
|
|
|
|
os.utime('reload', None)
|
2016-04-01 04:59:43 -04:00
|
|
|
else:
|
|
|
|
# user not admin, refuse
|
2016-04-03 11:54:00 -04:00
|
|
|
response = 'Don\'t be so rude! >:('
|
2016-03-30 16:28:59 -04:00
|
|
|
|
|
|
|
elif message.content.startswith('.whoami'):
|
|
|
|
# show info about user
|
2016-12-01 19:26:41 -05:00
|
|
|
response = 'User: `' + message.author.name + '` ID: `' + message.author.id + '` Discriminator: `' + message.author.discriminator + '`\nAccount Created: `' + strfromdt(message.author.created_at) + '`'
|
2016-03-30 16:28:59 -04:00
|
|
|
|
2016-04-08 14:15:03 -04:00
|
|
|
elif message.content.startswith('.whois '):
|
2016-04-01 09:27:00 -04:00
|
|
|
# show info about another user
|
|
|
|
tmp = message.content[7:]
|
2016-05-05 16:27:00 -04:00
|
|
|
user = message.server.get_member_named(tmp)
|
|
|
|
if user == None:
|
|
|
|
response = 'I can\'t find ' + tmp
|
2016-04-01 09:27:00 -04:00
|
|
|
else:
|
2016-12-01 19:26:41 -05:00
|
|
|
response = 'User: `' + user.name + '` ID: `' + user.id + '` Discriminator: `' + user.discriminator + '`\nAccount Created: `' + strfromdt(user.created_at) + '`'
|
2016-04-01 09:27:00 -04:00
|
|
|
|
2016-04-08 14:15:03 -04:00
|
|
|
elif message.content.startswith('.seen '):
|
2016-03-30 16:28:59 -04:00
|
|
|
# print when user was last seen
|
2016-11-07 16:56:54 -05:00
|
|
|
try:
|
2016-12-01 15:30:50 -05:00
|
|
|
target = message.server.id + message.server.get_member_named(message.content[6:]).id
|
2016-11-07 16:56:54 -05:00
|
|
|
except AttributeError:
|
2016-12-01 15:30:50 -05:00
|
|
|
response = "I can't find that user!"
|
2016-11-07 16:56:54 -05:00
|
|
|
target = ""
|
2016-05-05 16:27:00 -04:00
|
|
|
|
2016-12-01 15:30:50 -05:00
|
|
|
if target in history and history[target][0] == message.server.id:
|
2016-03-30 16:28:59 -04:00
|
|
|
# user logged, print last message and time
|
2016-12-01 15:30:50 -05:00
|
|
|
response = 'user ' + message.content[6:] + ' was last seen saying "' + history[target][2] + '" at ' + strfromdt(dtfromts(history[target][1]))
|
2016-04-16 08:40:14 -04:00
|
|
|
elif message.content[6:] == 'Maki':
|
2016-03-31 07:08:23 -04:00
|
|
|
# Maki doesn't need to be .seen
|
2016-04-03 11:54:00 -04:00
|
|
|
response = 'I\'m right here!'
|
2016-03-30 16:28:59 -04:00
|
|
|
else:
|
|
|
|
# user not logged
|
2016-11-07 16:56:54 -05:00
|
|
|
response = "user not seen yet"
|
2016-03-30 16:28:59 -04:00
|
|
|
|
2016-04-08 14:15:03 -04:00
|
|
|
elif message.content.startswith('.say '):
|
2016-04-01 08:43:26 -04:00
|
|
|
# delete calling message for effect
|
|
|
|
yield from client.delete_message(message)
|
2016-04-01 04:52:38 -04:00
|
|
|
# echo message
|
2016-04-03 11:54:00 -04:00
|
|
|
response = message.content[5:]
|
2016-04-01 04:52:38 -04:00
|
|
|
|
2016-04-08 14:15:03 -04:00
|
|
|
elif message.content.startswith('.sayy '):
|
|
|
|
# delete calling message
|
|
|
|
yield from client.delete_message(message)
|
|
|
|
# echo aesthetic message
|
2016-07-04 06:53:01 -04:00
|
|
|
response = ' '.join(message.content[6:])
|
2016-04-08 14:15:03 -04:00
|
|
|
|
2016-10-12 06:23:06 -04:00
|
|
|
elif message.content.startswith('.markov'):
|
2016-04-04 08:23:50 -04:00
|
|
|
# send typing signal to discord
|
2016-04-04 10:16:04 -04:00
|
|
|
for attempt in range(5):
|
|
|
|
try:
|
|
|
|
yield from client.send_typing(message.channel)
|
|
|
|
except discord.errors.HTTPException:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print('ERROR: Failed to send typing signal to discord after 5 attempts')
|
2016-04-04 08:23:50 -04:00
|
|
|
|
2016-04-03 11:03:40 -04:00
|
|
|
# generate a markov chain sentence based on the user's chat history
|
2016-04-03 11:25:35 -04:00
|
|
|
tmp = message.content[8:]
|
2016-10-12 06:23:06 -04:00
|
|
|
target = ''
|
|
|
|
|
2016-12-01 17:43:37 -05:00
|
|
|
if tmp == 'Maki':
|
|
|
|
response = "My markovs always say the same thing."
|
2016-10-12 06:23:06 -04:00
|
|
|
else:
|
2016-12-01 17:43:37 -05:00
|
|
|
# if no user provided, markov the author
|
|
|
|
if tmp == '':
|
|
|
|
target = message.server.id + '-' + message.author.id
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
target = message.server.id + '-' + message.server.get_member_named(tmp).id
|
|
|
|
except AttributeError:
|
|
|
|
response = "I can't find that user!"
|
|
|
|
|
|
|
|
if os.path.isfile('./markovs/' + target) and target != '':
|
|
|
|
mc = markov.Markov(open('./markovs/' + target))
|
|
|
|
response = mc.generate_text(random.randint(20,40))
|
|
|
|
elif target != '':
|
|
|
|
response = "I haven't seen them speak yet!"
|
2016-05-05 16:27:00 -04:00
|
|
|
|
|
|
|
elif message.content.startswith('.roll '):
|
|
|
|
# DnD style dice roll
|
|
|
|
tmp = message.content[6:]
|
|
|
|
|
|
|
|
#check syntax is valid
|
|
|
|
pattern = re.compile('^([0-9]+)d([0-9]+)$')
|
|
|
|
|
|
|
|
if pattern.match(tmp):
|
|
|
|
# extract numbers
|
|
|
|
nums = [int(s) for s in re.findall(r'\d+', message.content)]
|
|
|
|
|
|
|
|
# limit range
|
2016-10-12 05:56:33 -04:00
|
|
|
if nums[0] < 1: nums[0] = 1
|
|
|
|
if nums[1] < 1: nums[1] = 1
|
2016-05-05 16:27:00 -04:00
|
|
|
if nums[0] > 100: nums[0] = 100
|
|
|
|
if nums[1] > 1000000: nums[1] = 1000000
|
|
|
|
|
|
|
|
# roll dice multiple times and sum
|
|
|
|
rollsum = 0
|
|
|
|
for i in range(nums[0]):
|
|
|
|
rollsum += random.randint(1, nums[1])
|
|
|
|
|
2016-10-12 05:56:33 -04:00
|
|
|
response = 'Using ' + str(nums[0]) + 'd' + str(nums[1]) + ' you rolled: ' + str(rollsum)
|
2016-05-05 16:27:00 -04:00
|
|
|
else:
|
|
|
|
response = 'you did it wrong!'
|
2016-04-03 11:03:40 -04:00
|
|
|
|
2016-12-01 14:29:58 -05:00
|
|
|
elif message.content.startswith('.np'):
|
2016-12-01 15:30:50 -05:00
|
|
|
# show now playing info from last.fm
|
2016-12-01 14:29:58 -05:00
|
|
|
tmp = message.content[4:]
|
|
|
|
|
|
|
|
if tmp == '':
|
|
|
|
response = lastfm_np(message.author.name)
|
|
|
|
else:
|
|
|
|
response = lastfm_np(tmp)
|
|
|
|
|
2017-04-08 09:32:48 -04:00
|
|
|
elif message.content.startswith('.steam'):
|
|
|
|
# show steam status
|
|
|
|
tmp = message.content[7:]
|
|
|
|
|
|
|
|
if tmp == '':
|
|
|
|
response = steamdata(message.author.name)
|
|
|
|
else:
|
|
|
|
response = steamdata(tmp)
|
|
|
|
|
2016-05-25 08:10:24 -04:00
|
|
|
elif message.content.startswith('.qr '):
|
|
|
|
# generate QR code - DANGEROUS, CHECK CAREFULLY HERE
|
|
|
|
tmp = message.content[4:]
|
|
|
|
|
|
|
|
# send typing signal to discord
|
|
|
|
for attempt in range(5):
|
|
|
|
try:
|
|
|
|
yield from client.send_typing(message.channel)
|
|
|
|
except discord.errors.HTTPException:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print('ERROR: Failed to send typing signal to discord after 5 attempts')
|
|
|
|
|
|
|
|
# make sure there are no nasty characters
|
|
|
|
msg = re.sub(r'[^a-zA-Z0-9_ -]', '', tmp, 0)
|
|
|
|
|
|
|
|
# echo message
|
|
|
|
cmd = 'echo "\'' + msg + '\'"'
|
|
|
|
args = shlex.split(cmd)
|
|
|
|
echo = subprocess.Popen(args, stdout=subprocess.PIPE)
|
|
|
|
|
|
|
|
# generate QR code
|
|
|
|
cmd = 'qrencode -t png -o -'
|
|
|
|
args = shlex.split(cmd)
|
|
|
|
qr = subprocess.Popen(args, stdin=echo.stdout, stdout=subprocess.PIPE)
|
|
|
|
|
|
|
|
# upload file with curl and get URL
|
|
|
|
cmd = 'curl -F upload=@- https://w1r3.net'
|
|
|
|
args = shlex.split(cmd)
|
|
|
|
out = subprocess.check_output(args, stdin=qr.stdout)
|
|
|
|
|
|
|
|
# run piped commands
|
|
|
|
echo.wait()
|
|
|
|
|
|
|
|
# send response
|
|
|
|
response = out.decode('utf-8').strip()
|
|
|
|
|
2016-12-02 19:55:49 -05:00
|
|
|
elif message.content.startswith('.quiet'):
|
2016-12-02 20:00:05 -05:00
|
|
|
if message.author.id in admins:
|
|
|
|
quiet[message.server.id] = 1
|
2016-12-02 20:01:23 -05:00
|
|
|
else:
|
2016-12-02 20:00:05 -05:00
|
|
|
response = "No, *you* be quiet!"
|
2016-12-02 19:55:49 -05:00
|
|
|
|
|
|
|
elif message.content.startswith('.loud'):
|
2016-12-02 20:00:05 -05:00
|
|
|
if message.server.id in quiet and message.author.id in admins:
|
2016-12-02 19:55:49 -05:00
|
|
|
quiet.pop(message.server.id, None)
|
|
|
|
|
2016-04-03 11:03:40 -04:00
|
|
|
# Stuff that happens when message is not a bot command:
|
2016-04-01 08:47:53 -04:00
|
|
|
else:
|
|
|
|
# log each message against users
|
2016-04-16 08:40:14 -04:00
|
|
|
if message.content != "":
|
2016-12-01 15:30:50 -05:00
|
|
|
history[message.server.id + message.author.id] = (message.server.id, time.time(), message.content)
|
2016-04-16 08:40:14 -04:00
|
|
|
with open('hist.json', 'w') as fp:
|
|
|
|
json.dump(history, fp)
|
2016-04-01 08:47:53 -04:00
|
|
|
|
2016-04-04 04:38:24 -04:00
|
|
|
# log user messages for markov chains, ignoring messages with certain substrings
|
2016-12-01 14:53:54 -05:00
|
|
|
filters = ['`', 'http://', 'https://']
|
2016-04-04 04:38:24 -04:00
|
|
|
if not any(x in message.content for x in filters):
|
2016-11-07 16:56:54 -05:00
|
|
|
try:
|
2016-12-01 14:53:54 -05:00
|
|
|
with open('./markovs/' + message.server.id + '-' + message.author.id, 'a') as fp:
|
2016-11-07 16:56:54 -05:00
|
|
|
fp.write('\n' + message.content)
|
|
|
|
except PermissionError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# someone noticed me! <3
|
2017-04-22 10:37:51 -04:00
|
|
|
if bool(re.search(r'\bmaki\b', message.content, re.IGNORECASE)):
|
2016-11-07 16:56:54 -05:00
|
|
|
yield from client.add_reaction(message, '\N{BLACK HEART SUIT}')
|
2016-04-03 11:03:40 -04:00
|
|
|
|
2016-12-01 16:33:53 -05:00
|
|
|
# butter
|
2017-04-22 10:37:51 -04:00
|
|
|
if bool(re.search(r'\bbutter\b', message.content, re.IGNORECASE)):
|
2016-12-01 16:33:53 -05:00
|
|
|
yield from client.add_reaction(message, '\N{PERSON WITH FOLDED HANDS}')
|
|
|
|
|
2016-12-06 18:20:24 -05:00
|
|
|
# egg
|
2017-04-22 10:37:51 -04:00
|
|
|
if bool(re.search(r'\begg\b', message.content, re.IGNORECASE)):
|
2016-12-06 18:20:24 -05:00
|
|
|
yield from client.add_reaction(message, '\N{AUBERGINE}')
|
|
|
|
|
2016-04-03 11:54:00 -04:00
|
|
|
# send response to channel if needed:
|
2016-12-02 19:55:49 -05:00
|
|
|
if response is not '' and message.server.id not in quiet:
|
2016-04-03 11:54:00 -04:00
|
|
|
for attempt in range(5):
|
|
|
|
try:
|
|
|
|
yield from client.send_message(message.channel, response)
|
2016-04-03 11:55:38 -04:00
|
|
|
except discord.errors.HTTPException:
|
2016-04-03 11:54:00 -04:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print('ERROR: Failed to send message to discord after 5 attempts')
|
|
|
|
|
2016-03-30 16:28:59 -04:00
|
|
|
# Run the client
|
2016-04-24 16:15:40 -04:00
|
|
|
client.run(token)
|
2016-04-21 17:55:41 -04:00
|
|
|
|
|
|
|
# finish execution
|
|
|
|
exit(0)
|