1
0
mirror of https://github.com/MrDetonia/Maki.git synced 2024-11-22 03:44:18 -05:00

v1.1.0, improved dice roll mechanic, code reformatting by yapf

This commit is contained in:
MrDetonia 2018-08-23 20:30:26 +00:00
parent 998ec2bf3a
commit 47fa9e95cf
6 changed files with 341 additions and 309 deletions

View File

@ -5,8 +5,6 @@
# Copyright 2018 Zac Herd # Copyright 2018 Zac Herd
# Licensed under BSD 3-clause License, see LICENSE.md for more info # Licensed under BSD 3-clause License, see LICENSE.md for more info
# IMPORTS # IMPORTS
import os import os
import asyncio import asyncio
@ -14,60 +12,59 @@ import subprocess
import discord import discord
import urllib.request import urllib.request
# LOCAL IMPORTS # LOCAL IMPORTS
from common import * from common import *
from helpers import * from helpers import *
# COMMAND IMPLEMENTATINS # COMMAND IMPLEMENTATINS
@asyncio.coroutine @asyncio.coroutine
def cmd_die(client, msg): def cmd_die(client, msg):
print("INFO: accepting .die from " + msg.author.name) print("INFO: accepting .die from " + msg.author.name)
yield from client.send_message(msg.channel, "But will I dream? ;-;") yield from client.send_message(msg.channel, "But will I dream? ;-;")
yield from client.logout() yield from client.logout()
if msg.content[5:] == "reload": if msg.content[5:] == "reload":
# touch file to signal reload # touch file to signal reload
with open("reload", "a"): with open("reload", "a"):
os.utime("reload", None) os.utime("reload", None)
@asyncio.coroutine @asyncio.coroutine
def cmd_quiet(client, msg): def cmd_quiet(client, msg):
quiet[msg.server.id] = 1 quiet[msg.server.id] = 1
@asyncio.coroutine @asyncio.coroutine
def cmd_loud(client, msg): def cmd_loud(client, msg):
if msg.server.id in quiet: if msg.server.id in quiet:
quiet.pop(msg.server.id, None) quiet.pop(msg.server.id, None)
@asyncio.coroutine @asyncio.coroutine
def cmd_avatar(client, msg): def cmd_avatar(client, msg):
url = msg.content[8:] url = msg.content[8:]
response = "Avatar updated!" response = "Avatar updated!"
try: try:
httpresponse = urllib.request.urlopen(url) httpresponse = urllib.request.urlopen(url)
imgdata = httpresponse.read() imgdata = httpresponse.read()
yield from client.edit_profile(avatar=imgdata) yield from client.edit_profile(avatar=imgdata)
except urllib.error.URLError as e: except urllib.error.URLError as e:
response = "URL Error: " + str(e) response = "URL Error: " + str(e)
except discord.HTTPException: except discord.HTTPException:
response = "Dicsord failed to edit my profile!" response = "Dicsord failed to edit my profile!"
except discord.InvalidArgument: except discord.InvalidArgument:
response = "Invalid image!" response = "Invalid image!"
except: except:
response = "Error updating avatar!" response = "Error updating avatar!"
yield from discord_send(client, msg, response)
yield from discord_send(client, msg, response)
# COMMAND HANDLING # COMMAND HANDLING
admincommands = { admincommands = {
"die": cmd_die, "die": cmd_die,
"quiet": cmd_quiet, "quiet": cmd_quiet,
"loud": cmd_loud, "loud": cmd_loud,
"avatar": cmd_avatar, "avatar": cmd_avatar,
} }

92
bot.py
View File

