8000 Feat pressure by ikafly144 · Pull Request #55 · sabafly/gobot · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Feat pressure #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions api_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (h *WebsocketHandler) HandlerGuildDelete(w http.ResponseWriter, r *http.Req
if err := requests.Unmarshal(r, &guildDelete); err != nil {
logging.Error("[内部] [REST] アンマーシャルできませんでした %s", err)
w.WriteHeader(400)
err := json.NewEncoder(w).Encode(map[string]any{"status": "400 Bad Request"})
err := json.NewEncoder(w).Encode(map[string]any{"status": "400 Bad Request", "error": err})
if err != nil {
logging.Error("応答に失敗 %s", err)
}
Expand All @@ -108,11 +108,47 @@ func (h *WebsocketHandler) HandlerGuildDelete(w http.ResponseWriter, r *http.Req
}
}

// ギルドフィーチャー関連

var featureCache = caches.NewCacheManager[[]GuildFeature](nil)

func HandlerGuildFeaturePost(w http.ResponseWriter, r *http.Request) {
//データ取り出す
guildFeature := GuildFeature{}
if err := requests.Unmarshal(r, &guildFeature); err != nil {
logging.Error("[内部] [REST] アンマーシャルできませんでした %s", err)
w.WriteHeader(400)
err := json.NewEncoder(w).Encode(map[string]any{"status": "400 Bad Request", "error": err})
if err != nil {
logging.Error("応答に失敗 %s", err)
}
return
}

err := db.Save(&guildFeature)
if err != nil {
logging.Error("[内部] [REST] データベースへの書き込みに失敗 %s", err)
w.WriteHeader(400)
err := json.NewEncoder(w).Encode(map[string]any{"status": "400 Bad Request"})
if err != nil {
logging.Error("応答に失敗 %s", err)
}
return
}

caches.Append(featureCache, guildFeature.ID, guildFeature)

err = json.NewEncoder(w).Encode(map[string]any{"status": "200 OK"})
if err != nil {
logging.Error("応答に失敗 %s", err)
}
}

// ----------------------------------------------------------------
// メッセージ関連
// ----------------------------------------------------------------

var messageLog = caches.NewCacheManager[MessageLogs](nil)
var messageLog = caches.NewCacheManager[[]MessageLog](nil)

func (h *WebsocketHandler) HandlerMessageCreate(w http.ResponseWriter, r *http.Request) {
// データを取り出す
Expand Down Expand Up @@ -168,14 +204,8 @@ func (h *WebsocketHandler) HandlerMessageCreate(w http.ResponseWriter, r *http.R
return
}

messages, err := messageLog.Get(messageCreate.Author.ID)
if err != nil {
messages = MessageLogs{}
}

messages = append(messages, log)
caches.Append(messageLog, messageCreate.Author.ID, log)

messageLog.Set(messageCreate.Author.ID, messages)
err = json.NewEncoder(w).Encode(map[string]any{"status": "200 OK"})
if err != nil {
logging.Error("応答に失敗 %s", err)
Expand All @@ -197,14 +227,14 @@ func (h *WebsocketHandler) HandlerStaticsUserMessage(w http.ResponseWriter, r *h
return
}

logs := MessageLogs{}
logs := []MessageLog{}

err := db.Find(&logs, "user_id = ?", query.Get("user"))
if err != nil {
logging.Warning("見つからなかった %s", err)
}

res := MessageLogs{}
res := []MessageLog{}

for _, v := range logs {
if v.GuildID == query.Get("guild") {
Expand Down
47 changes: 47 additions & 0 deletions command_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,53 @@ func CommandTextPing(s *discordgo.Session, i *discordgo.InteractionCreate) {
}
}

// チャンネルエクスペリエンスを設定するコマンド
func CommandTextFeature(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
switch options[0].Name {
case "enable":
options = options[0].Options
for _, acido := range options {
// featureオプションじゃなかったらスキップ
if acido.Name != "feature" {
continue
}

err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: setEmbedProperties([]*discordgo.MessageEmbed{
{
Description: translate.Translate(
i.Locale,
"command_text_feature_add_message_select_target",
map[string]any{"Target": translate.Message(i.Locale, "channel")},
),
},
}),
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.SelectMenu{
CustomID: "text_feature_enable_" + acido.StringValue(),
MenuType: discordgo.ChannelSelectMenu,
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText},
},
},
},
},
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error("レスポンスに失敗 %s", err)
}
}
case "disable":
options = options[0].Options
}
}

