Skip to content

Commit

Permalink
Separated stats command
Browse files Browse the repository at this point in the history
And added one to count messages
  • Loading branch information
TheTipo01 committed Mar 28, 2021
1 parent 59409b3 commit b8ea312
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 35 deletions.
229 changes: 202 additions & 27 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,51 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/lit"
jsoniter "github.com/json-iterator/go"
"regexp"
)

var (
// Commands
commands = []*discordgo.ApplicationCommand{
{
Name: "stats",
Description: "Prints stats for a given channel, or if not specified for the entire server.",
Name: "characters",
Description: "Prints the number of characters sent for a channel, or the entire server if omitted",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel to get stats for",
Required: false,
},
},
},
{
Name: "words",
Description: "Prints the number of words sent for a channel, or the entire server if omitted",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel to get stats for",
Required: false,
},
},
},
{
Name: "messages",
Description: "Prints the number of messages sent for a channel, or the entire server if omitted",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel to get stats for",
Required: false,
},
},
},
{
Name: "charsPerMex",
Description: "Prints the number of characters per message sent for a channel, or the entire server if omitted",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Expand All @@ -27,20 +64,17 @@ var (

// Handler
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
// Prints stats for a given channel, or if not specified for the entire server.
"stats": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Prints the number of characters sent for a given channel, or if not specified for the entire server
"characters": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var (
mex *sql.Rows
err error
json = jsoniter.ConfigFastest
messageJSON, toSend string
m discordgo.Message
cont, authorNil int
words = make(map[string]int)
cont int
characters = make(map[string]int)
people = make(map[string]string)
messages = make(map[string]int)
charPerMex = make(map[string]int)
)

// If there's a specified channel, use it in the query
Expand Down Expand Up @@ -70,41 +104,66 @@ var (
if m.Author != nil {
characters[m.Author.ID] += len(m.Content)
people[m.Author.ID] = m.Author.Username
words[m.Author.ID] += wordCount(m.Content)
messages[m.Author.ID]++
} else {
authorNil++
}
}

// Characters
toSend = ""
chr := sorting(characters)
for i, kv := range chr {
for i, kv := range sorting(characters) {
cont += kv.Value
toSend += fmt.Sprintf("%d) %s: %d\n", i+1, people[kv.Key], kv.Value)
charPerMex[kv.Key] = kv.Value / messages[kv.Key]
}
toSend = fmt.Sprintf("Number of characters sent: %d\n\n", cont) + toSend

sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Characters", toSend).
SetColor(0x7289DA).MessageEmbed, i.Interaction)
},

// Characters per message
toSend = ""
cont = 0
for i, kv := range sorting(charPerMex) {
cont += kv.Value
toSend += fmt.Sprintf("%d) %s: %d\n", i+1, people[kv.Key], kv.Value)
// Prints the number of words sent for a given channel, or if not specified for the entire server
"words": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var (
mex *sql.Rows
err error
json = jsoniter.ConfigFastest
messageJSON, toSend string
m discordgo.Message
cont int
words = make(map[string]int)
people = make(map[string]string)
// Match non-space character sequences.
re = regexp.MustCompile(`[\S]+`)
)

// If there's a specified channel, use it in the query
if len(i.Data.Options) > 0 {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=? AND channelID=?", i.GuildID, i.Data.Options[0].ChannelValue(s).ID)
} else {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=?", i.GuildID)
}
if err != nil {
lit.Error("Can't query database, %s", err)
return
}
toSend = fmt.Sprintf("Number of characters per message sent: %d\n\n", cont) + toSend

sendEmbedInteractionFollowup(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Characters per message", toSend).
SetColor(0x7289DA).MessageEmbed, i.Interaction)
for mex.Next() {
err = mex.Scan(&messageJSON)
if err != nil {
lit.Error("Can't scan m, %s", err)
return
}

err = json.Unmarshal([]byte(messageJSON), &m)
if err != nil {
lit.Error("Can't unmarshal JSON, %s", err)
continue
}

if m.Author != nil {
people[m.Author.ID] = m.Author.Username
words[m.Author.ID] += len(re.FindAllString(m.Content, -1))
}
}

// Words
toSend = ""
cont = 0
for i, kv := range sorting(words) {
cont += kv.Value
toSend += fmt.Sprintf("%d) %s: %d\n", i+1, people[kv.Key], kv.Value)
Expand All @@ -114,5 +173,121 @@ var (
sendEmbedInteractionFollowup(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Words", toSend).
SetColor(0x7289DA).MessageEmbed, i.Interaction)
},

// Prints the number of messages sent for a given channel, or if not specified for the entire server
"messages": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var (
mex *sql.Rows
err error
json = jsoniter.ConfigFastest
messageJSON, toSend string
m discordgo.Message
cont int
people = make(map[string]string)
messages = make(map[string]int)
)

// If there's a specified channel, use it in the query
if len(i.Data.Options) > 0 {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=? AND channelID=?", i.GuildID, i.Data.Options[0].ChannelValue(s).ID)
} else {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=?", i.GuildID)
}
if err != nil {
lit.Error("Can't query database, %s", err)
return
}

for mex.Next() {
err = mex.Scan(&messageJSON)
if err != nil {
lit.Error("Can't scan m, %s", err)
return
}

err = json.Unmarshal([]byte(messageJSON), &m)
if err != nil {
lit.Error("Can't unmarshal JSON, %s", err)
continue
}

if m.Author != nil {
people[m.Author.ID] = m.Author.Username
messages[m.Author.ID]++
}
}

// Messages
for i, kv := range sorting(messages) {
cont += kv.Value
toSend += fmt.Sprintf("%d) %s: %d\n", i+1, people[kv.Key], kv.Value)
}
toSend = fmt.Sprintf("Number of messages: %d\n\n", cont) + toSend

sendEmbedInteractionFollowup(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Messages", toSend).
SetColor(0x7289DA).MessageEmbed, i.Interaction)
},

// Prints stats for a given channel, or if not specified for the entire server.
"charsPerMex": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var (
mex *sql.Rows
err error
json = jsoniter.ConfigFastest
messageJSON, toSend string
m discordgo.Message
cont int
characters = make(map[string]int)
people = make(map[string]string)
messages = make(map[string]int)
charPerMex = make(map[string]int)
)

// If there's a specified channel, use it in the query
if len(i.Data.Options) > 0 {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=? AND channelID=?", i.GuildID, i.Data.Options[0].ChannelValue(s).ID)
} else {
mex, err = db.Query("SELECT message FROM messages WHERE guildID=?", i.GuildID)
}
if err != nil {
lit.Error("Can't query database, %s", err)
return
}

for mex.Next() {
err = mex.Scan(&messageJSON)
if err != nil {
lit.Error("Can't scan m, %s", err)
return
}

err = json.Unmarshal([]byte(messageJSON), &m)
if err != nil {
lit.Error("Can't unmarshal JSON, %s", err)
continue
}

if m.Author != nil {
characters[m.Author.ID] += len(m.Content)
people[m.Author.ID] = m.Author.Username
messages[m.Author.ID]++
}
}

// Characters
for k, v := range characters {
charPerMex[k] = v / messages[k]
}

for i, kv := range sorting(charPerMex) {
cont += kv.Value
toSend += fmt.Sprintf("%d) %s: %d\n", i+1, people[kv.Key], kv.Value)
}
toSend = fmt.Sprintf("Number of characters per message sent: %d\n\n", cont) + toSend

sendEmbedInteractionFollowup(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Characters per message", toSend).
SetColor(0x7289DA).MessageEmbed, i.Interaction)

},
}
)
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/viper"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
)
Expand All @@ -20,8 +19,6 @@ var (
db *sql.DB
// Server structure for all the things we need (currently only the number of messages)
server = make(map[string]*Server)
// Match non-space character sequences.
re = regexp.MustCompile(`[\S]+`)
)

func init() {
Expand Down
5 changes: 0 additions & 5 deletions utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ func sendEmbedInteractionFollowup(s *discordgo.Session, embed *discordgo.Message
}
}

// Counts the number of word in a string
func wordCount(str string) int {
return len(re.FindAllString(str, -1))
}

// Sorts a map into an array
func sorting(classifica map[string]int) []kv {
var ss []kv
Expand Down

0 comments on commit b8ea312

Please sign in to comment.