feature:加入了群成员变化监控功能,提醒群成员,有人退出了群聊

This commit is contained in:
liuwei
2025-02-18 17:57:29 +08:00
parent 6c8147cb17
commit 48a36d42d4
3 changed files with 276 additions and 6 deletions
+93
View File
@@ -0,0 +1,93 @@
import redis
# 创建 Redis 连接
r = redis.StrictRedis(host='192.168.2.32', port=6379, db=0, decode_responses=True)
# Redis 中存储群组映射的前缀
mapping_prefix = "group:group_mapping:"
# 添加群组ID到指定key
def add_mapping(key, group_id):
if r.sismember(mapping_prefix + key, group_id):
print(f"Group ID {group_id} already exists for key {key}.")
else:
r.sadd(mapping_prefix + key, group_id)
print(f"Added: {key} -> {group_id}")
# 删除指定key下的某个群组ID
def del_mapping(key, group_id):
if r.sismember(mapping_prefix + key, group_id):
r.srem(mapping_prefix + key, group_id)
print(f"Deleted: {key} -> {group_id}")
else:
print(f"Group ID {group_id} not found for key {key}.")
# 获取指定key下的所有群组ID
def get_group_ids(key):
group_ids = r.smembers(mapping_prefix + key)
if group_ids:
print(f"Group IDs for {key}: {', '.join(group_ids)}")
else:
print(f"Key '{key}' has no associated group IDs.")
# 获取指定key的第一个群组ID
def get_first_group_id(key):
group_ids = r.smembers(mapping_prefix + key)
if group_ids:
first_group_id = next(iter(group_ids)) # 获取集合中的第一个元素
print(f"First Group ID for {key}: {first_group_id}")
else:
print(f"Key '{key}' has no associated group IDs.")
# 处理命令行输入
def process_command(command):
command_parts = command.split()
if len(command_parts) == 0:
print("Invalid command.")
return
cmd = command_parts[0]
if cmd == "add" and len(command_parts) == 3:
key = command_parts[1]
group_id = command_parts[2]
add_mapping(key, group_id)
elif cmd == "del" and len(command_parts) == 3:
key = command_parts[1]
group_id = command_parts[2]
del_mapping(key, group_id)
elif cmd == "get" and len(command_parts) == 2:
key = command_parts[1]
get_group_ids(key)
elif cmd == "get_first" and len(command_parts) == 2:
key = command_parts[1]
get_first_group_id(key)
else:
print("Unknown command or wrong number of arguments.")
# 主函数
def main():
print("群自动邀请系统已启动。")
print(
"输入 'add key group_id' 来添加群组ID'del key group_id' 来删除,'get key' 来查询所有群组ID'get_first key' 来查询第一个群组ID。")
while True:
command = input("请输入命令:")
if command.lower() == "exit":
print("退出系统。")
break
process_command(command)
if __name__ == "__main__":
main()