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,13 +12,11 @@ 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):
@ -64,6 +60,7 @@ def cmd_avatar(client, msg):
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,

24
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()
@ -60,15 +56,20 @@ 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,
msg.channel.name, msg.author.name,
msg.content))
except AttributeError: except AttributeError:
print("{} PRIVATE | {}: {}".format(timestr, msg.author.name, msg.content)) 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(
msg.channel) is not discord.PrivateChannel:
# log each message against users # log each message against users
if msg.content != "": if msg.content != "":
history[msg.server.id + msg.author.id] = (msg.server.id, time.time(), msg.content) history[msg.server.id + msg.author.id] = (msg.server.id,
time.time(), msg.content)
with open('hist.json', 'w') as fp: with open('hist.json', 'w') as fp:
json.dump(history, fp) json.dump(history, fp)
@ -76,9 +77,11 @@ def on_message(msg):
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,
'a') as fp:
fp.write('\n' + msg.content) fp.write('\n' + msg.content)
except PermissionError: pass except PermissionError:
pass
# react to stuff # react to stuff
yield from makireacts(client, msg) yield from makireacts(client, msg)
@ -98,5 +101,6 @@ def main():
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,7 +21,6 @@ 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):
@ -33,9 +29,11 @@ def cmd_help(client, msg):
@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],
sys.version_info[2])
appinfo = yield from client.application_info() appinfo = yield from client.application_info()
response = "I am **{}**, a Discord bot by **{}** | `{}` | Python `{}` | discord.py `{}`".format(appinfo.name, appinfo.owner.name, version, pyver, discord.__version__) 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) yield from discord_send(client, msg, response)
@ -50,7 +48,9 @@ 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,
msg.author.discriminator, msg.author.id,
strfromdt(msg.author.created_at))
yield from discord_send(client, msg, response) yield from discord_send(client, msg, response)
@ -62,7 +62,8 @@ def cmd_whois(client, msg):
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)
@ -79,7 +80,9 @@ def cmd_seen(client, msg):
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(
user.name, strfromdt(dtfromts(history[target][1])),
history[target][2])
else: else:
response = "I haven't seen **{}** speak yet!".format(tmp) response = "I haven't seen **{}** speak yet!".format(tmp)
@ -114,7 +117,8 @@ def cmd_markov(client, msg):
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,
msg.server.get_member_named(tmp).id)
except AttributeError: except AttributeError:
reponse = "I can't find `{}`".format(tmp) reponse = "I can't find `{}`".format(tmp)
@ -126,7 +130,7 @@ def cmd_markov(client, msg):
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
@ -136,33 +140,42 @@ def cmd_roll(client, msg):
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]
# roll and sum dice
rollsum = 0
for i in range(nums[0]):
rollsum += random.randint(1, nums[1])
response = "Using `{}d{}`, {} rolled: `{}`".format(nums[0], nums[1], msg.author.display_name, rollsum)
elif pattern2.match(tmp): elif pattern2.match(tmp):
# extract number numdice = 1
num = [int(s) for s in re.findall(r"\d+", tmp)] diceval = nums[0]
# limit range
num[0] = clamp(num[0], 1, 1000000)
# roll dice
roll = random.randint(1, num[0])
response = "Using `1d{}`, {} rolled `{}`".format(num[0], msg.author.display_name, roll)
else: else:
response = "Expected format: `[<num>]d<value>`" response = "Expected format: `[<num>]d<value>`"
yield from discord_send(client, msg, response)
# limit ranges
numdice = clamp(numdice, 1, 10)
diceval = clamp(diceval, 1, 1000)
# roll and sum dice
rolls = []
for i in range(numdice):
rolls.append(random.randint(1, diceval))
rollsum = sum(rolls)
response = "**{} rolled:** {}d{}".format(msg.author.display_name, numdice,
diceval)
if numdice > 1:
response += "\n**Rolls:** `{}`".format(rolls)
response += "\n**Result:** `{}`".format(rollsum)
if rollsum == numdice * diceval:
response += " *(Natural)*"
elif rollsum == numdice:
response += " *(Crit fail)*"
yield from discord_send(client, msg, response) yield from discord_send(client, msg, response)
@ -174,10 +187,14 @@ def cmd_qr(client, msg):
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(
"qrencode -t png -o -".split(),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
qr.stdin.write(tmp.encode("utf-8")) qr.stdin.write(tmp.encode("utf-8"))
qr.stdin.close() qr.stdin.close()
out = subprocess.check_output("curl -F upload=@- https://w1r3.net".split(), stdin=qr.stdout) 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()
@ -209,16 +226,22 @@ def cmd_steam(client, msg):
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 = {
'format': 'json',
'method': 'user.getRecentTracks',
'user': cleanusername,
'limit': '1',
'api_key': lfmkey
}
r = requests.get("http://ws.audioscrobbler.com/2.0/", params=payload) r = requests.get("http://ws.audioscrobbler.com/2.0/", params=payload)
# read json data # read json data
@ -237,7 +260,8 @@ def lastfm_np(username):
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 != "":
@ -251,7 +275,8 @@ def lastfm_np(username):
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
@ -264,7 +289,8 @@ def steamdata(vanityname):
# 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=' +
vanityname).json()['response']
except: except:
return "I can't connect to Steam" return "I can't connect to Steam"
@ -276,29 +302,35 @@ def steamdata(vanityname):
# 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=' +
steamid).json()['response']['players'][0]
except: except:
return "Can't find info on `{}`".format(vanityname) 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,

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

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,7 +17,8 @@ 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
@ -35,8 +35,10 @@ def dtfromts(ts):
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')
handler.setFormatter(
logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler) logger.addHandler(handler)
@ -66,7 +68,8 @@ def discord_typing(client, message):
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...

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