@ -1,4 +1,3 @@
# Maki # Maki
# ---- # ----
# Discord bot by MrDetonia # Discord bot by MrDetonia
@ -6,8 +5,6 @@
# Copyright 2018 Zac Herd # Copyright 2018 Zac Herd
# Licensed under BSD 3-clause License, see LICENSE.md for more info # Licensed under BSD 3-clause License, see LICENSE.md for more info
# IMPORTS # IMPORTS
import discord import discord
import asyncio import asyncio
@ -33,7 +30,6 @@ from admincommands import *
# file in this directory called "secret.py" should contain these variables # file in this directory called "secret.py" should contain these variables
from secret import token, lfmkey, steamkey from secret import token, lfmkey, steamkey
# DISCORD CLIENT INSTANCE # DISCORD CLIENT INSTANCE
client = discord.Client() client = discord.Client()
@ -43,60 +39,68 @@ client = discord.Client()
@client.event @client.event
@asyncio.coroutine @asyncio.coroutine
def on_ready(): def on_ready():
# info on terminal # info on terminal
print('Connected') print('Connected')
print('User: ' + client.user.name) print('User: ' + client.user.name)
print('ID: ' + client.user.id) print('ID: ' + client.user.id)
# set "Now Playing" to print version # set "Now Playing" to print version
game = discord.Game(name = version) game = discord.Game(name=version)
yield from client.change_presence(game=game) yield from client.change_presence(game=game)
# called when message received # called when message received
@client.event @client.event
@asyncio.coroutine @asyncio.coroutine
def on_message(msg): def on_message(msg):
# print messages to terminal for info # print messages to terminal for info
timestr = time.strftime('%Y-%m-%d-%H:%M:%S: ') timestr = time.strftime('%Y-%m-%d-%H:%M:%S: ')
try: try:
print("{} {} - {} | {}: {}".format(timestr, msg.server.name, msg.channel.name, msg.author.name, msg.content)) print("{} {} - {} | {}: {}".format(timestr, msg.server.name,
except AttributeError: msg.channel.name, msg.author.name,
print("{} PRIVATE | {}: {}".format(timestr, msg.author.name, msg.content)) msg.content))
except AttributeError:
print("{} PRIVATE | {}: {}".format(timestr, msg.author.name,
msg.content))
# do not parse own messages or private messages # do not parse own messages or private messages
if msg.author != client.user and type(msg.channel) is not discord.PrivateChannel: if msg.author != client.user and type(
# log each message against users msg.channel) is not discord.PrivateChannel:
if msg.content != "": # log each message against users
history[msg.server.id + msg.author.id] = (msg.server.id, time.time(), msg.content) if msg.content != "":
with open('hist.json', 'w') as fp: history[msg.server.id + msg.author.id] = (msg.server.id,
json.dump(history, fp) time.time(), msg.content)
with open('hist.json', 'w') as fp:
json.dump(history, fp)
# log user messages for markov chains, ignoring messages with certain substrings # log user messages for markov chains, ignoring messages with certain substrings
filters = ['`', 'http://', 'https://'] filters = ['`', 'http://', 'https://']
if not any(x in msg.content for x in filters): if not any(x in msg.content for x in filters):
try: try:
with open('./markovs/' + msg.server.id + '-' + msg.author.id, 'a') as fp: with open('./markovs/' + msg.server.id + '-' + msg.author.id,
fp.write('\n' + msg.content) 'a') as fp:
except PermissionError: pass fp.write('\n' + msg.content)
except PermissionError:
pass
# react to stuff # react to stuff
yield from makireacts(client, msg) yield from makireacts(client, msg)
# check for commands # check for commands
if msg.content.startswith(prefix): if msg.content.startswith(prefix):
cmd = msg.content.split(' ', 1)[0][1:] cmd = msg.content.split(' ', 1)[0][1:]
if cmd in commands: if cmd in commands:
yield from commands[cmd](client, msg) yield from commands[cmd](client, msg)
elif cmd in admincommands and msg.author.id in admins: elif cmd in admincommands and msg.author.id in admins:
yield from admincommands[cmd](client, msg) yield from admincommands[cmd](client, msg)
# MAIN FUNCTION # MAIN FUNCTION
def main(): def main():
logger() logger()
client.run(token) client.run(token)
exit(0) exit(0)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -5,8 +5,6 @@
# Copyright 2018 Zac Herd # Copyright 2018 Zac Herd
# Licensed under BSD 3-clause License, see LICENSE.md for more info # Licensed under BSD 3-clause License, see LICENSE.md for more info
# IMPORTS # IMPORTS
import asyncio import asyncio
import os import os
@ -16,7 +14,6 @@ import requests
import random import random
import subprocess import subprocess
# LOCAL IMPORTS # LOCAL IMPORTS
from common import * from common import *
from helpers import * from helpers import *
@ -24,25 +21,26 @@ from secret import lfmkey, steamkey
import markov import markov
# COMMAND IMPLEMENTATIONS # COMMAND IMPLEMENTATIONS
@asyncio.coroutine @asyncio.coroutine
def cmd_help(client, msg): def cmd_help(client, msg):
yield from discord_send(client, msg, helptext) yield from discord_send(client, msg, helptext)
@asyncio.coroutine @asyncio.coroutine
def cmd_info(client, msg): def cmd_info(client, msg):
pyver = "{}.{}.{}".format(sys.version_info[0], sys.version_info[1], sys.version_info[2]) pyver = "{}.{}.{}".format(sys.version_info[0], sys.version_info[1],
appinfo = yield from client.application_info() sys.version_info[2])
response = "I am **{}**, a Discord bot by **{}** | `{}` | Python `{}` | discord.py `{}`".format(appinfo.name, appinfo.owner.name, version, pyver, discord.__version__) appinfo = yield from client.application_info()
yield from discord_send(client, msg, response) response = "I am **{}**, a Discord bot by **{}** | `{}` | Python `{}` | discord.py `{}`".format(
appinfo.name, appinfo.owner.name, version, pyver, discord.__version__)
yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_upskirt(client, msg): def cmd_upskirt(client, msg):
response = "No, don\'t look at my pantsu, baka! <https://github.com/MrDetonia/maki>" response = "No, don\'t look at my pantsu, baka! <https://github.com/MrDetonia/maki>"
yield from discord_send(client, msg, response) yield from discord_send(client, msg, response)
whoistring = "**{}#{}**: `{}`\n**Account Created:** `{}`" whoistring = "**{}#{}**: `{}`\n**Account Created:** `{}`"
@ -50,267 +48,301 @@ whoistring = "**{}#{}**: `{}`\n**Account Created:** `{}`"
@asyncio.coroutine @asyncio.coroutine
def cmd_whoami(client, msg): def cmd_whoami(client, msg):
response = whoistring.format(msg.author.name, msg.author.discriminator, msg.author.id, strfromdt(msg.author.created_at)) response = whoistring.format(msg.author.name,
yield from discord_send(client, msg, response) msg.author.discriminator, msg.author.id,
strfromdt(msg.author.created_at))
yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_whois(client, msg): def cmd_whois(client, msg):
tmp = msg.content[7:] tmp = msg.content[7:]
user = msg.server.get_member_named(tmp) user = msg.server.get_member_named(tmp)
if user == None: if user == None:
reponse = "I can't find `{}`".format(tmp) reponse = "I can't find `{}`".format(tmp)
else: else:
response = whoistring.format(user.name, user.discriminator, user.id, strfromdt(user.created_at)) response = whoistring.format(user.name, user.discriminator, user.id,
strfromdt(user.created_at))
yield from discord_send(client, msg, response) yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_seen(client, msg): def cmd_seen(client, msg):
tmp = msg.content[6:] tmp = msg.content[6:]
user = msg.server.get_member_named(tmp) user = msg.server.get_member_named(tmp)
if user == None: if user == None:
reponse = "I can't find `{}`".format(tmp) reponse = "I can't find `{}`".format(tmp)
elif user.name == "Maki": elif user.name == "Maki":
reponse = "I'm right here!" reponse = "I'm right here!"
else: else:
target = msg.server.id + user.id target = msg.server.id + user.id
if target in history and history[target][0] == msg.server.id: if target in history and history[target][0] == msg.server.id:
response = "**{}** was last seen saying the following at {}:\n{}".format(user.name, strfromdt(dtfromts(history[target][1])), history[target][2]) response = "**{}** was last seen saying the following at {}:\n{}".format(
else: user.name, strfromdt(dtfromts(history[target][1])),
response = "I haven't seen **{}** speak yet!".format(tmp) history[target][2])
else:
response = "I haven't seen **{}** speak yet!".format(tmp)
yield from discord_send(client, msg, response) yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_say(client, msg): def cmd_say(client, msg):
response = msg.content[5:] response = msg.content[5:]
yield from client.delete_message(msg) yield from client.delete_message(msg)
yield from discord_send(client, msg, response) yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_sayy(client, msg): def cmd_sayy(client, msg):
response = " ".join(msg.content[6:]) response = " ".join(msg.content[6:])
yield from client.delete_message(msg) yield from client.delete_message(msg)
yield from discord_send(client, msg, response) yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_markov(client, msg): def cmd_markov(client, msg):
yield from discord_typing(client, msg) yield from discord_typing(client, msg)
tmp = msg.content[8:] tmp = msg.content[8:]
target = "" target = ""
if tmp == "Maki": if tmp == "Maki":
response = "My markovs always say the same thing" response = "My markovs always say the same thing"
else: else:
if tmp == "": if tmp == "":
target = "{}-{}".format(msg.server.id, msg.author.id) target = "{}-{}".format(msg.server.id, msg.author.id)
else: else:
try: try:
target = "{}-{}".format(msg.server.id, msg.server.get_member_named(tmp).id) target = "{}-{}".format(msg.server.id,
except AttributeError: msg.server.get_member_named(tmp).id)
reponse = "I can't find `{}`".format(tmp) except AttributeError:
reponse = "I can't find `{}`".format(tmp)
if target != "": if target != "":
mfile = "./markovs/" + target mfile = "./markovs/" + target
if os.path.isfile(mfile): if os.path.isfile(mfile):
mc = markov.Markov(open(mfile)) mc = markov.Markov(open(mfile))
response = mc.generate_text(random.randint(20,40)) response = mc.generate_text(random.randint(20, 40))
else: else:
response = "I haven't seen `{}` speak yet.".format(tmp) response = "I haven't seen `{}` speak yet.".format(tmp)
yield from discord_send(client, msg, response); yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_roll(client, msg): def cmd_roll(client, msg):
tmp = msg.content[6:] tmp = msg.content[6:]
pattern = re.compile("^([0-9]+)d([0-9]+)$") pattern = re.compile("^([0-9]+)d([0-9]+)$")
pattern2 = re.compile("^d([0-9]+)$") pattern2 = re.compile("^d([0-9]+)$")
if pattern.match(tmp): # extract numbers
# extract numbers nums = [int(s) for s in re.findall(r"\d+", tmp)]
nums = [int(s) for s in re.findall(r"\d+", tmp)]
# limit ranges if pattern.match(tmp):
nums[0] = clamp(nums[0], 1,100) numdice = nums[0]
nums[1] = clamp(nums[1], 1, 1000000) diceval = nums[1]
elif pattern2.match(tmp):
numdice = 1
diceval = nums[0]
else:
response = "Expected format: `[<num>]d<value>`"
yield from discord_send(client, msg, response)
# roll and sum dice # limit ranges
rollsum = 0 numdice = clamp(numdice, 1, 10)
for i in range(nums[0]): diceval = clamp(diceval, 1, 1000)
rollsum += random.randint(1, nums[1])
response = "Using `{}d{}`, {} rolled: `{}`".format(nums[0], nums[1], msg.author.display_name, rollsum) # roll and sum dice
elif pattern2.match(tmp): rolls = []
# extract number for i in range(numdice):
num = [int(s) for s in re.findall(r"\d+", tmp)] rolls.append(random.randint(1, diceval))
# limit range rollsum = sum(rolls)
num[0] = clamp(num[0], 1, 1000000)
# roll dice response = "**{} rolled:** {}d{}".format(msg.author.display_name, numdice,
roll = random.randint(1, num[0]) diceval)
response = "Using `1d{}`, {} rolled `{}`".format(num[0], msg.author.display_name, roll) if numdice > 1:
else: response += "\n**Rolls:** `{}`".format(rolls)
response = "Expected format: `[<num>]d<value>`"
yield from discord_send(client, msg, response) response += "\n**Result:** `{}`".format(rollsum)
if rollsum == numdice * diceval:
response += " *(Natural)*"
elif rollsum == numdice:
response += " *(Crit fail)*"
yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_qr(client, msg): def cmd_qr(client, msg):
tmp = msg.content[4:] tmp = msg.content[4:]
yield from discord_typing(client, msg) yield from discord_typing(client, msg)
# generate qr code # generate qr code
qr = subprocess.Popen("qrencode -t png -o -".split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE) qr = subprocess.Popen(
qr.stdin.write(tmp.encode("utf-8")) "qrencode -t png -o -".split(),
qr.stdin.close() stdin=subprocess.PIPE,
out = subprocess.check_output("curl -F upload=@- https://w1r3.net".split(), stdin=qr.stdout) stdout=subprocess.PIPE)
qr.stdin.write(tmp.encode("utf-8"))
qr.stdin.close()
out = subprocess.check_output(
"curl -F upload=@- https://w1r3.net".split(), stdin=qr.stdout)
response = out.decode("utf-8").strip() response = out.decode("utf-8").strip()
yield from discord_send(client, msg, response) yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_np(client, msg): def cmd_np(client, msg):
tmp = msg.content[4:] tmp = msg.content[4:]
if tmp == "": if tmp == "":
response = lastfm_np(msg.author.name) response = lastfm_np(msg.author.name)
else: else:
response = lastfm_np(tmp) response = lastfm_np(tmp)
print("CALLING SEND") print("CALLING SEND")
yield from discord_send(client, msg, response) yield from discord_send(client, msg, response)
@asyncio.coroutine @asyncio.coroutine
def cmd_steam(client, msg): def cmd_steam(client, msg):
tmp = msg.content[7:] tmp = msg.content[7:]
if tmp == "": if tmp == "":
response = steamdata(msg.author.name) response = steamdata(msg.author.name)
else: else:
response = steamdata(tmp) response = steamdata(tmp)
yield from discord_send(client, msg, response)
yield from discord_send(client, msg, response)
# HELPER FUNCTIONS # HELPER FUNCTIONS
# gets now playing information from last.fm # gets now playing information from last.fm
def lastfm_np(username): def lastfm_np(username):
# sanitise username # sanitise username
cleanusername = re.sub(r'[^a-zA-Z0-9_-]', '', username, 0) cleanusername = re.sub(r'[^a-zA-Z0-9_-]', '', username, 0)
# fetch JSON from last.fm # fetch JSON from last.fm
payload = {'format': 'json', 'method': 'user.getRecentTracks', 'user': cleanusername, 'limit': '1', 'api_key': lfmkey} payload = {
r = requests.get("http://ws.audioscrobbler.com/2.0/", params=payload) 'format': 'json',
'method': 'user.getRecentTracks',
'user': cleanusername,
'limit': '1',
'api_key': lfmkey
}
r = requests.get("http://ws.audioscrobbler.com/2.0/", params=payload)
# read json data # read json data
np = r.json() np = r.json()
# check we got a valid response # check we got a valid response
if 'error' in np: if 'error' in np:
return "I couldn't get last.fm data for `{}`".format(username) return "I couldn't get last.fm data for `{}`".format(username)
# get fields # get fields
try: try:
username = np['recenttracks']['@attr']['user'] username = np['recenttracks']['@attr']['user']
track = np['recenttracks']['track'][0] track = np['recenttracks']['track'][0]
album = track['album']['#text'] album = track['album']['#text']
artist = track['artist']['#text'] artist = track['artist']['#text']
song = track['name'] song = track['name']
nowplaying = '@attr' in track nowplaying = '@attr' in track
except IndexError: except IndexError:
return "It looks like `{}` hasn't played anything recently.".format(username) return "It looks like `{}` hasn't played anything recently.".format(
username)
# grammar # grammar
if album != "": if album != "":
albumtext = "` from the album `{}`".format(album) albumtext = "` from the album `{}`".format(album)
else: else:
albumtext = "`" albumtext = "`"
if nowplaying == True: if nowplaying == True:
nowplaying = " is listening" nowplaying = " is listening"
else: else:
nowplaying = " last listened" nowplaying = " last listened"
# construct string # construct string
return "{}{} to `{}` by `{}{}".format(username, nowplaying, song, artist, albumtext) return "{}{} to `{}` by `{}{}".format(username, nowplaying, song, artist,
albumtext)
# gets general steam user info from a vanityurl name # gets general steam user info from a vanityurl name
def steamdata(vanityname): def steamdata(vanityname):
# sanitise username # sanitise username
cleanvanityname = re.sub(r'[^a-zA-Z0-9_-]', '', vanityname, 0) cleanvanityname = re.sub(r'[^a-zA-Z0-9_-]', '', vanityname, 0)
resolveurl = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=' resolveurl = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key='
dataurl = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' dataurl = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='
# fetch json from steam # fetch json from steam
try: try:
idresponse = requests.get(resolveurl + steamkey + '&vanityurl=' + vanityname).json()['response'] idresponse = requests.get(resolveurl + steamkey + '&vanityurl=' +
except: vanityname).json()['response']
return "I can't connect to Steam" except:
return "I can't connect to Steam"
# check if user was found and extract steamid # check if user was found and extract steamid
if idresponse['success'] is not 1: if idresponse['success'] is not 1:
return "I couldn't find `{}`".format(vanityname) return "I couldn't find `{}`".format(vanityname)
else: else:
steamid = idresponse['steamid'] steamid = idresponse['steamid']
# fetch steam user info # fetch steam user info
try: try:
dataresponse = requests.get(dataurl + steamkey + '&steamids=' + steamid).json()['response']['players'][0] dataresponse = requests.get(dataurl + steamkey + '&steamids=' +
except: steamid).json()['response']['players'][0]
return "Can't find info on `{}`".format(vanityname) except:
return "Can't find info on `{}`".format(vanityname)
personastates = ['Offline', 'Online', 'Busy', 'Away', 'Snoozed', 'Looking to trade', 'Looking to play'] personastates = [
'Offline', 'Online', 'Busy', 'Away', 'Snoozed', 'Looking to trade',
'Looking to play'
]
if 'personaname' in dataresponse: namestr = dataresponse['personaname'] if 'personaname' in dataresponse: namestr = dataresponse['personaname']
else: namestr = '' else: namestr = ''
if 'personastate' in dataresponse: statestr = '`' + personastates[dataresponse['personastate']] + '`' if 'personastate' in dataresponse:
else: statestr = '' statestr = '`' + personastates[dataresponse['personastate']] + '`'
if 'gameextrainfo' in dataresponse: gamestr = ' playing `' + dataresponse['gameextrainfo'] + '`' else:
else: gamestr = '' statestr = ''
if 'gameextrainfo' in dataresponse:
gamestr = ' playing `' + dataresponse['gameextrainfo'] + '`'
else:
gamestr = ''
responsetext = [(namestr + ' is ' + statestr + gamestr).replace(' ', ' ')] responsetext = [(namestr + ' is ' + statestr + gamestr).replace(' ', ' ')]
return '\n'.join(responsetext)
return '\n'.join(responsetext)
# COMMAND HANDLING # COMMAND HANDLING
prefix = "." prefix = "."
commands = { commands = {
"help": cmd_help, "help": cmd_help,
"info": cmd_info, "info": cmd_info,
"upskirt": cmd_upskirt, "upskirt": cmd_upskirt,
"whoami": cmd_whoami, "whoami": cmd_whoami,
"whois": cmd_whois, "whois": cmd_whois,
"seen": cmd_seen, "seen": cmd_seen,
"say": cmd_say, "say": cmd_say,
"sayy": cmd_sayy, "sayy": cmd_sayy,
"markov": cmd_markov, "markov": cmd_markov,
"roll": cmd_roll, "roll": cmd_roll,
"qr": cmd_qr, "qr": cmd_qr,
"np": cmd_np, "np": cmd_np,
"steam": cmd_steam, "steam": cmd_steam,
} }

