Add random message filter by reaction

This commit is contained in:
2020-12-19 13:47:46 +01:00
parent 2504a65b61
commit cd963ae6b2

18
bot.py
View File

@@ -117,11 +117,11 @@ async def on_message(message: discord.Message):
@bot.command(name='random_message', brief='Select a random message from a channel')
async def random_message_command(ctx: commands.Context, channel: Optional[discord.TextChannel] = None,
max_cnt: int = 100):
max_cnt: Optional[int] = 100, reaction_filter: Optional[str] = None):
typing = ctx.channel.trigger_typing()
if channel is None:
channel = ctx.channel
messages = async_filter(_is_message_allowed_for_operations, channel.history(limit=max_cnt))
messages = async_filter(lambda m: _is_message_valid_for_selection(m, reaction_filter), channel.history(limit=max_cnt))
messages = [item async for item in messages]
if not messages:
await typing
@@ -138,12 +138,24 @@ async def random_message_command(ctx: commands.Context, channel: Optional[discor
await ctx.channel.send(embed=embed)
def _is_message_allowed_for_operations(message: discord.Message) -> bool:
def _is_message_valid_for_selection(message: discord.Message, reaction_filter: Optional[str] = None) -> bool:
if message.clean_content.strip() == '':
return False
for reaction in message.reactions:
if reaction.emoji == '':
return False
if reaction_filter is not None:
for reaction in message.reactions:
if type(reaction.emoji) is str:
if reaction.emoji == reaction_filter:
return True
elif type(reaction.emoji) is discord.Emoji or type(reaction.emoji) is discord.PartialEmoji:
if ':' + reaction.emoji.name + ':' == reaction_filter:
return True
return False
return True