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:
parent
998ec2bf3a
commit
47fa9e95cf
@ -5,8 +5,6 @@
|
||||
# Copyright 2018 Zac Herd
|
||||
# Licensed under BSD 3-clause License, see LICENSE.md for more info
|
||||
|
||||
|
||||
|
||||
# IMPORTS
|
||||
import os
|
||||
import asyncio
|
||||
@ -14,13 +12,11 @@ import subprocess
|
||||
import discord
|
||||
import urllib.request
|
||||
|
||||
|
||||
# LOCAL IMPORTS
|
||||
from common import *
|
||||
from helpers import *
|
||||
|
||||
|
||||
|
||||
# COMMAND IMPLEMENTATINS
|
||||
@asyncio.coroutine
|
||||
def cmd_die(client, msg):
|
||||
@ -64,6 +60,7 @@ def cmd_avatar(client, msg):
|
||||
|
||||
yield from discord_send(client, msg, response)
|
||||
|
||||
|
||||
# COMMAND HANDLING
|
||||
admincommands = {
|
||||
"die": cmd_die,
|
||||
|
26
bot.py
26
bot.py
@ -1,4 +1,3 @@
|
||||
|
||||
# Maki
|
||||
# ----
|
||||
# Discord bot by MrDetonia
|
||||
@ -6,8 +5,6 @@
|
||||
# Copyright 2018 Zac Herd
|
||||
# Licensed under BSD 3-clause License, see LICENSE.md for more info
|
||||
|
||||
|
||||
|
||||
# IMPORTS
|
||||
import discord
|
||||
import asyncio
|
||||
@ -33,7 +30,6 @@ from admincommands import *
|
||||
# file in this directory called "secret.py" should contain these variables
|
||||
from secret import token, lfmkey, steamkey
|
||||
|
||||
|
||||
# DISCORD CLIENT INSTANCE
|
||||
client = discord.Client()
|
||||
|
||||
@ -49,7 +45,7 @@ def on_ready():
|
||||
print('ID: ' + client.user.id)
|
||||
|
||||
# set "Now Playing" to print version
|
||||
game = discord.Game(name = version)
|
||||
game = discord.Game(name=version)
|
||||
yield from client.change_presence(game=game)
|
||||
|
||||
|
||||
@ -60,15 +56,20 @@ def on_message(msg):
|
||||
# print messages to terminal for info
|
||||
timestr = time.strftime('%Y-%m-%d-%H:%M:%S: ')
|
||||
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:
|
||||
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
|
||||
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
|
||||
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:
|
||||
json.dump(history, fp)
|
||||
|
||||
@ -76,9 +77,11 @@ def on_message(msg):
|
||||
filters = ['`', 'http://', 'https://']
|
||||
if not any(x in msg.content for x in filters):
|
||||
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)
|
||||
except PermissionError: pass
|
||||
except PermissionError:
|
||||
pass
|
||||
|
||||
# react to stuff
|
||||
yield from makireacts(client, msg)
|
||||
@ -98,5 +101,6 @@ def main():
|
||||
client.run(token)
|
||||
exit(0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
128
commands.py
128
commands.py
@ -5,8 +5,6 @@
|
||||
# Copyright 2018 Zac Herd
|
||||
# Licensed under BSD 3-clause License, see LICENSE.md for more info
|
||||
|
||||
|
||||
|
||||
# IMPORTS
|
||||
import asyncio
|
||||
import os
|
||||
@ -16,7 +14,6 @@ import requests
|
||||
import random
|
||||
import subprocess
|
||||
|
||||
|
||||
# LOCAL IMPORTS
|
||||
from common import *
|
||||
from helpers import *
|
||||
@ -24,7 +21,6 @@ from secret import lfmkey, steamkey
|
||||
import markov
|
||||
|
||||
|
||||
|
||||
# COMMAND IMPLEMENTATIONS
|
||||
@asyncio.coroutine
|
||||
def cmd_help(client, msg):
|
||||
@ -33,9 +29,11 @@ def cmd_help(client, msg):
|
||||
|
||||
@asyncio.coroutine
|
||||
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()
|
||||
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)
|
||||
|
||||
|
||||
@ -50,7 +48,9 @@ whoistring = "**{}#{}**: `{}`\n**Account Created:** `{}`"
|
||||
|
||||
@asyncio.coroutine
|
||||
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)
|
||||
|
||||
|
||||
@ -62,7 +62,8 @@ def cmd_whois(client, msg):
|
||||
if user == None:
|
||||
reponse = "I can't find `{}`".format(tmp)
|
||||
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)
|
||||
|
||||
@ -79,7 +80,9 @@ def cmd_seen(client, msg):
|
||||
else:
|
||||
target = msg.server.id + user.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:
|
||||
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)
|
||||
else:
|
||||
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:
|
||||
reponse = "I can't find `{}`".format(tmp)
|
||||
|
||||
@ -122,11 +126,11 @@ def cmd_markov(client, msg):
|
||||
mfile = "./markovs/" + target
|
||||
if os.path.isfile(mfile):
|
||||
mc = markov.Markov(open(mfile))
|
||||
response = mc.generate_text(random.randint(20,40))
|
||||
response = mc.generate_text(random.randint(20, 40))
|
||||
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
|
||||
@ -136,33 +140,42 @@ def cmd_roll(client, msg):
|
||||
pattern = re.compile("^([0-9]+)d([0-9]+)$")
|
||||
pattern2 = re.compile("^d([0-9]+)$")
|
||||
|
||||
if pattern.match(tmp):
|
||||
# extract numbers
|
||||
nums = [int(s) for s in re.findall(r"\d+", tmp)]
|
||||
|
||||
# limit ranges
|
||||
nums[0] = clamp(nums[0], 1,100)
|
||||
nums[1] = clamp(nums[1], 1, 1000000)
|
||||
|
||||
# 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)
|
||||
if pattern.match(tmp):
|
||||
numdice = nums[0]
|
||||
diceval = nums[1]
|
||||
elif pattern2.match(tmp):
|
||||
# extract number
|
||||
num = [int(s) for s in re.findall(r"\d+", tmp)]
|
||||
|
||||
# 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)
|
||||
numdice = 1
|
||||
diceval = nums[0]
|
||||
else:
|
||||
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)
|
||||
|
||||
@ -174,10 +187,14 @@ def cmd_qr(client, msg):
|
||||
yield from discord_typing(client, msg)
|
||||
|
||||
# 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.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()
|
||||
|
||||
@ -209,16 +226,22 @@ def cmd_steam(client, msg):
|
||||
yield from discord_send(client, msg, response)
|
||||
|
||||
|
||||
|
||||
# HELPER FUNCTIONS
|
||||
|
||||
|
||||
# gets now playing information from last.fm
|
||||
def lastfm_np(username):
|
||||
# sanitise username
|
||||
cleanusername = re.sub(r'[^a-zA-Z0-9_-]', '', username, 0)
|
||||
|
||||
# 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)
|
||||
|
||||
# read json data
|
||||
@ -237,7 +260,8 @@ def lastfm_np(username):
|
||||
song = track['name']
|
||||
nowplaying = '@attr' in track
|
||||
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
|
||||
if album != "":
|
||||
@ -251,7 +275,8 @@ def lastfm_np(username):
|
||||
nowplaying = " last listened"
|
||||
|
||||
# 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
|
||||
@ -264,7 +289,8 @@ def steamdata(vanityname):
|
||||
|
||||
# fetch json from steam
|
||||
try:
|
||||
idresponse = requests.get(resolveurl + steamkey + '&vanityurl=' + vanityname).json()['response']
|
||||
idresponse = requests.get(resolveurl + steamkey + '&vanityurl=' +
|
||||
vanityname).json()['response']
|
||||
except:
|
||||
return "I can't connect to Steam"
|
||||
|
||||
@ -276,29 +302,35 @@ def steamdata(vanityname):
|
||||
|
||||
# fetch steam user info
|
||||
try:
|
||||
dataresponse = requests.get(dataurl + steamkey + '&steamids=' + steamid).json()['response']['players'][0]
|
||||
dataresponse = requests.get(dataurl + steamkey + '&steamids=' +
|
||||
steamid).json()['response']['players'][0]
|
||||
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']
|
||||
else: namestr = ''
|
||||
if 'personastate' in dataresponse: statestr = '`' + personastates[dataresponse['personastate']] + '`'
|
||||
else: statestr = ''
|
||||
if 'gameextrainfo' in dataresponse: gamestr = ' playing `' + dataresponse['gameextrainfo'] + '`'
|
||||
else: gamestr = ''
|
||||
if 'personastate' in dataresponse:
|
||||
statestr = '`' + personastates[dataresponse['personastate']] + '`'
|
||||
else:
|
||||
statestr = ''
|
||||
if 'gameextrainfo' in dataresponse:
|
||||
gamestr = ' playing `' + dataresponse['gameextrainfo'] + '`'
|
||||
else:
|
||||
gamestr = ''
|
||||
|
||||
responsetext = [(namestr + ' is ' + statestr + gamestr).replace(' ', ' ')]
|
||||
|
||||
return '\n'.join(responsetext)
|
||||
|
||||
|
||||
|
||||
# COMMAND HANDLING
|
||||
prefix = "."
|
||||
|
||||
|
||||
commands = {
|
||||
"help": cmd_help,
|
||||
"info": cmd_info,
|
||||
|
@ -5,16 +5,12 @@
|
||||
# Copyright 2018 Zac Herd
|
||||
# Licensed under BSD 3-clause License, see LICENSE.md for more info
|
||||
|
||||
|
||||
|
||||
# IMPORTS
|
||||
import os
|
||||
import json
|
||||
|
||||
|
||||
# bot version
|
||||
version = "v1.0.10"
|
||||
|
||||
version = "v1.1.0"
|
||||
|
||||
# TODO: generate this on the fly and make it look acceptable
|
||||
# text shown by .help command
|
||||
|
13
helpers.py
13
helpers.py
@ -5,7 +5,6 @@
|
||||
# Copyright 2018 Zac Herd
|
||||
# Licensed under BSD 3-clause License, see LICENSE.md for more info
|
||||
|
||||
|
||||
# IMPORTS
|
||||
import asyncio
|
||||
import discord
|
||||
@ -18,7 +17,8 @@ from common import *
|
||||
|
||||
|
||||
# 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
|
||||
@ -35,8 +35,10 @@ def dtfromts(ts):
|
||||
def logger():
|
||||
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'))
|
||||
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)
|
||||
|
||||
|
||||
@ -66,7 +68,8 @@ def discord_typing(client, message):
|
||||
else:
|
||||
break
|
||||
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...
|
||||
|
10
markov.py
10
markov.py
@ -1,7 +1,7 @@
|
||||
import random
|
||||
|
||||
class Markov(object):
|
||||
|
||||
class Markov(object):
|
||||
def __init__(self, open_file):
|
||||
self.cache = {}
|
||||
self.open_file = open_file
|
||||
@ -20,14 +20,14 @@ class Markov(object):
|
||||
return
|
||||
|
||||
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):
|
||||
if len(self.words) < 3:
|
||||
return
|
||||
|
||||
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):
|
||||
for w1, w2, w3 in self.triples():
|
||||
@ -39,13 +39,13 @@ class Markov(object):
|
||||
|
||||
def generate_text(self, size=25):
|
||||
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
|
||||
gen_words = []
|
||||
for i in range(size):
|
||||
gen_words.append(w1)
|
||||
try:
|
||||
w1, w2 = w2, random.choice(self.cache[(w1,w2)])
|
||||
w1, w2 = w2, random.choice(self.cache[(w1, w2)])
|
||||
except KeyError:
|
||||
break
|
||||
gen_words.append(w1)
|
||||
|
Loading…
Reference in New Issue
Block a user