View File

@ -5,16 +5,12 @@
# Copyright 2018 Zac Herd # Copyright 2018 Zac Herd
# Licensed under BSD 3-clause License, see LICENSE.md for more info # Licensed under BSD 3-clause License, see LICENSE.md for more info
# IMPORTS # IMPORTS
import os import os
import json import json
# bot version # bot version
version = "v1.0.10" version = "v1.1.0"
# TODO: generate this on the fly and make it look acceptable # TODO: generate this on the fly and make it look acceptable
# text shown by .help command # text shown by .help command
@ -42,8 +38,8 @@ admins = ['116883900688629761']
# log of users' last messages and timestamps # log of users' last messages and timestamps
history = {} history = {}
if os.path.isfile('hist.json'): if os.path.isfile('hist.json'):
with open('hist.json', 'r') as fp: with open('hist.json', 'r') as fp:
history = json.load(fp) history = json.load(fp)
# quiet modes # quiet modes
quiet = {} quiet = {}

View File

@ -5,7 +5,6 @@
# Copyright 2018 Zac Herd # Copyright 2018 Zac Herd
# Licensed under BSD 3-clause License, see LICENSE.md for more info # Licensed under BSD 3-clause License, see LICENSE.md for more info
# IMPORTS # IMPORTS
import asyncio import asyncio
import discord import discord
@ -18,68 +17,72 @@ from common import *
# clamps an integer # clamps an integer
def clamp(n, small, large): return max(small, min(n, large)) def clamp(n, small, large):
return max(small, min(n, large))
# converts a datetime to a string # converts a datetime to a string
def strfromdt(dt): def strfromdt(dt):
return dt.strftime('%Y-%m-%d %H:%M:%S') return dt.strftime('%Y-%m-%d %H:%M:%S')
# converts a timestamp to a datetime # converts a timestamp to a datetime
def dtfromts(ts): def dtfromts(ts):
return datetime.datetime.fromtimestamp(ts) return datetime.datetime.fromtimestamp(ts)
# logging setup # logging setup
def logger(): def logger():
logger = logging.getLogger('discord') logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w') handler = logging.FileHandler(
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) filename='discord.log', encoding='utf-8', mode='w')
logger.addHandler(handler) handler.setFormatter(
logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
# send_message wrapper (deals with Discord's shit API) # send_message wrapper (deals with Discord's shit API)
@asyncio.coroutine @asyncio.coroutine
def discord_send(client, message, response): def discord_send(client, message, response):
if response is not '' and message.server.id not in quiet: if response is not '' and message.server.id not in quiet:
for attempt in range(5): for attempt in range(5):
try: try:
yield from client.send_message(message.channel, response) yield from client.send_message(message.channel, response)
except discord.errors.HTTPException: except discord.errors.HTTPException:
continue continue
else: else:
break break
else: else:
print('ERROR: Failed to send message to discord after 5 attempts') print('ERROR: Failed to send message to discord after 5 attempts')
# send typing signal to Discord # send typing signal to Discord
@asyncio.coroutine @asyncio.coroutine
def discord_typing(client, message): def discord_typing(client, message):
for attempt in range(5): for attempt in range(5):
try: try:
yield from client.send_typing(message.channel) yield from client.send_typing(message.channel)
except discord.errors.HTTPException: except discord.errors.HTTPException:
continue continue
else: else:
break break
else: else:
print('ERROR: Failed to send typing signal to discord after 5 attempts') print(
'ERROR: Failed to send typing signal to discord after 5 attempts')
# Maki Reacts to... # Maki Reacts to...
@asyncio.coroutine @asyncio.coroutine
def makireacts(client, msg): def makireacts(client, msg):
# TODO: track down the person(s) responsible for naming emoji # TODO: track down the person(s) responsible for naming emoji
reactions = { reactions = {
r"\bmaki\b": "\N{BLACK HEART SUIT}", r"\bmaki\b": "\N{BLACK HEART SUIT}",
r"\bbutter\b": "\N{PERSON WITH FOLDED HANDS}", r"\bbutter\b": "\N{PERSON WITH FOLDED HANDS}",
r"\begg[s]?\b": "\N{AUBERGINE}", r"\begg[s]?\b": "\N{AUBERGINE}",
r"\bproblematic\b": "\N{EYEGLASSES}", r"\bproblematic\b": "\N{EYEGLASSES}",
} }
for i in reactions: for i in reactions:
if bool(re.search(i, msg.content, re.IGNORECASE)): if bool(re.search(i, msg.content, re.IGNORECASE)):
yield from client.add_reaction(msg, reactions[i]) yield from client.add_reaction(msg, reactions[i])

View File

@ -1,7 +1,7 @@
import random import random
class Markov(object):
class Markov(object):
def __init__(self, open_file): def __init__(self, open_file):
self.cache = {} self.cache = {}
self.open_file = open_file self.open_file = open_file
@ -20,14 +20,14 @@ class Markov(object):
return return
for i in range(len(self.words) - 1): for i in range(len(self.words) - 1):
yield (self.words[i], self.words[i+1]) yield (self.words[i], self.words[i + 1])
def triples(self): def triples(self):
if len(self.words) < 3: if len(self.words) < 3:
return return
for i in range(len(self.words) - 2): for i in range(len(self.words) - 2):
yield (self.words[i], self.words[i+1], self.words[i+2]) yield (self.words[i], self.words[i + 1], self.words[i + 2])
def database(self): def database(self):
for w1, w2, w3 in self.triples(): for w1, w2, w3 in self.triples():
@ -39,13 +39,13 @@ class Markov(object):
def generate_text(self, size=25): def generate_text(self, size=25):
seed = random.randint(0, self.word_size - 3) seed = random.randint(0, self.word_size - 3)
seed_word, next_word = self.words[seed], self.words[seed+1] seed_word, next_word = self.words[seed], self.words[seed + 1]
w1, w2 = seed_word, next_word w1, w2 = seed_word, next_word
gen_words = [] gen_words = []
for i in range(size): for i in range(size):
gen_words.append(w1) gen_words.append(w1)
try: try:
w1, w2 = w2, random.choice(self.cache[(w1,w2)]) w1, w2 = w2, random.choice(self.cache[(w1, w2)])
except KeyError: except KeyError:
break break
gen_words.append(w1) gen_words.append(w1)