2017-10-13 02:30:07 +00:00
|
|
|
import asyncio
|
2017-10-16 18:06:31 +00:00
|
|
|
import json as jsn
|
2017-10-14 03:59:14 +00:00
|
|
|
import os
|
2017-10-13 02:30:07 +00:00
|
|
|
import pickle as pkl
|
2017-10-15 01:53:55 +00:00
|
|
|
import subprocess
|
2017-10-15 16:58:16 +00:00
|
|
|
from contextlib import suppress
|
2017-10-13 02:30:07 +00:00
|
|
|
|
2017-10-15 01:53:55 +00:00
|
|
|
import aiohttp
|
2017-10-16 06:06:08 +00:00
|
|
|
import discord as d
|
2017-10-15 01:53:55 +00:00
|
|
|
from pync import Notifier
|
|
|
|
|
2017-10-18 00:28:56 +00:00
|
|
|
from misc import exceptions as exc
|
|
|
|
|
2017-10-15 01:53:55 +00:00
|
|
|
print('\nPID : {}\n'.format(os.getpid()))
|
|
|
|
|
|
|
|
|
|
|
|
# def notify(message):
|
|
|
|
# subprocess.run(['terminal-notifier', '-message', message, '-title',
|
|
|
|
# 'Modumind', '-activate', 'com.apple.Terminal', '-appIcon', 'icon.png', '-sound', 'Ping'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2017-10-13 02:30:07 +00:00
|
|
|
|
2017-10-14 03:59:14 +00:00
|
|
|
|
|
|
|
try:
|
2017-10-17 23:04:45 +00:00
|
|
|
with open('config.json') as infile:
|
|
|
|
config = jsn.load(infile)
|
2017-10-20 20:20:33 +00:00
|
|
|
print('LOADED : config.json')
|
2017-10-14 03:59:14 +00:00
|
|
|
except FileNotFoundError:
|
2017-10-17 23:04:45 +00:00
|
|
|
with open('config.json', 'w') as outfile:
|
|
|
|
jsn.dump({'client_id': 0, 'info_channel': 0, 'owner_id': 0, 'permissions': 126016,
|
2017-10-18 00:46:43 +00:00
|
|
|
'playing': 'a game', 'prefix': [',', 'm,'], 'token': 'str'}, outfile, indent=4, sort_keys=True)
|
2017-10-17 23:04:45 +00:00
|
|
|
raise FileNotFoundError(
|
2017-10-20 20:20:33 +00:00
|
|
|
'FILE NOT FOUND : config.json created with abstract values. Restart run.py with correct values')
|
2017-10-14 03:59:14 +00:00
|
|
|
|
2017-10-13 02:30:07 +00:00
|
|
|
|
|
|
|
def setdefault(filename, default=None):
|
2017-10-17 23:04:45 +00:00
|
|
|
try:
|
|
|
|
with open(filename, 'rb') as infile:
|
2017-10-20 20:20:33 +00:00
|
|
|
print('LOADED : {}'.format(filename))
|
2017-10-17 23:04:45 +00:00
|
|
|
return pkl.load(infile)
|
|
|
|
except FileNotFoundError:
|
|
|
|
with open(filename, 'wb+') as iofile:
|
2017-10-20 20:20:33 +00:00
|
|
|
print('FILE NOT FOUND : {} created and loaded with default values'.format(filename))
|
2017-10-17 23:04:45 +00:00
|
|
|
pkl.dump(default, iofile)
|
|
|
|
iofile.seek(0)
|
|
|
|
return pkl.load(iofile)
|
2017-10-13 02:30:07 +00:00
|
|
|
|
|
|
|
|
2017-10-16 18:06:31 +00:00
|
|
|
def load(filename, *, json=False):
|
2017-10-17 23:04:45 +00:00
|
|
|
if not json:
|
|
|
|
with open(filename, 'rb') as infile:
|
|
|
|
return pkl.load(infile)
|
|
|
|
else:
|
|
|
|
with open(filename) as infile:
|
|
|
|
return jsn.load(infile)
|
2017-10-16 18:06:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dump(obj, filename, *, json=False):
|
2017-10-17 23:04:45 +00:00
|
|
|
if not json:
|
|
|
|
with open(filename, 'wb') as outfile:
|
|
|
|
pkl.dump(obj, outfile)
|
|
|
|
else:
|
|
|
|
with open(filename, 'w') as outfile:
|
|
|
|
jsn.dump(obj, outfile, indent=4, sort_keys=True)
|
2017-10-11 06:54:32 +00:00
|
|
|
|
2017-10-13 02:30:07 +00:00
|
|
|
|
2017-10-15 03:40:56 +00:00
|
|
|
settings = setdefault('settings.pkl', {'del_ctx': []})
|
2017-10-16 22:49:32 +00:00
|
|
|
tasks = setdefault('cogs/tasks.pkl', {'auto_del': [], 'auto_qual': [], 'auto_rev': []})
|
2017-10-17 22:10:00 +00:00
|
|
|
temp = setdefault('temp.pkl', {})
|
2017-10-11 06:54:32 +00:00
|
|
|
|
2017-10-17 03:47:35 +00:00
|
|
|
RATE_LIMIT = 2.2
|
2017-10-16 06:06:08 +00:00
|
|
|
session = aiohttp.ClientSession()
|
2017-10-11 06:54:32 +00:00
|
|
|
|
2017-10-13 02:30:07 +00:00
|
|
|
|
2017-10-16 06:06:08 +00:00
|
|
|
# async def clear(obj, interval=10 * 60, replace=None):
|
|
|
|
# if replace is None:
|
|
|
|
# if type(obj) is list:
|
|
|
|
# replace = []
|
|
|
|
# elif type(obj) is dict:
|
|
|
|
# replace = {}
|
|
|
|
# elif type(obj) is int:
|
|
|
|
# replace = 0
|
|
|
|
# elif type(obj) is str:
|
|
|
|
# replace = ''
|
|
|
|
#
|
|
|
|
# while True:
|
|
|
|
# obj = replace
|
|
|
|
# asyncio.sleep(interval)
|
2017-10-15 02:37:57 +00:00
|
|
|
|
|
|
|
|
2017-10-15 16:58:16 +00:00
|
|
|
def close(loop):
|
2017-10-17 23:04:45 +00:00
|
|
|
global session
|
2017-10-15 02:37:57 +00:00
|
|
|
|
2017-10-17 23:04:45 +00:00
|
|
|
if session:
|
|
|
|
session.close()
|
2017-10-15 02:37:57 +00:00
|
|
|
|
2017-10-17 23:04:45 +00:00
|
|
|
loop.stop()
|
|
|
|
pending = asyncio.Task.all_tasks()
|
|
|
|
for task in pending:
|
|
|
|
task.cancel()
|
|
|
|
# with suppress(asyncio.CancelledError):
|
|
|
|
# loop.run_until_complete(task)
|
|
|
|
# loop.close()
|
2017-10-15 16:58:16 +00:00
|
|
|
|
2017-10-17 23:04:45 +00:00
|
|
|
print('Finished cancelling tasks.')
|
2017-10-15 16:58:16 +00:00
|
|
|
|
2017-10-13 02:30:07 +00:00
|
|
|
|
|
|
|
async def fetch(url, *, params={}, json=False):
|
2017-10-17 23:04:45 +00:00
|
|
|
global session
|
2017-10-15 02:37:57 +00:00
|
|
|
|
2017-10-17 23:04:45 +00:00
|
|
|
async with session.get(url, params=params, headers={'user-agent': 'Modumind/0.0.1 (Myned)'}) as r:
|
|
|
|
if json:
|
|
|
|
return await r.json()
|
|
|
|
return await r.read()
|
2017-10-16 06:06:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# def geneate_embed(**kwargs):
|
|
|
|
# embed = d.Embed(title=kwargs['title'], )
|
2017-10-17 21:59:44 +00:00
|
|
|
|
2017-10-18 00:13:40 +00:00
|
|
|
def get_kwargs(ctx, args, *, limit=False):
|
2017-10-17 23:04:45 +00:00
|
|
|
destination = ctx
|
|
|
|
remaining = list(args[:])
|
2017-10-18 00:13:40 +00:00
|
|
|
rm = False
|
|
|
|
lim = 1
|
2017-10-17 23:04:45 +00:00
|
|
|
|
|
|
|
if '-d' in remaining or '-dm' in remaining:
|
|
|
|
destination = ctx.author
|
2017-10-17 21:59:44 +00:00
|
|
|
|
2017-10-19 08:39:18 +00:00
|
|
|
for flag in ('-d', '-dm'):
|
|
|
|
with suppress(ValueError):
|
|
|
|
remaining.remove(flag)
|
2017-10-17 21:59:44 +00:00
|
|
|
|
2017-10-18 00:13:40 +00:00
|
|
|
if ('-r' in remaining or '-rm' in remaining or '-remove' in remaining) and ctx.author.permissions_in(ctx.channel).manage_messages:
|
|
|
|
rm = True
|
2017-10-17 23:04:45 +00:00
|
|
|
|
2017-10-19 08:39:18 +00:00
|
|
|
for flag in ('-r', '-rm', '-remove'):
|
|
|
|
with suppress(ValueError):
|
|
|
|
remaining.remove(flag)
|
2017-10-17 21:59:44 +00:00
|
|
|
|
2017-10-18 00:13:40 +00:00
|
|
|
if limit:
|
2017-10-17 23:04:45 +00:00
|
|
|
for arg in remaining:
|
2017-10-20 20:20:47 +00:00
|
|
|
if arg.isdigit():
|
2017-10-18 00:13:40 +00:00
|
|
|
if 1 <= int(arg) <= limit:
|
|
|
|
lim = int(arg)
|
2017-10-17 23:04:45 +00:00
|
|
|
remaining.remove(arg)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise exc.BoundsError(arg)
|
|
|
|
|
2017-10-18 00:13:40 +00:00
|
|
|
return {'destination': destination, 'remaining': remaining, 'remove': rm, 'limit': lim}
|