Migration to rewrite

some performance chances with message caching/fetching and user fetching
This commit is contained in:
Matthias Nadler committed 2019-04-12 20:30:36 +02:00
1 parent 8d183eeb54
commit dae2a6e494
16 files changed
+565 -699

No files matched your search

-11
View File
@@ -1,11 +0,0 @@
import asyncio
class UniqueQueue(asyncio.Queue):
async def put_unique_id(self, item):
if not item.get('id'):
return
if item.get('id') not in [v.get('id') for v in self._queue]:
await self.put(item)
-96
View File
@@ -1,96 +0,0 @@
import datetime
import json
import os
import pytz
async def import_old_database(bot, server):
"""try to import the old database"""
try:
clean_server = str(server).replace("/", "")
while clean_server.startswith("."):
clean_server = clean_server[1:]
fn = 'backup/' + clean_server + '.json'
with open(fn, 'r') as infile:
polls = json.load(infile)
for p in polls:
#print(polls[p]['short'])
wr = []
wn = []
for r, n in polls[p]['weights'].items():
wr.append(r)
try:
if n.is_integer():
n = int(n)
except:
pass
wn.append(n)
created = datetime.datetime.strptime(polls[p]['datestarted'], '%d-%m-%Y %H:%M').replace(tzinfo=pytz.utc)
if polls[p]['duration'] == 0:
duration = 0
else:
duration = created + datetime.timedelta(hours=float(polls[p]['duration']))
votes = {}
for u,o in polls[p]['votes'].items():
# get weight
user = server.get_member(u)
weight = 1
if wr.__len__() > 0:
valid_weights = [wn[wr.index(r)] for r in
list(set([n.name for n in user.roles]).intersection(set(wr)))]
if valid_weights.__len__() > 0:
#print(wr, wn)
weight = max(valid_weights)
choices = []
if o in polls[p]['options']:
choices = [polls[p]['options'].index(o)]
votes[u] = {'weight': weight, 'choices': choices}
new_format = {
'server_id': str(server.id),
'channel_id': str(polls[p]['channel']),
'author': str(polls[p]['author']),
'name': polls[p]['name'],
'short': polls[p]['short'],
'anonymous': polls[p]['anonymous'],
'reaction': True,
'multiple_choice': False,
'options_reaction': polls[p]['options'],
'reaction_default': False,
'roles': polls[p]['roles'],
'weights_roles': wr,
'weights_numbers': wn,
'duration': duration,
'duration_tz': 'UTC',
'time_created': created,
'open': polls[p]['open'],
'active': True,
'activation': 0,
'activation_tz': 'UTC',
'votes': votes
}
await bot.db.polls.update_one({'server_id': str(server.id), 'short': polls[p]['short']},
{'$set': new_format}, upsert=True)
os.remove(fn)
except FileNotFoundError:
pass
except Exception as e:
print(e)
# potential update message
# text = "**Dear Server Admin!**\n\n" \
# "After more than a year in the field, today Pollmaster received it's first big update and I am excited to present you the new Version!\n\n" \
# "**TL;DR: A massive overhaul of every function. The new (now customizable) prefix is `pm!` and you can find the rest of the commands with `pm!help`**\n\n" \
# "**Here are some more highlights:**\n" \
# "🔹 Voting is no longer done per text, but by using reactions\n" \
# "🔹 Creating new polls is now an interactive process instead of command lines\n" \
# "🔹 There is now a settings for multiple choice polls\n" \
# "🔹 You can use all the commands in a private message with Pollmaster to reduce spam in your channels\n\n" \
# "For the full changelog, please visit: https://github.com/matnad/pollmaster/blob/master/changelog.md\n" \
# "All your old polls should be converted to the new format and accessible with `pm!show` or `pm!show closed`.\n" \
# "If you have any problems or questions, please join the support discord: https://discord.gg/Vgk8Nve\n\n" \
# "**No action from you is required. But you might want to update permissions for your users. " \
# "See pm!help -> configuration**\n\n" \
# "I hope you enjoy the new Pollmaster!\n" \
# "Regards, Newti#0654"
+25 -17
View File
@@ -1,4 +1,9 @@
async def embed_list_paginated(bot, pre, items, item_fct, base_embed, footer_prefix='', msg=None, start=0, per_page=10):
import asyncio
import discord
async def embed_list_paginated(ctx, bot, pre, items, item_fct, base_embed, footer_prefix='', msg=None, start=0, per_page=10):
embed = base_embed
# generate list
@@ -21,27 +26,30 @@ async def embed_list_paginated(bot, pre, items, item_fct, base_embed, footer_pre
# post / edit message
if msg is not None:
await bot.edit_message(msg, embed=embed)
if str(msg.channel.type) != 'private':
await bot.clear_reactions(msg)
await msg.edit(embed=embed)
if not isinstance(msg.channel, discord.abc.PrivateChannel):
await msg.clear_reactions()
else:
msg = await bot.say(embed=embed)
msg = await ctx.send(embed=embed)
# add reactions
if start > 0:
await bot.add_reaction(msg, '⏪')
await msg.add_reaction('⏪')
if items.__len__() > start+per_page:
await bot.add_reaction(msg, '⏩')
await msg.add_reaction('⏩')
# wait for reactions (2 minutes)
def check(reaction, user):
return reaction.emoji if user != bot.user else False
res = await bot.wait_for_reaction(emoji=['⏪', '⏩'], message=msg, timeout=120, check=check)
# redirect on reaction
if res is None:
return
elif res.reaction.emoji == '⏪' and start > 0:
await embed_list_paginated(bot, pre, items, item_fct, base_embed, footer_prefix=footer_prefix, msg=msg, start=start-per_page, per_page=per_page)
elif res.reaction.emoji == '⏩' and items.__len__() > start+per_page:
await embed_list_paginated(bot, pre, items, item_fct, base_embed, footer_prefix=footer_prefix, msg=msg, start=start+per_page, per_page=per_page)
return True if user != bot.user and str(reaction.emoji) in ['⏪', '⏩'] and reaction.message.id == msg.id else False
try:
reaction, user = await bot.wait_for('reaction_add', timeout=120, check=check)
except asyncio.TimeoutError:
pass
else:
# redirect on reaction
if reaction is None:
return
elif reaction.emoji == '⏪' and start > 0:
await embed_list_paginated(ctx, bot, pre, items, item_fct, base_embed, footer_prefix=footer_prefix, msg=msg, start=start-per_page, per_page=per_page)
elif reaction.emoji == '⏩' and items.__len__() > start+per_page:
await embed_list_paginated(ctx, bot, pre, items, item_fct, base_embed, footer_prefix=footer_prefix, msg=msg, start=start+per_page, per_page=per_page)
+3 -3
View File
@@ -6,9 +6,9 @@ animals = ['frog', 'newt', 'tadpole', 'toad', 'spider', 'biddy', 'canary', 'crow
async def generate_word(bot, server_id):
exists = 1
while exists is not None:
ad = random.sample(adjs, 2)
ad = random.sample(adjs, 1)
an = random.sample(animals, 1)
short = ad[0].capitalize() + ad[1].capitalize() + an[0].capitalize()
exists = await bot.db.polls.find_one({'server_id': str(server_id), 'short': short})
short = ad[0].capitalize() + an[0].capitalize()
exists = await bot.db.polls.find_one({'server_id': server_id, 'short': short})
return short