#!/usr/bin/env python3.12 import re import discord from typing import Optional, Any """ Discord Helper Methods """ async def get_channel_by_name(bot: discord.Bot, channel: str, guild: int | None = None) -> Optional[Any]: # Optional[Any] used as pycord channel types can be ambigious """ Get Channel by Name Args: bot (discord.Bot) channel (str) guild (int|None) Returns: Optional[Any] """ channel: str = re.sub(r'^#', '', channel.strip()) if not guild: return discord.utils.get(bot.get_all_channels(), name=channel) else: channels: list = bot.get_guild(guild).channels for _channel in channels: if _channel.name.lower() == channel.lower().strip(): return _channel return async def send_message(bot: discord.Bot, channel: str, message: str, guild: int | None = None) -> None: """ Send Message to the provided channel. If guild is provided, will limit to channels within that guild to ensure the correct channel is selected. Useful in the event a channel exists in more than one guild that the bot resides in. Args: bot (discord.Bot) channel (str) message (str) guild (int|None) Returns: None """ if channel.isnumeric(): channel: int = int(channel) _channel = bot.get_channel(channel) else: channel: str = re.sub(r'^#', '', channel.strip()) _channel = await get_channel_by_name(bot=bot, channel=channel, guild=guild) await _channel.send(message)