fix crash, improve !cmd help and errors, add database locks for high traffic servers, cache prefixes, add production/development setting
This commit is contained in:
1 parent
32a70bb01d
commit
ac1f3051ef
6 files changed
+139
-102
No files matched your search
@@ -29,6 +29,7 @@ class Config:
|
||||
f'If you would like to add a trailing whitespace to the prefix, use `{pre}prefix {pre}\w`.'
|
||||
|
||||
await self.bot.db.config.update_one({'_id': str(server.id)}, {'$set': {'prefix': str(pre)}}, upsert=True)
|
||||
self.bot.pre[str(server.id)] = str(pre)
|
||||
await self.bot.say(msg)
|
||||
|
||||
@commands.command(pass_context=True)
|
||||
|
||||
+2
-1
@@ -20,7 +20,8 @@ class DiscordBotsOrgAPI:
|
||||
while True:
|
||||
logger.info('attempting to post server count')
|
||||
try:
|
||||
await self.dblpy.post_server_count()
|
||||
if SETTINGS.mode == 'production':
|
||||
await self.dblpy.post_server_count()
|
||||
logger.info('posted server count ({})'.format(len(self.bot.servers)))
|
||||
except Exception as e:
|
||||
logger.exception('Failed to post server count\n{}: {}'.format(type(e).__name__, e))
|
||||
|
||||
+121
-96
@@ -1,4 +1,5 @@
|
||||
import argparse
|
||||
import asyncio
|
||||
import copy
|
||||
import datetime
|
||||
import json
|
||||
@@ -24,6 +25,11 @@ class PollControls:
|
||||
self.bot = bot
|
||||
|
||||
# General Methods
|
||||
def get_lock(self, server_id):
|
||||
if not self.bot.locks.get(str(server_id)):
|
||||
self.bot.locks[server_id] = asyncio.Lock()
|
||||
return self.bot.locks.get(str(server_id))
|
||||
|
||||
async def is_admin_or_creator(self, ctx, server, owner_id, error_msg=None):
|
||||
member = server.get_member(ctx.message.author.id)
|
||||
if member.id == owner_id:
|
||||
@@ -288,11 +294,23 @@ class PollControls:
|
||||
return
|
||||
|
||||
try:
|
||||
args = parser.parse_args(cmds)
|
||||
args, unknown_args = parser.parse_known_args(cmds)
|
||||
except SystemExit:
|
||||
await self.say_error(ctx, error_text=helpstring)
|
||||
return
|
||||
|
||||
if unknown_args:
|
||||
error_text = f'**There was an error reading the command line options!**.\n' \
|
||||
f'Most likely this is because you didn\'t surround the arguments with double quotes like this: ' \
|
||||
f'`{pre}cmd -q "question of the poll" -o "yes, no, maybe"`' \
|
||||
f'\n\nHere are the arguments I could not understand:\n'
|
||||
error_text += '`'+'\n'.join(unknown_args)+'`'
|
||||
error_text += f'\n\nHere are the arguments which are ok:\n'
|
||||
error_text += '`' + '\n'.join([f'{k}: {v}' for k, v in vars(args).items()]) + '`'
|
||||
|
||||
await self.say_error(ctx, error_text=error_text, footer_text=f'type `{pre}cmd help` for details.')
|
||||
return
|
||||
|
||||
# pass arguments to the wizard
|
||||
async def route(poll):
|
||||
await poll.set_name(force=args.question)
|
||||
@@ -306,7 +324,7 @@ class PollControls:
|
||||
|
||||
poll = await self.wizard(ctx, route, server)
|
||||
if poll:
|
||||
await poll.post_embed()
|
||||
await poll.post_embed(destination=poll.channel)
|
||||
|
||||
|
||||
@commands.command(pass_context=True)
|
||||
@@ -328,7 +346,7 @@ class PollControls:
|
||||
|
||||
poll = await self.wizard(ctx, route, server)
|
||||
if poll:
|
||||
await poll.post_embed()
|
||||
await poll.post_embed(destination=poll.channel)
|
||||
|
||||
@commands.command(pass_context=True)
|
||||
async def prepare(self, ctx, *, cmd=None):
|
||||
@@ -371,7 +389,7 @@ class PollControls:
|
||||
|
||||
poll = await self.wizard(ctx, route, server)
|
||||
if poll:
|
||||
await poll.post_embed()
|
||||
await poll.post_embed(destination=poll.channel)
|
||||
|
||||
# The Wizard!
|
||||
async def wizard(self, ctx, route, server):
|
||||
@@ -460,14 +478,17 @@ class PollControls:
|
||||
user_msg = copy.deepcopy(message)
|
||||
user_msg.author = user
|
||||
server = await ask_for_server(self.bot, user_msg, label)
|
||||
p = await Poll.load_from_db(self.bot, server.id, label)
|
||||
if not isinstance(p, Poll):
|
||||
return
|
||||
|
||||
if not p.anonymous:
|
||||
# for anonymous polls we can't unvote because we need to hide reactions
|
||||
member = server.get_member(user_id)
|
||||
await p.unvote(member, emoji, message)
|
||||
# this is exclusive
|
||||
async with self.get_lock(server.id):
|
||||
p = await Poll.load_from_db(self.bot, server.id, label)
|
||||
if not isinstance(p, Poll):
|
||||
return
|
||||
|
||||
if not p.anonymous:
|
||||
# for anonymous polls we can't unvote because we need to hide reactions
|
||||
member = server.get_member(user_id)
|
||||
await p.unvote(member, emoji, message)
|
||||
|
||||
|
||||
async def do_on_reaction_add(self, data):
|
||||
@@ -510,103 +531,107 @@ class PollControls:
|
||||
user_msg = copy.deepcopy(message)
|
||||
user_msg.author = user
|
||||
server = await ask_for_server(self.bot, user_msg, label)
|
||||
p = await Poll.load_from_db(self.bot, server.id, label)
|
||||
if not isinstance(p, Poll):
|
||||
return
|
||||
|
||||
# export
|
||||
if emoji == '📎':
|
||||
# sending file
|
||||
file = await p.export()
|
||||
if file is not None:
|
||||
await self.bot.send_file(
|
||||
user,
|
||||
file,
|
||||
content='Sending you the requested export of "{}".'.format(p.short)
|
||||
)
|
||||
return
|
||||
# this is exclusive to keep database access sequential
|
||||
# hopefully it will scale well enough or I need a different solution
|
||||
async with self.get_lock(server.id):
|
||||
p = await Poll.load_from_db(self.bot, server.id, label)
|
||||
if not isinstance(p, Poll):
|
||||
return
|
||||
|
||||
# info
|
||||
member = server.get_member(user_id)
|
||||
if emoji == '❔':
|
||||
is_open = await p.is_open()
|
||||
embed = discord.Embed(title=f"Info for the {'CLOSED ' if not is_open else ''}poll \"{p.name}\"",
|
||||
description='', color=SETTINGS.color)
|
||||
embed.set_author(name=f" >> {p.short}", icon_url=SETTINGS.author_icon)
|
||||
# export
|
||||
if emoji == '📎':
|
||||
# sending file
|
||||
file = await p.export()
|
||||
if file is not None:
|
||||
await self.bot.send_file(
|
||||
user,
|
||||
file,
|
||||
content='Sending you the requested export of "{}".'.format(p.short)
|
||||
)
|
||||
return
|
||||
|
||||
# vote rights
|
||||
vote_rights = await p.has_required_role(member)
|
||||
embed.add_field(name=f'{"Can you vote?" if is_open else "Could you vote?"}',
|
||||
value=f'{"✅" if vote_rights else "❎"}', inline=False)
|
||||
# info
|
||||
member = server.get_member(user_id)
|
||||
if emoji == '❔':
|
||||
is_open = await p.is_open()
|
||||
embed = discord.Embed(title=f"Info for the {'CLOSED ' if not is_open else ''}poll \"{p.name}\"",
|
||||
description='', color=SETTINGS.color)
|
||||
embed.set_author(name=f" >> {p.short}", icon_url=SETTINGS.author_icon)
|
||||
|
||||
# edit rights
|
||||
edit_rights = False
|
||||
if str(member.id) == str(p.author):
|
||||
edit_rights = True
|
||||
elif member.server_permissions.manage_server:
|
||||
edit_rights = True
|
||||
else:
|
||||
result = await self.bot.db.config.find_one({'_id': str(server.id)})
|
||||
if result and result.get('admin_role') in [r.name for r in member.roles]:
|
||||
# vote rights
|
||||
vote_rights = await p.has_required_role(member)
|
||||
embed.add_field(name=f'{"Can you vote?" if is_open else "Could you vote?"}',
|
||||
value=f'{"✅" if vote_rights else "❎"}', inline=False)
|
||||
|
||||
# edit rights
|
||||
edit_rights = False
|
||||
if str(member.id) == str(p.author):
|
||||
edit_rights = True
|
||||
embed.add_field(name='Can you manage the poll?', value=f'{"✅" if edit_rights else "❎"}', inline=False)
|
||||
elif member.server_permissions.manage_server:
|
||||
edit_rights = True
|
||||
else:
|
||||
result = await self.bot.db.config.find_one({'_id': str(server.id)})
|
||||
if result and result.get('admin_role') in [r.name for r in member.roles]:
|
||||
edit_rights = True
|
||||
embed.add_field(name='Can you manage the poll?', value=f'{"✅" if edit_rights else "❎"}', inline=False)
|
||||
|
||||
# choices
|
||||
choices = 'You have not voted yet.' if vote_rights else 'You can\'t vote in this poll.'
|
||||
if user.id in p.votes:
|
||||
if p.votes[user.id]['choices'].__len__() > 0:
|
||||
choices = ', '.join([p.options_reaction[c] for c in p.votes[user.id]['choices']])
|
||||
embed.add_field(name=f'{"Your current votes (can be changed as long as the poll is open):" if is_open else "Your final votes:"}',
|
||||
value=choices, inline=False)
|
||||
# choices
|
||||
choices = 'You have not voted yet.' if vote_rights else 'You can\'t vote in this poll.'
|
||||
if user.id in p.votes:
|
||||
if p.votes[user.id]['choices'].__len__() > 0:
|
||||
choices = ', '.join([p.options_reaction[c] for c in p.votes[user.id]['choices']])
|
||||
embed.add_field(name=f'{"Your current votes (can be changed as long as the poll is open):" if is_open else "Your final votes:"}',
|
||||
value=choices, inline=False)
|
||||
|
||||
# weight
|
||||
if vote_rights:
|
||||
weight = 1
|
||||
if p.weights_roles.__len__() > 0:
|
||||
valid_weights = [p.weights_numbers[p.weights_roles.index(r)] for r in
|
||||
list(set([n.name for n in member.roles]).intersection(set(p.weights_roles)))]
|
||||
if valid_weights.__len__() > 0:
|
||||
weight = max(valid_weights)
|
||||
else:
|
||||
weight = 'You can\'t vote in this poll.'
|
||||
embed.add_field(name='Weight of your votes:', value=weight, inline=False)
|
||||
# weight
|
||||
if vote_rights:
|
||||
weight = 1
|
||||
if p.weights_roles.__len__() > 0:
|
||||
valid_weights = [p.weights_numbers[p.weights_roles.index(r)] for r in
|
||||
list(set([n.name for n in member.roles]).intersection(set(p.weights_roles)))]
|
||||
if valid_weights.__len__() > 0:
|
||||
weight = max(valid_weights)
|
||||
else:
|
||||
weight = 'You can\'t vote in this poll.'
|
||||
embed.add_field(name='Weight of your votes:', value=weight, inline=False)
|
||||
|
||||
# time left
|
||||
deadline = p.get_duration_with_tz()
|
||||
if not is_open:
|
||||
time_left = 'This poll is closed.'
|
||||
elif deadline == 0:
|
||||
time_left = 'Until manually closed.'
|
||||
else:
|
||||
time_left = str(deadline-datetime.datetime.utcnow().replace(tzinfo=pytz.utc)).split('.', 2)[0]
|
||||
# time left
|
||||
deadline = p.get_duration_with_tz()
|
||||
if not is_open:
|
||||
time_left = 'This poll is closed.'
|
||||
elif deadline == 0:
|
||||
time_left = 'Until manually closed.'
|
||||
else:
|
||||
time_left = str(deadline-datetime.datetime.utcnow().replace(tzinfo=pytz.utc)).split('.', 2)[0]
|
||||
|
||||
embed.add_field(name='Time left in the poll:', value=time_left, inline=False)
|
||||
embed.add_field(name='Time left in the poll:', value=time_left, inline=False)
|
||||
|
||||
await self.bot.send_message(user, embed=embed)
|
||||
return
|
||||
await self.bot.send_message(user, embed=embed)
|
||||
return
|
||||
|
||||
# Assume: User wants to vote with reaction
|
||||
# no rights, terminate function
|
||||
if not await p.has_required_role(member):
|
||||
await self.bot.remove_reaction(message, emoji, user)
|
||||
await self.bot.send_message(user, f'You are not allowed to vote in this poll. Only users with '
|
||||
f'at least one of these roles can vote:\n{", ".join(p.roles)}')
|
||||
return
|
||||
|
||||
# order here is crucial since we can't determine if a reaction was removed by the bot or user
|
||||
# update database with vote
|
||||
await p.vote(member, emoji, message)
|
||||
#
|
||||
# check if we need to remove reactions (this will trigger on_reaction_remove)
|
||||
if str(channel.type) != 'private':
|
||||
if p.anonymous:
|
||||
# immediately remove reaction and to be safe, remove all reactions
|
||||
# Assume: User wants to vote with reaction
|
||||
# no rights, terminate function
|
||||
if not await p.has_required_role(member):
|
||||
await self.bot.remove_reaction(message, emoji, user)
|
||||
elif p.multiple_choice == 1:
|
||||
# remove all other reactions
|
||||
for r in message.reactions:
|
||||
if r.emoji and r.emoji != emoji:
|
||||
await self.bot.remove_reaction(message, r.emoji, user)
|
||||
await self.bot.send_message(user, f'You are not allowed to vote in this poll. Only users with '
|
||||
f'at least one of these roles can vote:\n{", ".join(p.roles)}')
|
||||
return
|
||||
|
||||
# order here is crucial since we can't determine if a reaction was removed by the bot or user
|
||||
# update database with vote
|
||||
await p.vote(member, emoji, message)
|
||||
#
|
||||
# check if we need to remove reactions (this will trigger on_reaction_remove)
|
||||
if str(channel.type) != 'private':
|
||||
if p.anonymous:
|
||||
# immediately remove reaction and to be safe, remove all reactions
|
||||
await self.bot.remove_reaction(message, emoji, user)
|
||||
elif p.multiple_choice == 1:
|
||||
# remove all other reactions
|
||||
for r in message.reactions:
|
||||
if r.emoji and r.emoji != emoji:
|
||||
await self.bot.remove_reaction(message, r.emoji, user)
|
||||
|
||||
def setup(bot):
|
||||
global logger
|
||||
|
||||
Reference in new issue
Block a user