121 lines
3.6 KiB
Python
121 lines
3.6 KiB
Python
import redis
|
||
|
||
# 创建 Redis 连接
|
||
try:
|
||
r = redis.StrictRedis(host='192.168.2.32', port=6379, db=0, decode_responses=True)
|
||
r.ping() # 检查连接是否正常
|
||
except redis.ConnectionError as e:
|
||
print(f"Redis连接失败: {e}")
|
||
exit(1)
|
||
|
||
# Redis 中存储群组映射的前缀
|
||
mapping_prefix = "group:group_mapping:"
|
||
|
||
|
||
# 添加群组ID到指定key
|
||
def add_mapping(key, group_id):
|
||
try:
|
||
if r.sismember(mapping_prefix + key, group_id):
|
||
return f"Group ID {group_id} already exists for key {key}."
|
||
else:
|
||
r.sadd(mapping_prefix + key, group_id)
|
||
return f"Added: {key} -> {group_id}"
|
||
except redis.RedisError as e:
|
||
return f"操作失败: {e}"
|
||
|
||
|
||
# 删除指定key下的某个群组ID
|
||
def del_mapping(key, group_id):
|
||
try:
|
||
if r.sismember(mapping_prefix + key, group_id):
|
||
r.srem(mapping_prefix + key, group_id)
|
||
return f"Deleted: {key} -> {group_id}"
|
||
else:
|
||
return f"Group ID {group_id} not found for key {key}."
|
||
except redis.RedisError as e:
|
||
return f"操作失败: {e}"
|
||
|
||
|
||
# 获取指定key下的所有群组ID
|
||
def get_group_ids(key):
|
||
try:
|
||
group_ids = r.smembers(mapping_prefix + key)
|
||
if group_ids:
|
||
return f"Group IDs for {key}: {', '.join(group_ids)}"
|
||
else:
|
||
return f"Key '{key}' has no associated group IDs."
|
||
except redis.RedisError as e:
|
||
return f"操作失败: {e}"
|
||
|
||
|
||
# 获取指定key的第一个群组ID
|
||
def get_first_group_id(key):
|
||
try:
|
||
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:
|
||
return f"Key '{key}' has no associated group IDs."
|
||
except redis.RedisError as e:
|
||
return f"操作失败: {e}"
|
||
|
||
|
||
# 处理命令行输入
|
||
def process_command(command):
|
||
command_parts = command.split()
|
||
if len(command_parts) == 0:
|
||
return "Invalid command."
|
||
|
||
cmd = command_parts[0]
|
||
|
||
if cmd == "add" and len(command_parts) == 3:
|
||
key = command_parts[1]
|
||
group_id = command_parts[2]
|
||
return add_mapping(key, group_id)
|
||
|
||
elif cmd == "del" and len(command_parts) == 3:
|
||
key = command_parts[1]
|
||
group_id = command_parts[2]
|
||
return del_mapping(key, group_id)
|
||
|
||
elif cmd == "get" and len(command_parts) == 2:
|
||
key = command_parts[1]
|
||
return get_group_ids(key)
|
||
|
||
elif cmd == "get_first" and len(command_parts) == 2:
|
||
key = command_parts[1]
|
||
return get_first_group_id(key)
|
||
|
||
elif cmd == "help":
|
||
commands = (
|
||
"命令列表:\n"
|
||
"'#加群配置|add key group_id' - 添加群组ID\n"
|
||
"'#加群配置|del key group_id' - 删除群组ID\n"
|
||
"'#加群配置|get key' - 获取所有群组ID\n"
|
||
"'#加群配置|get_first key' - 获取第一个群组ID\n"
|
||
)
|
||
return commands
|
||
|
||
else:
|
||
return "Unknown command or wrong number of arguments. please enter : #加群配置|help"
|
||
|
||
|
||
# 主函数
|
||
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()
|