首页 > 解决方案 > 作者频道名称

问题描述

我希望它输入文本频道名称,但它不起作用请帮忙

@client.command()
async def lock(ctx):
    channel = ctx.message.author.discord.text_channel.name
    server = ctx.message.guild
    overwrites_everyone = ctx.message.channel.overwrites_for(server.default_role)
    overwrites_everyone.send_messages = False
    await ctx.message.channel.set_permissions(server.default_role, overwrite=overwrites_everyone)
    await ctx.send(f"{channel} it is closed ")

标签: pythondiscorddiscord.py

解决方案


ctx(Context) 对象中,您可以通过ctx.channel. 它返回一个TextChannel对象,然后您可以从中获取其他属性:

@bot.command()
async def whereami(ctx):
    await ctx.send(f"You're in a channel called {ctx.channel} ({ctx.channel.mention})!")

如示例中所示,您可以通过访问该.mention属性来获得频道的提及。这相当于f"<#{ctx.channel.id}>"

将通道对象转换为字符串时,它会显示通道的名称。它相当于ctx.channel.name.


参考:


推荐阅读