Filter seemingly empty messages from random messages
This commit is contained in:
4
bot.py
4
bot.py
@@ -10,6 +10,7 @@ from discord.ext import commands
|
|||||||
from dotenv import load_dotenv
|
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.config import config, config_meta, config_load, config_save, config_get, config_set, config_get_descriptions
|
||||||
|
from lib.utils import async_filter
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
TOKEN = os.getenv('DISCORD_TOKEN')
|
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()
|
typing = ctx.channel.trigger_typing()
|
||||||
if channel is None:
|
if channel is None:
|
||||||
channel = ctx.channel
|
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)
|
msg: discord.Message = random.choice(messages)
|
||||||
author: discord.abc.User = msg.author
|
author: discord.abc.User = msg.author
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
|
|||||||
8
lib/utils.py
Normal file
8
lib/utils.py
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user