feat: 添加Steam查房功能

This commit is contained in:
lixiangwuxian 2024-10-26 03:30:33 +08:00
parent ecdc1ed254
commit 40f1f51cfa
2 changed files with 83 additions and 1 deletions

View File

@ -0,0 +1,81 @@
package steamplaying
import (
"fmt"
"io"
"net/http"
"regexp"
"time"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/util"
"golang.org/x/net/proxy"
)
func init() {
// Register the handler with the server
handler.RegisterHandler("查房", checkSteamPlaying, constants.LEVEL_USER)
}
func checkSteamPlaying(msg model.Message) model.Reply {
tokens := util.SplitN(msg.RawMsg, 2)
if len(tokens) < 2 {
return model.Reply{
ReplyMsg: "请输入正确的steamID",
ReferOriginMsg: true,
FromMsg: msg,
}
}
status, err := checkSteamGameStatus(tokens[1])
if err != nil {
return model.Reply{
ReplyMsg: fmt.Sprintf("检查失败: %v", err),
ReferOriginMsg: true,
FromMsg: msg,
}
}
return model.Reply{
ReplyMsg: status,
ReferOriginMsg: true,
FromMsg: msg,
}
}
func checkSteamGameStatus(steamID string) (string, error) {
dialer, err := proxy.SOCKS5("tcp", "100.94.183.43:2080", nil, proxy.Direct)
if err != nil {
return "", fmt.Errorf("设置SOCKS5代理失败: %v", err)
}
url := fmt.Sprintf("https://steamcommunity.com/id/%s", steamID)
httpTransport := &http.Transport{}
httpTransport.Dial = dialer.Dial
client := &http.Client{
Transport: httpTransport,
Timeout: 10 * time.Second, // 设置请求超时时间
}
resp, err := client.Get(url)
if err != nil {
return "", fmt.Errorf("获取页面失败: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("读取页面内容失败: %v", err)
}
fmt.Print(string(body))
// 提取游戏名称
gameNameRegex := regexp.MustCompile(`(?s)<div\s+class="profile_in_game_name">\s*(.*?)\s*</div>`)
matches := gameNameRegex.FindSubmatch(body)
if len(matches) < 2 {
return "", fmt.Errorf("%s好像没有在玩游戏", steamID)
}
gameName := string(matches[1])
return fmt.Sprintf("该用户正在玩: %s", gameName), nil
}

View File

@ -4,6 +4,7 @@ import (
_ "git.lxtend.com/qqbot/handler/auth"
_ "git.lxtend.com/qqbot/handler/beatleader"
_ "git.lxtend.com/qqbot/handler/echo"
_ "git.lxtend.com/qqbot/handler/exec"
_ "git.lxtend.com/qqbot/handler/getweb"
_ "git.lxtend.com/qqbot/handler/headmaster"
_ "git.lxtend.com/qqbot/handler/help"
@ -12,8 +13,8 @@ import (
_ "git.lxtend.com/qqbot/handler/restart"
_ "git.lxtend.com/qqbot/handler/roll"
_ "git.lxtend.com/qqbot/handler/scoresaber"
_ "git.lxtend.com/qqbot/handler/steamplaying"
_ "git.lxtend.com/qqbot/handler/ticket"
_ "git.lxtend.com/qqbot/handler/urlparser"
_ "git.lxtend.com/qqbot/handler/xibao"
_ "git.lxtend.com/qqbot/handler/exec"
)