// ----------------------------------------------------------------
// ユーザーコマンド
// ----------------------------------------------------------------
Expand Down
30 changes: 29 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
gobot "github.com/sabafly/gobot/pkg/bot"
)

var DMPermission = true
var DMPermission = false

func commands() gobot.ApplicationCommands {
return gobot.ApplicationCommands{
Expand All @@ -33,6 +33,34 @@ func commands() gobot.ApplicationCommands {
},
Handler: CommandTextPing,
},
{
ApplicationCommand: &discordgo.ApplicationCommand{
Name: "feature",
Description: "manage feature command",
DMPermission: &DMPermission,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "enable",
Description: "Enable an experiment",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "feature",
Description: "kind of feature",
Type: discordgo.ApplicationCommandOptionString,
Choices: []*discordgo.ApplicationCommandOptionChoice{
{
Name: "pressure",
Value: "1",
},
},
},
},
},
},
},
Handler: CommandTextFeature,
},
{
ApplicationCommand: &discordgo.ApplicationCommand{
Name: "info",
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.2.0 // indirect
Expand All @@ -37,6 +38,7 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVL
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -143,8 +144,9 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
Expand Down
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ func main() {
Handler: func(ctx *gin.Context) { wh.HandlerGuildDelete(ctx.Writer, ctx.Request) },
},
},

Child: []*apinternal.Page{
{
Path: "feature",
Handlers: []*apinternal.Handler{
{
Method: "POST",
Handler: func(ctx *gin.Context) {},
},
},
},
},
},
{
Path: "message",
Expand Down
2 changes: 2 additions & 0 deletions pkg/bot/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var (
EndpointGuild = EndpointAPI + "guild"
EndpointMessage = EndpointAPI + "message"

EndpointGuildFeature = EndpointGuild + "/feature"

EndpointStatics = EndpointAPI + "statics/"
EndpointStaticsUser = EndpointStatics + "user"
EndpointStaticsUserMessage = EndpointStaticsUser + "/message"
Expand Down
13 changes: 13 additions & 0 deletions pkg/bot/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ func (a *Api) messageCreateCall(m *discordgo.MessageCreate) {
}
}

// ----------------------------------------------------------------
// ギルド
// ----------------------------------------------------------------

func (a *Api) GuildFeatureCreate(feature *GuildFeature) (err error) {
_, err = a.Request("POST", EndpointGuildFeature, feature)
if err != nil {
return err
}

return nil
}

// ----------------------------------------------------------------
// 統計
// ----------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions pkg/bot/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ type MessageLog struct {
Content string
Bot bool
}

type GuildFeature struct {
ID string
ChannelID string
FeatureID int
}
10 changes: 10 additions & 0 deletions pkg/lib/caches/caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,13 @@ func (c *CacheManager[T]) Len() int {
defer c.sync.Unlock()
return len(c.caches)
}

func Append[T any](c *CacheManager[[]T], key string, v T) (data []T) {
d, err := c.Get(key)
if err != nil {
d = []T{}
}
d = append(d, v)
c.Set(key, d)
return d
}
3 changes: 2 additions & 1 deletion pkg/lib/translate/ja.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ hour: "時間"
minute: "分"
seconds: "秒"
all_time: "全期間"
channel: "チャンネル"
channel: "チャンネル"
command_text_feature_add_message_select_target: "対象となる{{.Target}}を選んで下さい"
6 changes: 5 additions & 1 deletion structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ type MessageLog struct {
Bot bool
}

type MessageLogs []MessageLog
type GuildFeature struct {
Model
ChannelID string
FeatureID int
}
3 changes: 2 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
package main

import (
"fmt"
"runtime/debug"
"sort"
"time"
Expand Down Expand Up @@ -49,7 +50,7 @@ func ErrorTraceEmbed(locale discordgo.Locale, err error) []*discordgo.MessageEmb
embeds := []*discordgo.MessageEmbed{
{
Title: "💥" + translate.Message(locale, "error_occurred_embed_message"),
Description: "```" + string(stack) + "```",
Description: fmt.Sprintf("%s\r```%s```", err, string(stack)),
Color: 0xff0000,
},
}
Expand Down
0