grim/wasdead

Parents c06bc2e462ed
Children a926ffc57913
Major overhaul to the database, please look at issue #13 for more information on why we assume channels[] is always 1
--- a/database/bitcask.go Mon Apr 08 23:38:13 2019 -0500
+++ b/database/bitcask.go Tue Apr 09 13:14:34 2019 -0700
@@ -2,6 +2,7 @@
import (
"github.com/prologic/bitcask"
+ log "github.com/sirupsen/logrus"
)
type db_bitcask struct {
@@ -16,18 +17,31 @@
})
}
-func (db db_bitcask) GetChannel(guildid string) string {
+func (db db_bitcask) GetGuild(guildid string) *Guild {
data, err := db.database.Get(guildid)
+ if err != nil || len(data) == 0 {
+ log.Errorf("Bitcask GetGuild error: %s", err)
+
+ // We need to return a empty Guild object
+ return &Guild{}
+ }
+
+ guild := toGuildObject(data)
+
+ return &guild
+}
+
+func (db db_bitcask) AddGuild(guild *Guild) {
+ err := db.database.Put(guild.ID, toJsonObject(*guild))
+
if err != nil {
- return ""
+ log.Errorf("Bitcask AddGuild error: %s", err)
}
-
- return string(data)
}
-func (db db_bitcask) SetChannel(guildid, channel string) {
- db.database.Put(guildid, []byte(channel))
+func (db db_bitcask) UpdateGuild(guild *Guild) {
+ db.AddGuild(guild)
}
func (db db_bitcask) Close() {
--- a/database/database.go Mon Apr 08 23:38:13 2019 -0500
+++ b/database/database.go Tue Apr 09 13:14:34 2019 -0700
@@ -1,8 +1,15 @@
package database
+import (
+ "encoding/json"
+
+ log "github.com/sirupsen/logrus"
+)
+
type Database interface {
- SetChannel(guildid, channel string)
- GetChannel(guildid string) string
+ GetGuild(guildid string) *Guild
+ AddGuild(guild *Guild)
+ UpdateGuild(guild *Guild)
Close()
}
@@ -30,3 +37,25 @@
return ret
}
+
+func toGuildObject(data []byte) Guild {
+ var item Guild
+ err := json.Unmarshal(data, &item)
+ if err != nil {
+ log.Errorf("toGuildObject failed: %#v", err)
+ return Guild{}
+ }
+
+ return item
+}
+
+func toJsonObject(guild Guild) []byte {
+ data, err := json.Marshal(guild)
+
+ if err != nil {
+ log.Errorf("toJsonObject failed: %#v", err)
+ return nil
+ }
+
+ return data
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/database/guild.go Tue Apr 09 13:14:34 2019 -0700
@@ -0,0 +1,41 @@
+package database
+
+// TODO: enable more features like config
+type Guild struct {
+ ID string
+ Channels []Channel
+}
+
+// TODO: add methods to channel that let us use this system more
+type Channel struct {
+ ID string
+}
+
+func (guild *Guild) HasChannel(channel string) bool {
+ for _, g := range guild.Channels {
+ if g.ID == channel {
+ return true
+ }
+ }
+
+ return false
+}
+
+func (guild *Guild) AddChannel(channel string) {
+ if guild.HasChannel(channel) {
+ return
+ }
+
+ guild.Channels = append(guild.Channels, Channel{channel})
+}
+
+func (guild *Guild) RemoveChannel(channel string) {
+ newChannels := make([]Channel, 0)
+ for _, g := range guild.Channels {
+ if g.ID != channel {
+ newChannels = append(newChannels, g)
+ }
+ }
+
+ guild.Channels = newChannels
+}
--- a/database/redis.go Mon Apr 08 23:38:13 2019 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-package database
-
-import (
- "github.com/go-redis/redis"
-)
-
-type db_redis struct {
- database *redis.Client
-}
-
-func init() {
- database := redis.NewClient(&redis.Options{
- Addr: "localhost:6379",
- Password: "", // no password set
- DB: 0, // use default DB
- })
-
- Register("redis", db_redis{
- database: database,
- })
-}
-
-func (db db_redis) GetChannel(guildid string) string {
- data, err := db.database.Get(guildid).Result()
-
- if err != nil {
- return ""
- }
-
- return data
-}
-
-func (db db_redis) SetChannel(guildid, channel string) {
- db.database.Set(guildid, []byte(channel), 0)
-}
-
-func (db db_redis) Close() {
- db.database.Close()
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/discord/command_remove_channel.go Tue Apr 09 13:14:34 2019 -0700
@@ -0,0 +1,29 @@
+package discord
+
+import (
+ "fmt"
+
+ "github.com/bwmarrin/discordgo"
+)
+
+type commandRemoveChannel struct{}
+
+func (cmd *commandRemoveChannel) Help() string {
+ return "removes the channel from the bot to send messages to"
+}
+
+func (cmd *commandRemoveChannel) Run(args []string, c *DiscordClient, m *discordgo.MessageCreate) (interface{}, error) {
+ guild := c.db.GetGuild(m.GuildID)
+ if guild.ID == "" {
+ return "This guild is not registered please use `set-channel`", nil
+ } else {
+ if !guild.HasChannel(m.ChannelID) {
+ return fmt.Sprintf("<#%s> is not a registered channel, please use `set-channel`", m.ChannelID), nil
+ }
+
+ guild.RemoveChannel(m.ChannelID)
+ c.db.UpdateGuild(guild)
+ }
+
+ return fmt.Sprintf("<#%s> has been removed from the channels list", m.ChannelID), nil
+}
--- a/discord/command_set_channel.go Mon Apr 08 23:38:13 2019 -0500
+++ b/discord/command_set_channel.go Tue Apr 09 13:14:34 2019 -0700
@@ -13,7 +13,17 @@
}
func (cmd *commandSetChannel) Run(args []string, c *DiscordClient, m *discordgo.MessageCreate) (interface{}, error) {
- c.db.SetChannel(m.GuildID, m.ChannelID)
+ guild := c.db.GetGuild(m.GuildID)
+ if guild.ID == "" {
+ guild.ID = m.GuildID
+ guild.AddChannel(m.ChannelID)
+ c.db.AddGuild(guild)
- return fmt.Sprintf("Set <#%s> as the announcement channel", m.ChannelID), nil
+ return fmt.Sprintf("Set <#%s> as the announcement channel", m.ChannelID), nil
+ } else {
+ // TODO: actually make this do something other then update
+ c.db.UpdateGuild(guild)
+ }
+
+ return fmt.Sprintf("<#%s> is already set as a registered channel", m.ChannelID), nil
}
--- a/discord/commands.go Mon Apr 08 23:38:13 2019 -0500
+++ b/discord/commands.go Tue Apr 09 13:14:34 2019 -0700
@@ -20,6 +20,7 @@
func init() {
commands.Set("help", &commandHelp{})
commands.Set("set-channel", &commandSetChannel{})
+ commands.Set("remove-channel", &commandRemoveChannel{})
commands.Set("status", &commandStatus{})
commands.Set("uptime", &commandUptime{})
}
--- a/discord/discord.go Mon Apr 08 23:38:13 2019 -0500
+++ b/discord/discord.go Tue Apr 09 13:14:34 2019 -0700
@@ -54,12 +54,20 @@
return c.client.Close()
}
-func (c *DiscordClient) send(guild, message string) error {
- channelID := c.db.GetChannel(guild)
- if channelID == "" {
+func (c *DiscordClient) send(guildID, message string) error {
+ // channelID := c.db.GetChannel(guild)
+ guild := c.db.GetGuild(guildID)
+
+ if guild.ID == "" {
+ return errors.New("Guild does not exist")
+ }
+
+ if len(guild.Channels) == 0 || guild.Channels[0].ID == "" {
return errors.New("No channel set")
}
+ channelID := guild.Channels[0].ID
+
return c.sendChannel(channelID, message)
}
--- a/discord/presence.go Mon Apr 08 23:38:13 2019 -0500
+++ b/discord/presence.go Tue Apr 09 13:14:34 2019 -0700
@@ -31,13 +31,18 @@
}
}
-func (c *DiscordClient) sendPresence(guild string, presence presence.Presence) error {
- channelID := c.db.GetChannel(guild)
+func (c *DiscordClient) sendPresence(guildID string, presence presence.Presence) error {
+ guild := c.db.GetGuild(guildID)
+ if guild.ID == "" {
+ return errors.New("Guild does not exist")
+ }
- if channelID == "" {
+ if len(guild.Channels) == 0 || guild.Channels[0].ID == "" {
return errors.New("No channel set")
}
+ channelID := guild.Channels[0].ID
+
return c.sendPresenceChannel(channelID, presence)
}