简体   繁体   中英

How to get a channel name Discord.py Rewrite

Im making a bot using discord.py and I am trying to get the channel name to confirm where to send the embed like:

Where do you want the Embed to be sent?

1 - #general

2 - different channel

Here is the code I tried to use:

channel = client.get_channel(ctx.author)

I get this error

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

Thanks!

As a brief glance at the docs would have told you you can get a channel object from the context object by doing

channel = ctx.channel

You can do a few different things with that like this:

@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}?")

You'll note that you can also pass a channel object in the command call like I demonstrated above.

clint.get_chanel can be uses by id of the channel.

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

If you want to get channel with name, use 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")

Also, if you have the guild, you can replace channels with guild.text_channels or guild.voice_channels etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM