From bc7656ed7ca7d57a6c792306beb1592e346937eb Mon Sep 17 00:00:00 2001 From: Sean Hinchee Date: Sun, 11 Feb 2018 22:27:45 +0000 Subject: [PATCH] added unsubscribe; added regular dump tied to Listener() --- mux.go | 2 ++ x/mux/commits.go | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/mux.go b/mux.go index 8ae5959992c9e3b26d71deae998cc460b9240dc4..daa66373596e8ec9f326128c87e77ed61e44a0eb 100644 --- a/mux.go +++ b/mux.go @@ -30,6 +30,8 @@ func init() { Router.Route("subscribe", "Subscribe current channel to a given feed id.", Router.Subscribe) + Router.Route("unsubscribe", "Unsubscribe current channel to a given feed id.", Router.Unsubscribe) + Router.Route("man", "Display a given Plan 9 manual page (From 9front for now).", Router.Man) Router.Route("about", "General information about the bot.", Router.About) diff --git a/x/mux/commits.go b/x/mux/commits.go index f2c5565f4872d3d42046d1f9d4e6bc90c657aa88..88a81462658c7c1c035758af475ef52a9f5482ae 100644 --- a/x/mux/commits.go +++ b/x/mux/commits.go @@ -42,9 +42,11 @@ func Listener() { break } } - time.Sleep(1 * time.Minute) + time.Sleep(2 * time.Minute) } - time.Sleep(1 * time.Minute) + time.Sleep(10 * time.Minute) + // Dump config to file regularly + Config.Write() } } @@ -190,3 +192,34 @@ func (m *Mux) Subscribe(ds *discordgo.Session, dm *discordgo.Message, ctx *Conte return } + +// a = a[:i+copy(a[i:], a[i+1:])] +// Unsubscribe current channel to notifications from a given feed id +func (m *Mux) Unsubscribe(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { + resp := "\n" + + id, _ := strconv.Atoi(ctx.Fields[len(ctx.Fields) -1]) + if id >= 0 && id < len(Config.Feeds) { + // Check if subscribed + removed := false + for i, v := range Config.Subs { + if v.ChanID == dm.ChannelID && v.SubID == id { + removed = true + Config.Subs = Config.Subs[:i+copy(Config.Subs[i:], Config.Subs[i+1:])] + } + } + + if(!removed) { + resp += "Denied unsubscription. Not subscribed in this channel." + } else { + resp += "Unsubscribed." + } + } else { + resp += "Denied unsubscription. Invalid stream id, see: list command" + } + + resp += "\n" + ds.ChannelMessageSend(dm.ChannelID, resp) + + return +}