Files
abot/group_auto/group_auto_invite.py
2025-02-19 09:10:25 +08:00

95 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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}")
return 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()