82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
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
|
|
}
|