Telethon 和 Python Telegram Bot 库 读取和设置群组的用户权限 Music和Voice Messages 即audio和voice_notes 有个坑Bug

环境说明

Telegram界面设置群组的默认权限.
进群验证机器人(tg-join-group-exam-bot.py) 在新成员通过验证后, 会在 Exceptions 里面设置权限.
清除 Exceptions 工具(tg-clean-exceptions.py) 会读取群组的默认权限, 再读取 Exceptions 中某个用户的权限.

测试过程

Case 1. 

群组默认权限 
music - Close
voice messages - Close

tg-join-group-exam-bot.py 设置 permission
can_send_audios=False,
can_send_voice_notes=False,


新成员通过验证后, 在Exceptions中的记录是这样的, 符合期望

tg-clean-exceptions.py
default_rights = entity.default_banned_rights 读出来的是什么
rights = getattr(p.participant, 'banned_rights', None)  读出来的是什么
可以看到默认权限是对的. 这个属性是 default_banned_rights. 所以 Ture 表示 Close.
而读取的 Exceptions 里面, 用户的权限是错的. False 表示 Open.

Case 2. 

群组默认权限 
music - Close
voice messages - Close

tg-join-group-exam-bot.py 设置 permission
can_send_audios=True,         (意思是 Open)
can_send_voice_notes=True,        (意思是 Open)

新成员通过验证后, 在Exceptions中的记录是这样的,  不符合预期.
因为上面的代码中设定的 permission 是 True, 应该是有这两个权限的. 而 telegram 界面上显示是没有. 

tg-clean-exceptions.py
default_rights = entity.default_banned_rights 读出来的是什么
rights = getattr(p.participant, 'banned_rights', None)  读出来的是什么
默认权限中, 这两个值都是 True, 表示 Close. 与实际上的设置一致.
读取的 Exceptions 里面, 用户的权限 的值 是 False, 表示 Open. 与 界面上不一致. 与 tg-join-group-exam-bot.py 代码一致.

Case 3. 

群组默认权限 
music - Open
voice messages - Open

tg-join-group-exam-bot.py 设置 permission
can_send_audios=False,
can_send_voice_notes=False,

新成员通过验证后, 在Exceptions中的记录是这样的. 不符合预期.
因为上面的代码中设定的 permission 是 False, 应该是没有有这两个权限的. 而 telegram 界面上显示是有. 

tg-clean-exceptions.py
default_rights = entity.default_banned_rights 读出来的是什么
rights = getattr(p.participant, 'banned_rights', None)  读出来的是什么
读取的结果与 telegram 界面上显示的 是一致的.
读取的 Exceptions 里面, 用户的权限 的值 是 False, 表示 Open. 与 界面上一致. 与 tg-join-group-exam-bot.py 代码不一致.

Case 4. 

群组默认权限 
music - Open
voice messages - Open

tg-join-group-exam-bot.py 设置 permission
can_send_audios=True,
can_send_voice_notes=True,

新成员通过验证后, 在Exceptions中的记录是这样的. 符合预期.

tg-clean-exceptions.py
default_rights = entity.default_banned_rights 读出来的是什么
rights = getattr(p.participant, 'banned_rights', None)  读出来的是什么
都是正确的. False 表示 Open, 有这个权限.

总结

在使用 Telethon 和 Python Telegram Bot 库 读取和设置群组的用户权限 Music和Voice Messages (即audio和voice_notes) 时, 应该保持这两个权限都为 Open. 

注: 当前库版本
pip3 show telethon
Name: Telethon
Version: 1.41.2

pip3 show python-telegram-bot
Name: python-telegram-bot
Version: 22.5

评论

The Hot3 in Last 7 Days

酒馆SillyTavern 玩英文角色卡 也能以中文输出 设置世界书Lorebooks

搭 Docker版 Sub-Store 带 http-meta 实现 集合订阅 测延迟 排序 筛选 生成新订阅 定时任务上传Gist