Search code examples
pythonpycharmdiscordbotsmention

A command for discord bot to say server amd channel name


I need help with {server} and with the {channel}. I can't make the bot say the server name and the channel name.

@client.command()
async def whereami(ctx):
    await ctx.send(f'You are on {server} on {channel}')

Solution

  • Server and channel is not defined. The ctx argument is an object which has two attributes which you are looking for; the guild and channel (guild is what they are called in Discord, not servers). So by using ctx.channel and ctx.guild you can access the guild and channel objects.

    @client.command()
    async def whereami(ctx):
       await ctx.send(f'You are on {ctx.guild} on {ctx.channel}')
    

    You can read more about all the attributes in the offical docs: discord.py docs