BonsaiCubing
BonsaiCubing

Reputation: 11

discord.py sending message to channel name instead of ID

I am a beginner to this programming stuff, and I have a quick question. I am trying to make a logs channel for multiple servers, and I want to be able to look for a channel that has the word “logs” in it, so can you help me?

Code:

@client.event
async def on_command_completion(ctx):
    channel = client.get_channel('829067962316750898')
    embed = discord.Embed(colour=discord.Color.green(),
                          title="Command Executed")
    embed.add_field(name="Command:", value=f"`,{ctx.command}`")
    embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
    embed.add_field(name="Channel:",
                    value=f"{ctx.channel} **( <#{ctx.channel.id}> )**")
    await channel.send(embed=embed)

Upvotes: 0

Views: 93

Answers (1)

12944qwerty
12944qwerty

Reputation: 1925

You need to get the channel. I suggest using discord.utils.get.
Also, try using {ctx.channel.mention} to mention the channel instead.

@client.event
async def on_command_completion(ctx):
    channel = discord.utils.get(ctx.guild.text_channels, name='logs')
    if channel is None:
         return # If there is no logs channel, do nothing (or what you want to do)
    embed = discord.Embed(colour=discord.Color.green(),
                          title="Command Executed")
    embed.add_field(name="Command:", value=f"`,{ctx.command}`")
    embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
    embed.add_field(name="Channel:",
                    value=f"{channel.name} **( {channel.mention} )**")
    await channel.send(embed=embed)

See the discord.TextChannel docs and discord.utils.get()

Upvotes: 2

Related Questions