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 @@
"github.com/prologic/bitcask"
+ log "github.com/sirupsen/logrus" @@ -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 + guild := toGuildObject(data) +func (db db_bitcask) AddGuild(guild *Guild) { + err := db.database.Put(guild.ID, toJsonObject(*guild))
+ log.Errorf("Bitcask AddGuild error: %s", err)
-func (db db_bitcask) SetChannel(guildid, channel string) {
- db.database.Put(guildid, []byte(channel))
+func (db db_bitcask) UpdateGuild(guild *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 @@
+ log "github.com/sirupsen/logrus" type Database interface {
- SetChannel(guildid, channel string)
- GetChannel(guildid string) string
+ GetGuild(guildid string) *Guild + UpdateGuild(guild *Guild) @@ -30,3 +37,25 @@
+func toGuildObject(data []byte) Guild { + err := json.Unmarshal(data, &item) + log.Errorf("toGuildObject failed: %#v", err) +func toJsonObject(guild Guild) []byte { + data, err := json.Marshal(guild) + log.Errorf("toJsonObject failed: %#v", err) --- /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 @@
+// TODO: enable more features like config +// TODO: add methods to channel that let us use this system more +func (guild *Guild) HasChannel(channel string) bool { + for _, g := range guild.Channels { +func (guild *Guild) AddChannel(channel string) { + if guild.HasChannel(channel) { + guild.Channels = append(guild.Channels, Channel{channel}) +func (guild *Guild) RemoveChannel(channel string) { + newChannels := make([]Channel, 0) + for _, g := range guild.Channels { + 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 @@
- "github.com/go-redis/redis"
- database := redis.NewClient(&redis.Options{
- Addr: "localhost:6379",
- Password: "", // no password set
- DB: 0, // use default DB
- Register("redis", db_redis{
-func (db db_redis) GetChannel(guildid string) string {
- data, err := db.database.Get(guildid).Result()
-func (db db_redis) SetChannel(guildid, channel string) {
- db.database.Set(guildid, []byte(channel), 0)
-func (db db_redis) 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 @@
+ "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) + return "This guild is not registered please use `set-channel`", nil + 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) + guild.AddChannel(m.ChannelID) - return fmt.Sprintf("Set <#%s> as the announcement channel", m.ChannelID), nil
+ return fmt.Sprintf("Set <#%s> as the announcement channel", m.ChannelID), nil + // 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 @@
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 @@
-func (c *DiscordClient) send(guild, message string) error {
- channelID := c.db.GetChannel(guild)
+func (c *DiscordClient) send(guildID, message string) error { + // channelID := c.db.GetChannel(guild) + guild := c.db.GetGuild(guildID) + 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) + 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.sendPresenceChannel(channelID, presence)