diff --git a/bot.py b/bot.py index e1e915d..a91e688 100644 --- a/bot.py +++ b/bot.py @@ -10,6 +10,7 @@ from discord.ext import commands from dotenv import load_dotenv from lib.config import config, config_meta, config_load, config_save, config_get, config_set, config_get_descriptions +from lib.utils import async_filter load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') @@ -120,7 +121,8 @@ async def random_message_command(ctx: commands.Context, channel: Optional[discor typing = ctx.channel.trigger_typing() if channel is None: channel = ctx.channel - messages = await channel.history(limit=max_cnt).flatten() + messages = async_filter(lambda m: m.clean_content.strip() != '', channel.history(limit=max_cnt)) + messages = [item async for item in messages] msg: discord.Message = random.choice(messages) author: discord.abc.User = msg.author embed = discord.Embed( diff --git a/lib/utils.py b/lib/utils.py new file mode 100644 index 0000000..908cd65 --- /dev/null +++ b/lib/utils.py @@ -0,0 +1,8 @@ +from collections import AsyncIterable +from typing import Callable, AsyncGenerator + + +async def async_filter(fun: Callable, iterable: AsyncIterable) -> AsyncGenerator: + async for val in iterable: + if fun(val): + yield val