Skip to content

Commit

Permalink
Merge pull request #1 from TheTipo01/dev
Browse files Browse the repository at this point in the history
feat: polls
  • Loading branch information
TheTipo01 authored Oct 8, 2022
2 parents 828d60c + f0b539f commit db57904
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 20 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
-
name: Set up Go
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.19.1
-
name: Run GoReleaser
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# messageCounter

[![Go Report Card](https://goreportcard.com/badge/github.com/TheTipo01/messageCounter)](https://goreportcard.com/report/github.com/TheTipo01/messageCounter)

messageCounter - a discord bot to track messages and ghostpings
Expand All @@ -9,7 +10,11 @@ messageCounter - a discord bot to track messages and ghostpings
- Sends random message every monday from a certain channel in a certain guild
- Send a nice as the message number 69420

For the random message part, you need to add a row to the config table with the server ID (guildID), the channel from where to get messages (channelID), and where to send the random message (channelToID). You can also add a offset for the message count part of the bot, as sometimes the numbers that the bot get and the one that discord returns are different.
For the random message part, you need to add a row to the config table with the server ID (guildID), the channel from
where to get messages (channelID), and where to send the random message (channelToID). You can also add a offset for the
message count part of the bot, as sometimes the numbers that the bot get and the one that discord returns are different.

## Install
Get a release for your system in the [release](https://github.com/TheTipo01/messageCounter/releases) tab, modify the provided `example_config.yml`, deploy the website on your favourite webserver, and you're good to go!

Get a release for your system in the [release](https://github.com/TheTipo01/messageCounter/releases) tab, modify the
provided `example_config.yml`, deploy the website on your favourite webserver, and you're good to go!
205 changes: 205 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
)

Expand Down Expand Up @@ -138,6 +139,84 @@ var (
},
},
},
{
Name: "poll",
Description: "Creates a poll",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "question",
Description: "The question to ask",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "group",
Description: "A group of people to poll",
Required: true,
},
},
},
{
Name: "creategroup",
Description: "Creates a group of people to poll",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "name",
Description: "The name of the group",
Required: true,
},
},
},
{
Name: "deletegroup",
Description: "Deletes a group of people to poll",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "name",
Description: "The name of the group",
Required: true,
},
},
},
{
Name: "addmember",
Description: "Adds a member to a group",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "group",
Description: "The selected group",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionUser,
Name: "member",
Description: "The member to add",
Required: true,
},
},
},
{
Name: "removemember",
Description: "Removes a member to a group",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "group",
Description: "The selected group",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionUser,
Name: "member",
Description: "The member to remove",
Required: true,
},
},
},
}

// Handler
Expand Down Expand Up @@ -640,5 +719,131 @@ var (

sendEmbedInteraction(s, embed.MessageEmbed, i.Interaction)
},
"poll": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var n int
question := i.ApplicationCommandData().Options[0].StringValue()
group := i.ApplicationCommandData().Options[1].StringValue()

