簡體   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