Skip to content
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

update main branch #150

Merged
merged 14 commits into from
Jan 29, 2024
10 changes: 5 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Code
Expand All @@ -23,7 +23,7 @@ jobs:
go-version: ['1.21']
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Code
Expand All @@ -37,11 +37,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Go vet
run: |
git submodule init
Expand All @@ -53,4 +53,4 @@ jobs:
with:
only-new-issues: true
skip-pkg-cache: true
skip-build-cache: true
skip-build-cache: true
15 changes: 10 additions & 5 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ on:
# The branches below must be a subset of the branches above
branches: [ "main" ]
schedule:
- cron: '33 15 * * 6'
- cron: '30 15 * * 6'

jobs:
analyze:
Expand All @@ -40,11 +40,16 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
# Run extended queries including queries using machine learning
queries: security-extended
Expand All @@ -61,7 +66,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Expand All @@ -74,6 +79,6 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
41 changes: 0 additions & 41 deletions .github/workflows/update_submodules.yml

This file was deleted.

52 changes: 34 additions & 18 deletions bot/commands/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package message
import (
"context"
"fmt"
"github.com/disgoorg/disgo/rest"
"log/slog"
"net/http"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -687,6 +689,15 @@ func Command(c *components.Components) *generic.Command {
EventHandler: func(c *components.Components, e bot.Event) errors.Error {
switch e := e.(type) {
case *events.GuildMessageCreate:

err, shouldContinue := doTextCommand(e, e)
if err != nil {
return errors.NewError(err)
}
if !shouldContinue {
return nil
}

// 語尾の処理
if err := messageSuffixMessageCreateHandler(e, c); err != nil {
return err
Expand All @@ -698,33 +709,38 @@ func Command(c *components.Components) *generic.Command {
defer c.GetLock("message_pin").Mutex(e.ChannelID).Unlock()

// ピン留めメッセージの処理
if err := func(e *events.GuildMessageCreate, c *components.Components) errors.Error {
id, _, err := webhookutil.GetWebhook(e.Client(), e.ChannelID)
if err := func(event *events.GuildMessageCreate, c *components.Components) errors.Error {
id, _, err := webhookutil.GetWebhook(event.Client(), event.ChannelID)
if err != nil {
err1 := rest.Error{}
if errors.As(err, &err1) && err1.Response.StatusCode == http.StatusForbidden {
// TODO: リファクタ
return errors.NewError(event.Client().Rest().LeaveGuild(event.GuildID))
}
return errors.NewError(err)
}
if e.Message.WebhookID != nil && id == *e.Message.WebhookID {
if event.Message.WebhookID != nil && id == *event.Message.WebhookID {
return nil
}

g, err := c.GuildCreateID(e, e.GuildID)
g, err := c.GuildCreateID(event, event.GuildID)
if err != nil {
return errors.NewError(err)
}
if !g.QueryMessagePins().Where(messagepin.ChannelID(e.ChannelID)).ExistX(e) {
if !g.QueryMessagePins().Where(messagepin.ChannelID(event.ChannelID)).ExistX(event) {
return nil
}
m := g.QueryMessagePins().Where(messagepin.ChannelID(e.ChannelID)).FirstX(e)
m := g.QueryMessagePins().Where(messagepin.ChannelID(event.ChannelID)).FirstX(event)

if m.RateLimit.CheckLimit() {
if m.BeforeID != nil {
if err := e.Client().Rest().DeleteMessage(e.ChannelID, *m.BeforeID); err != nil {
if err := event.Client().Rest().DeleteMessage(event.ChannelID, *m.BeforeID); err != nil {
slog.Error("削除に失敗", "err", err)
m.BeforeID = nil
}
}

message, err := webhookutil.SendWebhook(e.Client(), m.ChannelID,
message, err := webhookutil.SendWebhook(event.Client(), m.ChannelID,
discord.NewWebhookMessageCreateBuilder().
SetAvatarURL(c.Config().Message.PinIconImage).
SetUsername(translate.Message(g.Locale, "components.message.pin.username")).
Expand All @@ -736,10 +752,10 @@ func Command(c *components.Components) *generic.Command {
return errors.NewError(err)
}

m.Update().SetBeforeID(message.ID).SetRateLimit(m.RateLimit).ExecX(e)
slog.Info("ピン留め更新", "cid", e.ChannelID, "mid", e.MessageID)
m.Update().SetBeforeID(message.ID).SetRateLimit(m.RateLimit).ExecX(event)
slog.Info("ピン留め更新", "cid", event.ChannelID, "mid", event.MessageID)
} else {
m.Update().SetRateLimit(m.RateLimit).ExecX(e)
m.Update().SetRateLimit(m.RateLimit).ExecX(event)
}
return nil
}(e, c); err != nil {
Expand Down Expand Up @@ -853,15 +869,15 @@ func messageSuffixMessageCreateHandler(e *events.GuildMessageCreate, c *componen
if err != nil {
return errors.NewError(err)
}
mention_users := make([]snowflake.ID, len(e.Message.Mentions))
mentionUsers := make([]snowflake.ID, len(e.Message.Mentions))
for i, u := range e.Message.Mentions {
mention_users[i] = u.ID
mentionUsers[i] = u.ID
}
replied_user := false
repliedUser := false
if e.Message.MessageReference != nil && e.Message.MessageReference.ChannelID != nil && e.Message.MessageReference.MessageID != nil {
reply_message, err := e.Client().Rest().GetMessage(*e.Message.MessageReference.ChannelID, *e.Message.MessageReference.MessageID)
replyMessage, err := e.Client().Rest().GetMessage(*e.Message.MessageReference.ChannelID, *e.Message.MessageReference.MessageID)
if err == nil {
replied_user = slices.Index(mention_users, reply_message.Author.ID) != -1
repliedUser = slices.Index(mentionUsers, replyMessage.Author.ID) != -1
}
}
if _, err := webhookutil.SendWebhook(e.Client(), e.ChannelID,
Expand All @@ -871,9 +887,9 @@ func messageSuffixMessageCreateHandler(e *events.GuildMessageCreate, c *componen
SetContent(content).
SetAllowedMentions(
&discord.AllowedMentions{
Users: mention_users,
Users: mentionUsers,
Roles: e.Message.MentionRoles,
RepliedUser: replied_user,
RepliedUser: repliedUser,
},
).
Build(),
Expand Down
62 changes: 62 additions & 0 deletions bot/commands/message/text_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package message

import (
"context"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"math/rand"
"regexp"
"strconv"
"strings"
)

func doTextCommand(ctx context.Context, event *events.GuildMessageCreate) (err error, shouldContinue bool) {
c, ok := strings.CutPrefix(event.Message.Content, discord.UserMention(event.Client().ApplicationID()))
if ok {
return nil, true
}
content := strings.Split(strings.TrimSpace(c), " ")

switch {
case diceRollRegex.MatchString(content[0]):
subMatch := diceRollRegex.FindStringSubmatch(content[0])
diceCount, err := strconv.Atoi(subMatch[1])
if err != nil || diceCount < 1 || diceCount > 1000 {
return nil, true
}
diceSize, err := strconv.Atoi(subMatch[2])
if err != nil || diceSize < 1 || diceSize > 1000 {
return nil, true
}
content := "Dice Roll: "
sum := 0
for i := 0; i < diceCount; i++ {
roll := diceRoll(diceSize)
sum += roll
content += strconv.Itoa(roll) + " "
}

content += "\nSum: " + strconv.Itoa(sum)

_, err = event.Client().Rest().CreateMessage(event.ChannelID, discord.NewMessageBuilder().
SetContent(content).
SetMessageReferenceByID(event.Message.ID).
Create(),
)
if err != nil {
return err, false
}

}

return nil, true

}

func diceRoll(size int) int {
return rand.Intn(size) + 1
}

var (
diceRollRegex = regexp.MustCompile(`^(\d+)([dd])(\d+)$`)
)
2 changes: 1 addition & 1 deletion ent/schema/messagepin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package schema

import (
"encoding/json"
"github.com/sabafly/gobot/internal/uuid"
"time"

"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/snowflake/v2"
"github.com/google/uuid"
)

// MessagePin holds the schema definition for the MessagePin entity.
Expand Down
2 changes: 1 addition & 1 deletion ent/schema/messageremind.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"github.com/disgoorg/snowflake/v2"
"github.com/google/uuid"
"github.com/sabafly/gobot/internal/uuid"
)

// MessageRemind holds the schema definition for the MessageRemind entity.
Expand Down
2 changes: 1 addition & 1 deletion ent/schema/rolepanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"entgo.io/ent/schema/field"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/snowflake/v2"
"github.com/google/uuid"
"github.com/sabafly/gobot/internal/uuid"
)

// RolePanel holds the schema definition for the RolePanel entity.
Expand Down
2 changes: 1 addition & 1 deletion ent/schema/rolepaneledit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"github.com/disgoorg/snowflake/v2"
"github.com/google/uuid"
"github.com/sabafly/gobot/internal/uuid"
)

// RolePanelEdit holds the schema definition for the RolePanelEdit entity.
Expand Down
2 changes: 1 addition & 1 deletion ent/schema/rolepanelplaced.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package schema

import (
"github.com/sabafly/gobot/internal/uuid"
"time"

"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/snowflake/v2"
"github.com/google/uuid"
)

// RolePanelPlaced holds the schema definition for the RolePanelPlaced entity.
Expand Down
4 changes: 2 additions & 2 deletions ent/schema/wordsuffix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"github.com/disgoorg/snowflake/v2"
"github.com/google/uuid"
"github.com/sabafly/gobot/internal/uuid"
)

// WordSuffix holds the schema definition for the WordSuffix entity.
Expand All @@ -16,7 +16,7 @@ type WordSuffix struct {
// Fields of the WordSuffix.
func (WordSuffix) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.UUID{}).
field.UUID("id", uuid.New()).
Default(uuid.New),
field.String("suffix").
MaxLen(120).
Expand Down
Loading
Loading