diff --git a/.gitignore b/.gitignore index 1f6539a..9d37a07 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ Milen bot.db config.toml +persistent.toml diff --git a/commands/commands.go b/commands/commands.go index e7e04e3..29b7e88 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -19,6 +19,8 @@ type command struct { var commands = []command{ command{"autorole", " ", handleAutorole, 3, true}, + command{"ignorechannel", "", handleIgnoreChannel, 0, true}, + command{"unignorechannel", "", handleUnignoreChannel, 0, true}, command{"leaderboard", "", handleLeaderboard, 0, true}, command{"level", "", handleLevel, 0, false}, command{"recalclevel", "", handleRecalculateLevel, 0, false}, @@ -55,8 +57,9 @@ func Event(dg *discordgo.Session, m *discordgo.MessageCreate) { util.SendFailEmbed(dg, m.ChannelID, ":(", "Did you just call me out for no reason? Please specify what you want me to do.") return } + commandName := strings.ToLower(split[1]) for _, v := range commands { - if split[1] != v.Name { + if commandName != v.Name { continue } if (v.ExactArgument && len(split) != v.MinimumArgument+2) || (!v.ExactArgument && len(split) < v.MinimumArgument+2) { diff --git a/commands/ignoreChannel.go b/commands/ignoreChannel.go new file mode 100644 index 0000000..37cab7c --- /dev/null +++ b/commands/ignoreChannel.go @@ -0,0 +1,24 @@ +package commands + +import ( + "github.com/bwmarrin/discordgo" + + "gitea.teamortix.com/chanbakjsd/Milen/persistent" + "gitea.teamortix.com/chanbakjsd/Milen/util" +) + +func handleIgnoreChannel(dg *discordgo.Session, m *discordgo.MessageCreate, arguments []string) { + if !util.HasAdmin(dg, m.Author.ID, m.ChannelID) { + util.SendErrorEmbed(dg, m.ChannelID, util.ErrRequireAdmin) + return + } + persistent.IgnoredChannels[m.ChannelID] = true +} + +func handleUnignoreChannel(dg *discordgo.Session, m *discordgo.MessageCreate, arguments []string) { + if !util.HasAdmin(dg, m.Author.ID, m.ChannelID) { + util.SendErrorEmbed(dg, m.ChannelID, util.ErrRequireAdmin) + return + } + delete(persistent.IgnoredChannels, m.ChannelID) +} diff --git a/level/listener.go b/level/listener.go index 789e3ea..355dd2e 100644 --- a/level/listener.go +++ b/level/listener.go @@ -6,13 +6,14 @@ import ( "github.com/bwmarrin/discordgo" "gitea.teamortix.com/chanbakjsd/Milen/db" + "gitea.teamortix.com/chanbakjsd/Milen/persistent" "gitea.teamortix.com/chanbakjsd/Milen/util" ) var ShouldListen = true func Event(dg *discordgo.Session, m *discordgo.MessageCreate) { - if !ShouldListen || m.GuildID == "" { + if !ShouldListen || m.GuildID == "" || persistent.IgnoredChannels[m.ChannelID] { return } diff --git a/level/recalculateEngine.go b/level/recalculateEngine.go index d53ba45..af84048 100644 --- a/level/recalculateEngine.go +++ b/level/recalculateEngine.go @@ -8,6 +8,7 @@ import ( "github.com/bwmarrin/discordgo" "gitea.teamortix.com/chanbakjsd/Milen/db" + "gitea.teamortix.com/chanbakjsd/Milen/persistent" "gitea.teamortix.com/chanbakjsd/Milen/util" ) @@ -25,6 +26,9 @@ func RecalculateEverything(dg *discordgo.Session, guildID string) { } listOfMessages := make(map[string][]*discordgo.Message) for _, v := range guild.Channels { + if persistent.IgnoredChannels[v.ID] { + continue + } beforeID := "" for { messages, err := dg.ChannelMessages(v.ID, 100, beforeID, "", "") diff --git a/main.go b/main.go index b639498..054555a 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "gitea.teamortix.com/chanbakjsd/Milen/commands" "gitea.teamortix.com/chanbakjsd/Milen/db" "gitea.teamortix.com/chanbakjsd/Milen/level" + "gitea.teamortix.com/chanbakjsd/Milen/persistent" "gitea.teamortix.com/chanbakjsd/Milen/reactrole" "gitea.teamortix.com/chanbakjsd/Milen/util" ) @@ -34,6 +35,7 @@ func main() { dg.AddHandler(reactrole.EventRemove) go level.QueueRoles(dg) + go persistent.SaveLoop(dg) err = dg.Open() if err != nil { @@ -44,6 +46,7 @@ func main() { sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc + persistent.Save(dg) err = dg.Close() if err != nil { fmt.Println("error closing connection,", err) diff --git a/persistent/persistent.go b/persistent/persistent.go new file mode 100644 index 0000000..3a145b5 --- /dev/null +++ b/persistent/persistent.go @@ -0,0 +1,7 @@ +package persistent + +type persistent struct { + IgnoredChannels map[string]bool `toml:"ignored_channels"` +} + +var IgnoredChannels = make(map[string]bool) diff --git a/persistent/save.go b/persistent/save.go new file mode 100644 index 0000000..2ed4331 --- /dev/null +++ b/persistent/save.go @@ -0,0 +1,43 @@ +package persistent + +import ( + "io/ioutil" + "os" + "time" + + "github.com/BurntSushi/toml" + "github.com/bwmarrin/discordgo" + + "gitea.teamortix.com/chanbakjsd/Milen/util" +) + +func init() { + data, err := ioutil.ReadFile("persistent.toml") + if err != nil { + return + } + var loaded persistent + toml.Unmarshal(data, &loaded) + if loaded.IgnoredChannels != nil { + IgnoredChannels = loaded.IgnoredChannels + } +} + +func SaveLoop(dg *discordgo.Session) { + for { + time.Sleep(time.Minute) + Save(dg) + } +} + +func Save(dg *discordgo.Session) { + file, err := os.Create("persistent.toml") + if err != nil { + util.ReportError(dg, err) + return + } + defer file.Close() + toml.NewEncoder(file).Encode(&persistent{ + IgnoredChannels: IgnoredChannels, + }) +}