JAVA基于core开发时多个好友监听问题
-
大佬们 比如机器人有两个好友 当好友A给机器人发你好 机器人会回复 请输入指令 1 2 3中选一个回复 但是这个时候如果好友B也发送你好 就会回复好友B指令错误 相当于两个好友占用了一个监听 这种情况怎么解决 是需要用到线程嘛
-
为每一个触发指令的好友创建一个 session,通过 (指令, 好友QQ号) 对判断指令触发状态
-
@StageGuard 大佬可以给个例子嘛
-
@fengxiaoruia 发出你的代码
-
@NotNull
@EventHandler
public ListeningStatus onMessage(@NotNull FriendMessageEvent messageEvent) throws Exception {
if(message.equals("登录")){
messageEvent.getSubject().sendMessage("请输入手机号:");
UserFunctions.smsAddVariable(bot,qbot,adminQQ,eventQQ);
}
}public static void smsAddVariable(Bot bot, Qbot qbot, long adminQQ,long eventQQ){
bot.getEventChannel().subscribe(FriendMessageEvent.class, event -> {
String phoneNumber = String.valueOf(event.getMessage().get(1));
long eventQQ1 = event.getSender().getId();
if (PhoneUtil.isPhoneNumber(phoneNumber) && eventQQ1 == eventQQ) {event.getSubject().sendMessage("请输入验证码"); return ListeningStatus.STOPPED; }else{ return ListeningStatus.LISTENING; } }); return ListeningStatus.STOPPED; }else if(!PhoneUtil.isPhoneNumber(phoneNumber) && eventQQ1 == eventQQ){ event.getSubject().sendMessage("手机号格式错误 已退出会话"); return ListeningStatus.STOPPED; }else{ return ListeningStatus.LISTENING; } }); }
-
package io.github.karlatemp.jmse.tests; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class Session { private static final Map<Long, Object> SESSIONS = new ConcurrentHashMap<>(); private static final Object NEW_SESSION_PLACEHOLDER = new Object(); public static Session getSession(Long id) { do { Object session = SESSIONS.get(id); if (session == NEW_SESSION_PLACEHOLDER) { continue; } return (Session) session; } while (true); } public static Session acquireSession(Long id) { do { Object session = SESSIONS.get(id); if (session == NEW_SESSION_PLACEHOLDER) { continue; } if (session != null) { return (Session) session; } Object rsp = SESSIONS.putIfAbsent(id, NEW_SESSION_PLACEHOLDER); if (rsp != null) { if (rsp == NEW_SESSION_PLACEHOLDER) continue; return (Session) rsp; } else { Session session1 = new Session(); SESSIONS.put(id, session1); return session1; } } while (true); } public static void invalidateSession(Long id, Session session) { SESSIONS.remove(id, session); } }