#!/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 = re.sub(r'^#', '', channel.strip()) if not guild: return discord.utils.get(bot.get_all_channels(), name=channel) else: _guild: Optional[discord.Guild] = bot.get_guild(guild) if not _guild: return None channels: list = _guild.channels for _channel in channels: if _channel.name.lower() == channel.lower().strip(): return _channel return None 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 = int(channel) _channel = bot.get_channel(channel_int) else: channel = re.sub(r'^#', '', channel.strip()) _channel = await get_channel_by_name(bot=bot, channel=channel, guild=guild) if not isinstance(_channel, discord.TextChannel): return None await _channel.send(message)