1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-12-24 14:27:27 +00:00

Added prefixes dict to settings file

This commit is contained in:
Myned 2017-10-20 16:23:01 -04:00
parent c1a0f5850d
commit 8a0ba12f20

View file

@ -20,49 +20,49 @@ print('\nPID : {}\n'.format(os.getpid()))
try: try:
with open('config.json') as infile: with open('config.json') as infile:
config = jsn.load(infile) config = jsn.load(infile)
print('LOADED : config.json') print('LOADED : config.json')
except FileNotFoundError: except FileNotFoundError:
with open('config.json', 'w') as outfile: with open('config.json', 'w') as outfile:
jsn.dump({'client_id': 0, 'info_channel': 0, 'owner_id': 0, 'permissions': 126016, jsn.dump({'client_id': 0, 'info_channel': 0, 'owner_id': 0, 'permissions': 126016,
'playing': 'a game', 'prefix': [',', 'm,'], 'token': 'str'}, outfile, indent=4, sort_keys=True) 'playing': 'a game', 'prefix': [',', 'm,'], 'token': 'str'}, outfile, indent=4, sort_keys=True)
raise FileNotFoundError( raise FileNotFoundError(
'FILE NOT FOUND : config.json created with abstract values. Restart run.py with correct values') 'FILE NOT FOUND : config.json created with abstract values. Restart run.py with correct values')
def setdefault(filename, default=None): def setdefault(filename, default=None):
try: try:
with open(filename, 'rb') as infile: with open(filename, 'rb') as infile:
print('LOADED : {}'.format(filename)) print('LOADED : {}'.format(filename))
return pkl.load(infile) return pkl.load(infile)
except FileNotFoundError: except FileNotFoundError:
with open(filename, 'wb+') as iofile: with open(filename, 'wb+') as iofile:
print('FILE NOT FOUND : {} created and loaded with default values'.format(filename)) print('FILE NOT FOUND : {} created and loaded with default values'.format(filename))
pkl.dump(default, iofile) pkl.dump(default, iofile)
iofile.seek(0) iofile.seek(0)
return pkl.load(iofile) return pkl.load(iofile)
def load(filename, *, json=False): def load(filename, *, json=False):
if not json: if not json:
with open(filename, 'rb') as infile: with open(filename, 'rb') as infile:
return pkl.load(infile) return pkl.load(infile)
else: else:
with open(filename) as infile: with open(filename) as infile:
return jsn.load(infile) return jsn.load(infile)
def dump(obj, filename, *, json=False): def dump(obj, filename, *, json=False):
if not json: if not json:
with open(filename, 'wb') as outfile: with open(filename, 'wb') as outfile:
pkl.dump(obj, outfile) pkl.dump(obj, outfile)
else: else:
with open(filename, 'w') as outfile: with open(filename, 'w') as outfile:
jsn.dump(obj, outfile, indent=4, sort_keys=True) jsn.dump(obj, outfile, indent=4, sort_keys=True)
settings = setdefault('settings.pkl', {'del_ctx': []}) settings = setdefault('settings.pkl', {'del_ctx': [], 'prefixes': {}})
tasks = setdefault('cogs/tasks.pkl', {'auto_del': [], 'auto_qual': [], 'auto_rev': []}) tasks = setdefault('cogs/tasks.pkl', {'auto_del': [], 'auto_qual': [], 'auto_rev': []})
temp = setdefault('temp.pkl', {}) temp = setdefault('temp.pkl', {})
@ -87,62 +87,62 @@ session = aiohttp.ClientSession()
def close(loop): def close(loop):
global session global session
if session: if session:
session.close() session.close()
loop.stop() loop.stop()
pending = asyncio.Task.all_tasks() pending = asyncio.Task.all_tasks()
for task in pending: for task in pending:
task.cancel() task.cancel()
# with suppress(asyncio.CancelledError): # with suppress(asyncio.CancelledError):
# loop.run_until_complete(task) # loop.run_until_complete(task)
# loop.close() # loop.close()
print('Finished cancelling tasks.') print('Finished cancelling tasks.')
async def fetch(url, *, params={}, json=False): async def fetch(url, *, params={}, json=False):
global session global session
async with session.get(url, params=params, headers={'User-Agent': 'Myned/Modumind/0.0.1'}) as r: async with session.get(url, params=params, headers={'User-Agent': 'Myned/Modumind/0.0.1'}) as r:
if json: if json:
return await r.json() return await r.json()
return await r.read() return await r.read()
# def geneate_embed(**kwargs): # def geneate_embed(**kwargs):
# embed = d.Embed(title=kwargs['title'], ) # embed = d.Embed(title=kwargs['title'], )
def get_kwargs(ctx, args, *, limit=False): def get_kwargs(ctx, args, *, limit=False):
destination = ctx destination = ctx
remaining = list(args[:]) remaining = list(args[:])
rm = False rm = False
lim = 1 lim = 1
if '-d' in remaining or '-dm' in remaining: if '-d' in remaining or '-dm' in remaining:
destination = ctx.author destination = ctx.author
for flag in ('-d', '-dm'): for flag in ('-d', '-dm'):
with suppress(ValueError): with suppress(ValueError):
remaining.remove(flag) remaining.remove(flag)
if ('-r' in remaining or '-rm' in remaining or '-remove' in remaining) and ctx.author.permissions_in(ctx.channel).manage_messages: if ('-r' in remaining or '-rm' in remaining or '-remove' in remaining) and ctx.author.permissions_in(ctx.channel).manage_messages:
rm = True rm = True
for flag in ('-r', '-rm', '-remove'): for flag in ('-r', '-rm', '-remove'):
with suppress(ValueError): with suppress(ValueError):
remaining.remove(flag) remaining.remove(flag)
if limit: if limit:
for arg in remaining: for arg in remaining:
if arg.isdigit(): if arg.isdigit():
if 1 <= int(arg) <= limit: if 1 <= int(arg) <= limit:
lim = int(arg) lim = int(arg)
remaining.remove(arg) remaining.remove(arg)
break break
else: else:
raise exc.BoundsError(arg) raise exc.BoundsError(arg)
return {'destination': destination, 'remaining': remaining, 'remove': rm, 'limit': lim} return {'destination': destination, 'remaining': remaining, 'remove': rm, 'limit': lim}