繁体   English   中英

Discord.py 按名称中的字编辑频道

[英]Discord.py editing channel by word that is in the name

我正在制作统计机器人,但我遇到了一个问题,语音频道包含会员数。 我想让机器人更新该频道的名称, on_member_joinon_member_remove以及当用户使用命令refresh时,但我以不同的方式尝试了很多次,但它仍然不起作用,我想让他编辑名称中包含“成员”的频道:" 但我最多可以使用常量名称get频道。 有没有办法通过包含“成员:”来get频道?

好的,我尝试了 Łukasz 的代码,但它仍然没有更改频道名称。 我的代码:

@bot.event
async def on_member_join(member):
    await asyncio.sleep(random.randint(600, 1200))
    guild = member.guild
    channels = [c for c in guild.channels if "Members:" in c.name.lower()]
    for channel in channels:
        await channel.edit(f"Members: {member.guild.member_count}")

@bot.event
async def on_member_remove(member):
    await asyncio.sleep(random.randint(600, 1200))
    guild = member.guild
    channels = [c for c in guild.channels if "Members:" in c.name.lower()]
    for channel in channels:
        await channel.edit(name=f"Members: {member.guild.member_count}")

@bot.command()
async def refresh(ctx):
    await ctx.send('Starting refreshing members count voice channel.')
    guild = ctx.guild
    channels = [c for c in guild.channels if "Members:" in c.name.lower()]
    for channel in channels:
        await channel.edit(f"Members: {ctx.guild.member_count}")
        await ctx.send(':thumbsup:')

还有我的频道截图(也许这很重要):
在此处输入图像描述

在此处输入图像描述

你能告诉我为什么它不起作用吗?

您应该使用in关键字,例如

>>> "members:" in "whatever members: something"
True

要获取包含单词members:

guild = # Any `discord.Guild` instance
channels = []

for c in guild.channels: # You can also use the `Guild.text_channels` or `Guild.voice_channels` attributes
    if if "members:" in c.name.lower():
        channels.append(c)

如果你想要一个单行:

guild = # Any `discord.Guild` instance
channels = [c for c in guild.channels if "members:" in c.name.lower()]

然后你可以遍历每个通道并对其进行编辑:

for channel in channels:
    await channel.edit(**kwargs)

注意:编辑频道有很高的速率限制(每个频道 iirc 每 10 分钟 2 个请求)你应该明智地使用它

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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