在之前的實作中,所有的指令後都沒有帶參數,因此不需要處理空白後面的內容。但現在新增了一些指令,需要將指令後面的參數解析並存入 Set
集合中。
這些新指令包括:
!sp_price
!sp_floor
!sp_room
!sp_address
!sp_item
下面是處理指令的程式碼,會將使用者輸入的空白後面的參數存入 Set
中,而指令改用switch/case進行處理:
private void doBotCommand(String message, String channelId, String userId) {
// 取出指令和參數部分
String[] messageParts = message.split("\\s+", 2);
String command = messageParts[0]; // 指令名稱 (例如 !sp_room)
Set<String> arguments = messageParts.length == 1 ? new HashSet<String>() : new HashSet<>(Arrays.asList(messageParts[1].split("\\s+"))); // 指令參數
EmbedBuilder botResponeMessage;
switch (command) {
case "!help":
botResponeMessage = discordBotCommandService.helpCommand();
break;
case "!sub":
botResponeMessage = discordBotCommandService.subscribeCommand(userId, channelId);
break;
case "!unsub":
botResponeMessage = discordBotCommandService.unsubscribeCommand(userId);
break;
case "!sp_room":
botResponeMessage = discordBotCommandService.specifyRoomTypeCommand(userId, arguments);
break;
case "!sp_price":
botResponeMessage = discordBotCommandService.specifyPriceCommand(userId, arguments);
break;
case "!sp_address":
botResponeMessage = discordBotCommandService.specifyAddressCommand(userId, arguments);
break;
case "!sp_floor":
botResponeMessage = discordBotCommandService.specifyFloorCommand(userId, arguments);
break;
case "!sp_item":
botResponeMessage = discordBotCommandService.specifyItemCommand(userId, arguments);
break;
case "!info":
botResponeMessage = discordBotCommandService.showInfoCommand(userId);
break;
default:
botResponeMessage = discordBotCommandService.unKnowCommand();
break;
}
discordBotService.sendMessage(channelId, botResponeMessage);
}
價格範圍
新增最低價與最高價屬性,讓用戶可以設定租屋的價格範圍:
private Integer lowestPrice;
private Integer highestPrice;
樓層範圍
新增最低樓層與最高樓層屬性,讓用戶可以設定租屋樓層的範圍:
private Integer lowestFloor;
private Integer highestFloor;
房型、地址、設備
使用 @ElementCollection
將房型、地址及設備存入關聯表中,並將延遲加載關閉 (FetchType.EAGER)
,確保查詢時會立即載入相關的資料:
房型:
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "user_rooms", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "room_type")
private Set<String> rooms = new HashSet<>();
地址:
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "user_address", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "address")
private Set<String> address = new HashSet<>();
設備:
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "user_items", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "item_name")
private Set<String> items = new HashSet<>();
價格設定:
根據用戶輸入的參數設置最低價與最高價:
@Override
public EmbedBuilder specifyPriceCommand(String userID, Set<String> arguments) {
SubscribeUser subscribeUser = getOrNewSubscribeUser(userID, "");
String argumentString = arguments.iterator().next();
String[] messageParts = argumentString.split("~", 2);
subscribeUser.setLowestPrice(Integer.valueOf(messageParts[0]));
subscribeUser.setHighestPrice(Integer.valueOf(messageParts[1]));
subscribeUserRepository.save(subscribeUser);
return stringToEmbed("價格區間新增成功");
}
樓層設定:
根據用戶輸入的參數設置最低樓層與最高樓層:
@Override
public EmbedBuilder specifyFloorCommand(String userID, Set<String> arguments) {
SubscribeUser subscribeUser = getOrNewSubscribeUser(userID, "");
String argumentString = arguments.iterator().next();
String[] messageParts = argumentString.split("~", 2);
subscribeUser.setLowestFloor(Integer.valueOf(messageParts[0]));
subscribeUser.setHighestFloor(Integer.valueOf(messageParts[1]));
subscribeUserRepository.save(subscribeUser);
return stringToEmbed("樓層區間新增成功");
}
房型設定:
將房型參數存入 Set 並更新資料庫:
@Override
public EmbedBuilder specifyRoomTypeCommand(String userID, Set<String> arguments) {
SubscribeUser subscribeUser = getOrNewSubscribeUser(userID, "");
subscribeUser.setRooms(arguments);
subscribeUserRepository.save(subscribeUser);
return stringToEmbed("房型類別新增成功");
}
地址、地區設定:
將地址參數存入 Set 並更新資料庫:
@Override
public EmbedBuilder specifyAddressCommand(String userID, Set<String> arguments) {
SubscribeUser subscribeUser = getOrNewSubscribeUser(userID, "");
subscribeUser.setAddress(arguments);
subscribeUserRepository.save(subscribeUser);
return stringToEmbed("地址、地區新增成功");
}
設備設定:
將設備參數存入 Set 並更新資料庫:
public EmbedBuilder specifyItemCommand(String userID, Set<String> arguments) {
SubscribeUser subscribeUser = getOrNewSubscribeUser(userID, "");
subscribeUser.setItems(arguments);
subscribeUserRepository.save(subscribeUser);
return stringToEmbed("設備新增成功");
今天完成了幾個設定租屋條件的指令,包括房型、價格、樓層、地址及設備。接下來將實作 info 和 reset 功能,並對設定進行簡單展示,git也明天一併更新。