作者频道名称

时间:2020-06-13 11:38:12

标签: python discord discord.py

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

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

2 个答案:

答案 0 :(得分:0)

您可以通过执行ctx.channel来简单地获取当前文本通道

@bot.command
async def channel(ctx):
    channel = ctx.channel
    await ctx.send('Your are in ' + channel + ' text channel.')


答案 1 :(得分:0)

通过ctx(上下文)对象,您可以直接获取频道。它返回一个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}>"


参考: