如何获得频道名discord.py rewrite

j7dteeu8  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(365)

我正在使用discord.py制作一个bot,我正在尝试获取频道名称以确认在何处发送嵌入内容,例如:
您希望将嵌入发送到哪里?
1-#概述
2-不同频道
以下是我尝试使用的代码:

channel = client.get_channel(ctx.author)

我得到这个错误

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'context' object has no attribute 'text_channel'

谢谢!

mjqavswn

mjqavswn1#

简单看一下文档就会知道,您可以通过执行以下操作从context对象获取channel对象

channel = ctx.channel

你可以这样做:

@bot.command()
async def make_embed(ctx, embed_content, channel : discord.Channel):
    # create your embed here
    await ctx.send(f"Did you mean to send the embed to {ctx.channel.name} or {channel.name}?")

您将注意到,您还可以在命令调用中传递一个channel对象,就像我上面演示的那样。

6tqwzwtp

6tqwzwtp2#

clint.get_chanel 可以按通道的id使用。

channel = client.get_channel(123456789123456789)
await channel.send("message")

如果要获取名为的频道,请使用 discord.utils ```
channels = [c for g in client.guilds for c in g.text_channels] #getting all the text channels bot can see
channel = discord.utils.get(channels, name="general")

另外,如果你有公会,你可以替换 `channels` 与 `guild.text_channels` 或者 `guild.voice_channels` e、 t.c。

相关问题