Skip to content

Commit

Permalink
fix: model is now saved every time
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTipo01 committed Mar 16, 2022
1 parent 3e8e395 commit d05659a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
12 changes: 0 additions & 12 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ var (
},
},
},
{
Name: "rebuildmodel",
Description: "Rebuilds the markov model for the current guild",
},
{
Name: "markov",
Description: "Generates a message from the current markov chain",
Expand Down Expand Up @@ -580,14 +576,6 @@ var (
SetColor(0x7289DA).MessageEmbed, i.Interaction)
},

"rebuildmodel": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Successful", "Operation started, this may (and will) take a while").
SetColor(0x7289DA).MessageEmbed, i.Interaction)

server[i.GuildID].model = buildModel(i.GuildID)
saveModel(i.GuildID)
},

"markov": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
tokens := []string{gomarkov.StartToken}
for tokens[len(tokens)-1] != gomarkov.EndToken {
Expand Down
6 changes: 5 additions & 1 deletion database.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func addMessage(m *discordgo.Message) {
if m.Author != nil {
insertAuthor(m)
_, err = db.Exec("INSERT INTO messages (guildID, channelID, messageID, authorID, message) VALUES (?, ?, ?, ?, ?)", m.GuildID, m.ChannelID, m.ID, m.Author.ID, inJSON)

if !m.Author.Bot {
server[m.GuildID].model.Add(strings.Split(m.Content, " "))
saveModel(m.GuildID)
}
} else {
_, err = db.Exec("INSERT INTO messages (guildID, channelID, messageID, message) VALUES (?, ?, ?, ?)", m.GuildID, m.ChannelID, m.ID, inJSON)
}
Expand All @@ -45,7 +50,6 @@ func addMessage(m *discordgo.Message) {
lit.Error("Error while inserting message into db, %s", err)
}

server[m.GuildID].model.Add(strings.Split(m.Content, " "))
}

func deleteMessage(s *discordgo.Session, m *discordgo.Message) {
Expand Down
23 changes: 10 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/bwmarrin/lit"
_ "github.com/go-sql-driver/mysql"
"github.com/kkyr/fig"
"github.com/mb-14/gomarkov"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -62,12 +63,9 @@ func init() {
execQuery(tblMessages, tblUsers, tblServers, tblChannels, tblPings, tblConfig)

// And add the everyone user to the table, as we use that for logging @everyone and @here
_, err = db.Exec("INSERT INTO users (id, nickname) VALUES(?, ?)", "everyone", "everyone")
_, err = db.Exec("INSERT IGNORE INTO users (id, nickname) VALUES(?, ?)", "everyone", "everyone")
if err != nil {
str := err.Error()
if !strings.HasPrefix(str, "Error 1062: Duplicate entry") {
lit.Error("Error inserting user everyone in the database, %s", str)
}
lit.Error("Error inserting user everyone in the database, %s", err.Error())
}
}

Expand Down Expand Up @@ -107,6 +105,7 @@ func main() {
}

loadScheduler(dg)
loadModel()

// Wait here until CTRL-C or other term signal is received.
lit.Info("messageCounter is now running. Press CTRL-C to exit.")
Expand Down Expand Up @@ -152,12 +151,16 @@ func guildCreate(s *discordgo.Session, g *discordgo.GuildCreate) {
)

if server[g.ID] == nil {
server[g.ID] = &Server{numberOfMessages: 0}
_, err = db.Exec("INSERT INTO servers (id, name, model) VALUES(?, ?, '')", g.ID, g.Name)
if err != nil {
lit.Error("Error inserting into the database: %s", err.Error())
}

server[g.ID] = &Server{numberOfMessages: 0, model: &gomarkov.Chain{}}
}

for _, c := range g.Channels {
if c.Type != discordgo.ChannelTypeGuildVoice && c.Type != discordgo.ChannelTypeGuildCategory {

for {
_ = db.QueryRow("SELECT messageID FROM messages WHERE guildID=? AND channelID=? ORDER BY messageID LIMIT 1", c.GuildID, c.ID).Scan(&beforeID)
messages, err = s.ChannelMessages(c.ID, 100, beforeID, "", "")
Expand Down Expand Up @@ -192,12 +195,6 @@ func guildCreate(s *discordgo.Session, g *discordgo.GuildCreate) {
lit.Debug("Added offset of %d on guild \"%s\". New total of message %d", offset, g.Name, server[g.ID].numberOfMessages)
}

server[g.ID].model = loadModel(g.ID)
if server[g.ID].model == nil {
lit.Info("Model for guild %s doesn't exist. Building it right now", g.ID)
server[g.ID].model = buildModel(g.ID)
}

saveModel(g.ID)
}

Expand Down
27 changes: 17 additions & 10 deletions markov.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,26 @@ func saveModel(guildID string) {
}
}

// loadModel loads the model from the db or builds it if it doesn't exist
func loadModel(guildID string) *gomarkov.Chain {
// loadModel loads the model from the db
func loadModel() {
var (
data []byte
chain gomarkov.Chain
data []byte
chain gomarkov.Chain
guildID string
)

_ = db.QueryRow("SELECT model FROM servers WHERE id=?", guildID).Scan(&data)
rows, _ := db.Query("SELECT model, id FROM servers")

if len(data) == 0 {
return nil
} else {
_ = json.Unmarshal(data, &chain)
return &chain
for rows.Next() {
_ = rows.Scan(&data, &guildID)

server[guildID] = &Server{numberOfMessages: 0, model: nil}

if len(data) == 0 {
server[guildID].model = buildModel(guildID)
} else {
_ = json.Unmarshal(data, &chain)
server[guildID].model = &chain
}
}
}

0 comments on commit d05659a

Please sign in to comment.