python - Discord.py 按名称中的单词编辑频道
问题描述
我正在制作统计机器人,但我遇到了一个问题,语音频道包含会员数。我想让机器人更新该频道的名称,on_member_join
当on_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 个请求)你应该明智地使用它
参考:
推荐阅读
- php - 在 echo 中运行 php 脚本
- python - 如何使用 for 循环将多个 pandas 数据帧中的各个列连接到预先存在的 pandas 数据帧?
- c# - 在 C# 代码中用 LINQ 替换使用 MERGE 的 SQL 进程
- node.js - nodejs:使用 Postman 从 Mongodb 检索 base64 图像
- r - rollapplyr 学生跨列 t 检验
- powershell - Powershell错误输出
- reactjs - 多个文件上传反应
- cupy - AMD GPU 上的 CuPy 导致 ImportError
- c# - 有没有办法检查一个力是否被施加到一个物体上?
- sql - 每个总和都带有 WHERE 子句的 SUM 函数?