feat: Add ignored channels

master
Luther Wen Xu 2020-06-06 17:25:21 +07:00
parent e28fffa193
commit 75b91331a7
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
8 changed files with 88 additions and 2 deletions

1
.gitignore vendored

@ -1,3 +1,4 @@
Milen
bot.db
config.toml
persistent.toml

@ -19,6 +19,8 @@ type command struct {
var commands = []command{
command{"autorole", "<message link> <emoji> <role to assign>", 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) {

@ -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)
}

@ -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
}

@ -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, "", "")

@ -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)

@ -0,0 +1,7 @@
package persistent
type persistent struct {
IgnoredChannels map[string]bool `toml:"ignored_channels"`
}
var IgnoredChannels = make(map[string]bool)

@ -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,
})
}