// If the group doesn't exist, just quit
_ = db.QueryRow("SELECT COUNT(*) FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, group).Scan(&n)
if n == 0 {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group not found!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
return
}

// Create the poll message
msg, _ := s.ChannelMessageSendEmbed(i.ChannelID, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", question).MessageEmbed)

// Adds the message to the db
_, _ = db.Exec("INSERT INTO pollState (messageID, question, groupName, guildID) VALUES (?, ?, ?, ?)", msg.ID, question, group, i.GuildID)

// And the map
server[i.GuildID].polls[msg.ID] = true

// Add the reactions
_ = s.MessageReactionAdd(msg.ChannelID, msg.ID, "👍")
_ = s.MessageReactionAdd(msg.ChannelID, msg.ID, "👎")
},

"creategroup": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var n int
_ = db.QueryRow("SELECT COUNT(*) FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue()).Scan(&n)
if n == 0 {
_, _ = db.Exec("INSERT INTO pollsGroup (serverID, name, createdBy) VALUES (?, ?, ?)", i.GuildID, i.ApplicationCommandData().Options[0].StringValue(), i.Member.User.ID)
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group created!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
} else {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group already exists!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
}
},

"deletegroup": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var userID string
_ = db.QueryRow("SELECT createdBy FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue()).Scan(&userID)

if userID == "" {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group not found!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
}

if userID == i.Member.User.ID {
_, _ = db.Exec("DELETE FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue())
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group deleted!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
} else {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "You are not the owner of this group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
}
},

// Adds a member to a group. Only the creator of the group can add people
"addmember": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var createdBy, userIDs string
_ = db.QueryRow("SELECT createdBy, userIDs FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue()).Scan(&createdBy, &userIDs)

if createdBy == "" {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group not found!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
}

if createdBy == i.Member.User.ID {
user := i.ApplicationCommandData().Options[1].UserValue(s)

// If the user is already in the group, just return
if strings.Contains(userIDs, user.ID) {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "User already in the group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
return
}

// Gets the old members, and adds the new one
if userIDs == "" {
userIDs = user.ID
} else {
userIDs += "," + user.ID
}

_, _ = db.Exec("UPDATE pollsGroup SET userIDs=? WHERE serverID=? AND name=?", userIDs, i.GuildID, i.ApplicationCommandData().Options[0].StringValue())

// Adds the nickname to the database
_, _ = db.Exec("INSERT IGNORE INTO users (id, nickname) VALUES (?, ?)", user.ID, user.Username)

sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Member added!").MessageEmbed, i.Interaction, time.Second*3)
} else {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "You are not the owner of this group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
}
},

// Removes a member from a group. Only the creator of the group can remove people
"removemember": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var createdBy, userIDs string
_ = db.QueryRow("SELECT createdBy, userIDs FROM pollsGroup WHERE serverID=? AND name=?", i.GuildID, i.ApplicationCommandData().Options[0].StringValue()).Scan(&createdBy, &userIDs)

if createdBy == "" {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "Group not found!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
}

if createdBy == i.Member.User.ID {
// If the user is not in the group, just return
if !strings.Contains(userIDs, i.ApplicationCommandData().Options[1].UserValue(s).ID) {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "User not in the group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
return
}

// Gets the old members, and adds the new one
userIDs = strings.Replace(userIDs, ","+i.ApplicationCommandData().Options[1].UserValue(nil).ID, "", -1)

_, _ = db.Exec("UPDATE pollsGroup SET userIDs=? WHERE serverID=? AND name=?", userIDs, i.GuildID, i.ApplicationCommandData().Options[0].StringValue())

} else {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Poll", "You are not the owner of this group!").
SetColor(0x7289DA).MessageEmbed, i.Interaction, time.Second*3)
}
},
}
)
13 changes: 13 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,16 @@ func sendQuoteToServer(s *discordgo.Session, guildID, channelID, channelToID str
return
}
}

// Returns the nickname of a person given its ID
func getNickname(id string) string {
var nickname string

err := db.QueryRow("SELECT nickname FROM users WHERE id=?", id).Scan(&nickname)
if err != nil {
lit.Error("Can't get nickname, %s", err)
return ""
}

return nickname
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/psykhi/wordclouds v0.0.0-20220728072901-2d77dabdd4fd
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 // indirect
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect
golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 // indirect
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 // indirect
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 h1:WJywXQVIb56P2kAvXeMGTIgQ1ZHQxR60+F9dLsodECc=
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b h1:huxqepDufQpLLIRXiVkTvnxrzJlpwmIWAObmcCcUFr0=
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 h1:Lj6HJGCSn5AjxRAH2+r35Mir4icalbqku+CLUtjnvXY=
golang.org/x/image v0.0.0-20220902085622-e7cb96979f69/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 h1:ZrnxWX62AgTKOSagEqxvb3ffipvEDX2pl7E1TdqLqIc=
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 h1:cu5kTvlzcw1Q5S9f5ip1/cpiB4nXvw1XYzFPGgzLUOY=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875 h1:AzgQNqF+FKwyQ5LbVrVqOcuuFB67N47F9+htZYH0wFM=
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
Loading

0 comments on commit db57904

Please sign in to comment.