加群配置使用前端指令完成,方便管理。

This commit is contained in:
liuwei
2025-02-19 14:51:52 +08:00
parent 5019d86af3
commit 3083ed948f
5 changed files with 142 additions and 69 deletions

View File

@@ -1,7 +1,12 @@
import redis
# 创建 Redis 连接
r = redis.StrictRedis(host='192.168.2.32', port=6379, db=0, decode_responses=True)
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:"
@@ -9,71 +14,93 @@ 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}")
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):
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}.")
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):
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.")
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):
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.")
try:
group_ids = r.smembers(mapping_prefix + key)
if group_ids:
first_group_id = next(iter(group_ids)) # 获取集合中的第一个元素
return 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:
print("Invalid command.")
return
return "Invalid command."
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)
return 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)
return del_mapping(key, group_id)
elif cmd == "get" and len(command_parts) == 2:
key = command_parts[1]
get_group_ids(key)
return get_group_ids(key)
elif cmd == "get_first" and len(command_parts) == 2:
key = command_parts[1]
get_first_group_id(key)
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"
"'exit' - 退出系统"
)
return commands
else:
print("Unknown command or wrong number of arguments.")
return "Unknown command or wrong number of arguments. please enter : #加群配置|help"
# 主函数