Files
och-bot/lib/utils.py

32 lines
1.1 KiB
Python

from collections import AsyncIterable
from typing import Callable, AsyncGenerator, Optional
import discord
async def async_filter(fun: Callable, iterable: AsyncIterable) -> AsyncGenerator:
async for val in iterable:
if fun(val):
yield val
def find_category(guild: discord.Guild, group: str) -> Optional[discord.CategoryChannel]:
group = group.lower()
for cat in guild.categories:
if cat.name.lower() == group:
return cat
return None
async def find_role_case_insensitive(guild: discord.Guild, name: str, prefix: str = '') -> Optional[discord.Role]:
for role in await guild.fetch_roles():
if name.strip().lower() == role.name[len(prefix):].lower():
return role
return None
def link_channel(channel: discord.abc.GuildChannel, italic: bool = False) -> str:
if italic:
return '[*' + channel.name + '*](https://discord.com/channels/' + str(channel.guild.id) + '/' + str(
channel.id) + ')'
return '[' + channel.name + '](https://discord.com/channels/' + str(channel.guild.id) + '/' + str(channel.id) + ')'