在 Python Discord Bot 中按名称而不是 ID 将消息发送到特定文本通道

2024-02-18

是否有与 client.get_channel('ID') 等效的命令,允许您将消息发送到专门命名的文本通道。

我的项目是一个报告系统,它将在所有员工都可以看到的员工聊天中打印报告。我希望它可以跨多个服务器工作,因此不能选择使用 ID,因为所有服务器都有不同的通道 ID。


您可以使用discord.utils.get https://discordpy.readthedocs.io/en/latest/api.html#discord.utils.get迭代server.channels https://discordpy.readthedocs.io/en/latest/api.html#discord.Server.channels并找到具有特定名称的频道:

import discord
from discord.utils import get

async def report(server, name, *args, **kwargs):
    channel = get(server.channels, name=name, type=discord.ChannelType.text)
    await bot.send_message(channel, *args, **kwargs)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Python Discord Bot 中按名称而不是 ID 将消息发送到特定文本通道 的相关文章

随机推荐