[英]How to get a channel name Discord.py Rewrite
我正在使用 discord.py 制作一個機器人,我正在嘗試獲取頻道名稱以確認將嵌入發送到何處,例如:
您希望將嵌入發送到哪里?
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'
謝謝!
簡要瀏覽一下文檔會告訴您,您可以通過執行從上下文 object 獲取頻道 object
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}?")
您會注意到,您還可以像上面演示的那樣在命令調用中傳遞通道 object。
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")
另外,如果你有公會,你可以用guild.text_channels
或guild.voice_channels
etc 替換channels
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.