Skip to content

Commit

Permalink
Fixed LoadGuild Error
Browse files Browse the repository at this point in the history
and Disable alert reply mention and remove unnecessary config
  • Loading branch information
lb-migii committed Dec 20, 2023
1 parent d21a0c7 commit 8ced64e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
3 changes: 0 additions & 3 deletions config.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ discord:
token: YOUR_TOKEN_HERE
status: Duck Police is observing you | p.help
db:
kind: sqlite3
parameter: ./db.sqlite
tableprefix: duckpolice_
guild:
prefix: p.
lang: japanese
Expand All @@ -25,4 +23,3 @@ guild:
channel:
thread: threadId
log_period: 2592000

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/bwmarrin/discordgo v0.27.0
github.com/uptrace/bun v1.1.16
github.com/uptrace/bun/dialect/sqlitedialect v1.1.16
github.com/uptrace/bun/driver/sqliteshim v1.1.16
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -20,7 +21,6 @@ require (
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/uptrace/bun/driver/sqliteshim v1.1.16 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down Expand Up @@ -46,6 +48,7 @@ golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
Expand All @@ -67,6 +70,8 @@ modernc.org/cc/v3 v3.41.0 h1:QoR1Sn3YWlmA1T4vLaKZfawdVtSiGx8H+cEojbC7v1Q=
modernc.org/cc/v3 v3.41.0/go.mod h1:Ni4zjJYJ04CDOhG7dn640WGfwBzfE0ecX8TyMB0Fv0Y=
modernc.org/ccgo/v3 v3.16.15 h1:KbDR3ZAVU+wiLyMESPtbtE/Add4elztFyfsWoNTgxS0=
modernc.org/ccgo/v3 v3.16.15/go.mod h1:yT7B+/E2m43tmMOT51GMoM98/MtHIcQQSleGnddkUNI=
modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk=
modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
modernc.org/libc v1.24.1 h1:uvJSeCKL/AgzBo2yYIPPTy82v21KgGnizcGYfBHaNuM=
modernc.org/libc v1.24.1/go.mod h1:FmfO1RLrU3MHJfyi9eYYmZBfi/R+tqZ6+hQ3yQQUkak=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
Expand All @@ -79,5 +84,7 @@ modernc.org/sqlite v1.25.0 h1:AFweiwPNd/b3BoKnBOfFm+Y260guGMF+0UFk0savqeA=
modernc.org/sqlite v1.25.0/go.mod h1:FL3pVXie73rg3Rii6V/u5BoHlSoyeZeIgKZEgHARyCU=
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
modernc.org/tcl v1.15.2 h1:C4ybAYCGJw968e+Me18oW55kD/FexcHbqH2xak1ROSY=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
modernc.org/z v1.7.3 h1:zDJf6iHjrnB+WRD88stbXokugjyc0/pB91ri1gO6LZY=
2 changes: 0 additions & 2 deletions lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ type Config struct {
Status string
}
Db struct {
Kind string
Parameter string
Tableprefix string
}
Guild Guild
LogPeriod int64 `yaml:"log_period"`
Expand Down
24 changes: 13 additions & 11 deletions lib/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func LoadGuild(id string) *config.Guild {
}
var rawData string
guild := config.CurrentConfig.Guild
rows, _ := db.QueryContext(context.Background(), "SELECT * FROM config")
err := db.ScanRows(context.Background(), rows, &rawData)
row := db.QueryRow("SELECT data FROM config WHERE guild = ?", id)
err := row.Scan(&rawData)

if err == nil {
json.Unmarshal([]byte(rawData), &guild)
Expand Down Expand Up @@ -110,19 +110,18 @@ func AddLog(orgMsg *discordgo.MessageCreate, guildId, groupId string, content *s
ChannelID: channelid,
MessageID: messageid,
}
db.NewInsert().Model(&src).Exec(context.Background())
_, err := db.NewInsert().Model(&src).Exec(context.Background())
if err != nil {
log.Print("WARN: adding log error: ", err)
}
}

func SearchLog(session *discordgo.Session, guildId, groupId string, content *string) (found bool, channelid string, messageid string) {
func SearchLog(session *discordgo.Session, guildId, groupId string, content *string) (found bool, dst common.Log) {
src := common.Log{
Guild: guildId,
GroupID: groupId,
Content: *content,
}
dst := common.Log{
ChannelID: channelid,
MessageID: messageid,
}
_, err := db.NewSelect().Model(&src).Where("guild = ?", guildId).Where("groupid = ?", groupId).Where("content = ?", content).Exec(context.Background(), &dst)
if err != nil {
if err == sql.ErrNoRows {
Expand All @@ -137,12 +136,12 @@ func SearchLog(session *discordgo.Session, guildId, groupId string, content *str
}
if err == nil {
found = true
return found, dst.ChannelID, dst.MessageID
return
} else {
DeleteLog(guildId, groupId, content)
}
}
return found, dst.ChannelID, dst.MessageID
return
}

func DeleteLog(guildId, groupId string, content *string) {
Expand All @@ -151,7 +150,10 @@ func DeleteLog(guildId, groupId string, content *string) {
GroupID: groupId,
Content: *content,
}
db.NewDelete().Model(&src).Where("guild = ?", guildId).Where("groupid = ?").Where("content = ?").Exec(context.Background())
_, err := db.NewDelete().Model(&src).Where("guild = ?", guildId).Where("groupid = ?").Where("content = ?").Exec(context.Background())
if err != nil {
log.Print("WARN: deleting log error: ", err)
}
}

func timeToSnowflake(t time.Time) int64 {
Expand Down
15 changes: 12 additions & 3 deletions lib/handler/url_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func urlCheck(session *discordgo.Session, orgMsg *discordgo.MessageCreate) {
message := guild.Alert.Message

for _, url := range parsed {
found, channelid, messageid := db.SearchLog(session, orgMsg.GuildID, groupId, &url)
found, dst := db.SearchLog(session, orgMsg.GuildID, groupId, &url)
if found {
message += "\nhttps://discord.com/channels/" + orgMsg.GuildID + "/" + channelid + "/" + messageid
message += "\nhttps://discord.com/channels/" + orgMsg.GuildID + "/" + dst.ChannelID + "/" + dst.MessageID
} else {
db.AddLog(orgMsg, orgMsg.GuildID, groupId, &url, orgMsg.ChannelID, orgMsg.ID)
}
Expand Down Expand Up @@ -55,7 +55,16 @@ func urlCheck(session *discordgo.Session, orgMsg *discordgo.MessageCreate) {
if err != nil {
common.UnknownError(session, orgMsg, guild.Lang, err)
}
session.ChannelMessageSendReply(orgMsg.ChannelID, message, orgMsg.Reference())
reply := discordgo.MessageSend{}
reply.Content = message
reply.Reference = orgMsg.Reference()
reply.AllowedMentions = &discordgo.MessageAllowedMentions{
RepliedUser: false,
}
_, err = session.ChannelMessageSendComplex(orgMsg.ChannelID, &reply)
if err != nil {
log.Print("Failed to send reply: ", err)
}
}
}

Expand Down

0 comments on commit 8ced64e

Please sign in to comment.