Skip to content

Commit

Permalink
Update DB Query
Browse files Browse the repository at this point in the history
  • Loading branch information
lb-migii committed Dec 20, 2023
1 parent 8ced64e commit fc88729
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/bwmarrin/discordgo v0.27.0
github.com/jinzhu/copier v0.4.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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
Expand Down
31 changes: 15 additions & 16 deletions lib/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/tpc3/DuckPolice/lib/config"

"github.com/bwmarrin/discordgo"
"github.com/jinzhu/copier"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/driver/sqliteshim"
Expand Down Expand Up @@ -52,12 +53,20 @@ func LoadGuild(id string) *config.Guild {
return val
}
var rawData string
guild := config.CurrentConfig.Guild
currGuild := config.CurrentConfig.Guild
var guild config.Guild
err := copier.Copy(&guild, &currGuild)
if err != nil {
log.Print("WARN: Guild deep copy error: ", err)
}
row := db.QueryRow("SELECT data FROM config WHERE guild = ?", id)
err := row.Scan(&rawData)
err = row.Scan(&rawData)

if err == nil {
json.Unmarshal([]byte(rawData), &guild)
err = json.Unmarshal([]byte(rawData), &guild)
if err != nil {
log.Print("WARN: JSON parsing error: ", err)
}
} else if err == sql.ErrNoRows {
// skip
} else {
Expand Down Expand Up @@ -117,12 +126,7 @@ func AddLog(orgMsg *discordgo.MessageCreate, guildId, groupId string, content *s
}

func SearchLog(session *discordgo.Session, guildId, groupId string, content *string) (found bool, dst common.Log) {
src := common.Log{
Guild: guildId,
GroupID: groupId,
Content: *content,
}
_, err := db.NewSelect().Model(&src).Where("guild = ?", guildId).Where("groupid = ?", groupId).Where("content = ?", content).Exec(context.Background(), &dst)
_, err := db.NewSelect().Model(&common.Log{}).Where("guild = ?", guildId).Where("groupid = ?", groupId).Where("content = ?", content).Exec(context.Background(), &dst)
if err != nil {
if err == sql.ErrNoRows {
found = false
Expand All @@ -145,12 +149,7 @@ func SearchLog(session *discordgo.Session, guildId, groupId string, content *str
}

func DeleteLog(guildId, groupId string, content *string) {
src := common.Log{
Guild: guildId,
GroupID: groupId,
Content: *content,
}
_, err := db.NewDelete().Model(&src).Where("guild = ?", guildId).Where("groupid = ?").Where("content = ?").Exec(context.Background())
_, err := db.NewDelete().Model(&common.Log{}).Where("guild = ?", guildId).Where("groupid = ?", groupId).Where("content = ?", content).Exec(context.Background())
if err != nil {
log.Print("WARN: deleting log error: ", err)
}
Expand All @@ -161,7 +160,7 @@ func timeToSnowflake(t time.Time) int64 {
}

func CleanOldLog() (int64, error) {
res, err := db.NewDelete().Table("log").Where("messageid < ?", timeToSnowflake(time.Now().Add(-time.Duration(config.CurrentConfig.LogPeriod)*time.Second))).Exec(context.Background())
res, err := db.NewDelete().Model(&common.Log{}).Where("messageid < ?", timeToSnowflake(time.Now().Add(-time.Duration(config.CurrentConfig.LogPeriod)*time.Second))).Exec(context.Background())
if err != nil {
return 0, err
}
Expand Down
4 changes: 2 additions & 2 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, dst := db.SearchLog(session, orgMsg.GuildID, groupId, &url)
found, msg := db.SearchLog(session, orgMsg.GuildID, groupId, &url)
if found {
message += "\nhttps://discord.com/channels/" + orgMsg.GuildID + "/" + dst.ChannelID + "/" + dst.MessageID
message += "\nhttps://discord.com/channels/" + orgMsg.GuildID + "/" + msg.ChannelID + "/" + msg.MessageID
} else {
db.AddLog(orgMsg, orgMsg.GuildID, groupId, &url, orgMsg.ChannelID, orgMsg.ID)
}
Expand Down

0 comments on commit fc88729

Please sign in to comment.