From af81e9ddc83d7f0397119c0eee180d102903b602 Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Wed, 5 May 2021 22:59:55 -0400 Subject: [PATCH] Commands for everyone --- commands.go | 43 ++++++++++++++++++++++++------------------- discordMessage.go | 20 ++++++++++++++------ types.go | 9 +++++---- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/commands.go b/commands.go index d336a0f..7533c63 100644 --- a/commands.go +++ b/commands.go @@ -12,33 +12,37 @@ import ( func setupCommands() { reboot := Command{ - Name: "Reboot", - Help: "Reboot me, requires token from logs.", - Keywords: []string{"reboot", "re", "restart"}, - Exec: Reboot, + Name: "Reboot", + RequiresAdmin: true, + Help: "Reboot me, requires token from logs.", + Keywords: []string{"reboot", "re", "restart"}, + Exec: Reboot, } commands = append(commands, reboot) bumpset := Command{ - Name: "BumpSet", - Help: "Set the bump timer (requires time in minutes until next bump).", - Keywords: []string{"bs", "bumpset", "bumps"}, - Exec: BumpSet, + Name: "BumpSet", + RequiresAdmin: true, + Help: "Set the bump timer (requires time in minutes until next bump).", + Keywords: []string{"bs", "bumpset", "bumps"}, + Exec: BumpSet, } commands = append(commands, bumpset) retrieveVerification := Command{ - Name: "Retrieve Verification", - Help: "Retrieve verification either by discord ID or by nickname", - Keywords: []string{"veri", "verification", "retrieve"}, - Exec: RetrieveVerification, + Name: "Retrieve Verification", + RequiresAdmin: true, + Help: "Retrieve verification either by discord ID or by nickname", + Keywords: []string{"veri", "verification", "retrieve"}, + Exec: RetrieveVerification, } commands = append(commands, retrieveVerification) addQuote := Command{ - Name: "Add Quote", - Keywords: []string{"quote", "addq", "q"}, - Exec: AddQuote, + Name: "Add Quote", + RequiresAdmin: true, + Keywords: []string{"quote", "addq", "q"}, + Exec: AddQuote, } commands = append(commands, addQuote) @@ -50,9 +54,10 @@ func setupCommands() { commands = append(commands, snap) status := Command{ - Name: "Status", - Keywords: []string{"st", "status", "stats"}, - Exec: Status, + Name: "Status", + RequiresAdmin: true, + Keywords: []string{"st", "status", "stats"}, + Exec: Status, } commands = append(commands, status) } @@ -163,7 +168,7 @@ func Status(b BotCommand) bool { status += fmt.Sprintf("%+v for another %+v\n", probationUser.Username, time.Until(join.Add(2*time.Hour))) } status += "```" - }else { + } else { status += "There are no users on probation." } b.Session.ChannelMessageSend(config.AdminChannel, status) diff --git a/discordMessage.go b/discordMessage.go index d3ace6a..93e2052 100644 --- a/discordMessage.go +++ b/discordMessage.go @@ -52,15 +52,23 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if time.Since(config.BumpTime) > 2*time.Hour { s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%+v please say \"!d bump\" without the quotes to bump our server :)", m.Author.Mention())) } - if m.ChannelID == config.AdminChannel { - if strings.HasPrefix(m.Content, s.State.User.Mention()) { - for _, cmd := range commands { - for _, keyword := range cmd.Keywords { - if strings.Contains(m.Content, keyword) { - b.Command = keyword + if strings.HasPrefix(m.Content, s.State.User.Mention()) { + for _, cmd := range commands { + for _, keyword := range cmd.Keywords { + if strings.Contains(m.Content, keyword) { + b.Command = keyword + if !cmd.RequiresAdmin { if !cmd.Exec(b) { s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("There was an error running %+v\n%+v", cmd.Name, cmd.Help)) } + } else { + for role := range m.Member.Roles { + if fmt.Sprintf("%+v", role) == config.AdminRole { + if !cmd.Exec(b) { + s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("There was an error running %+v\n%+v", cmd.Name, cmd.Help)) + } + } + } } } } diff --git a/types.go b/types.go index 0a4205b..e053b68 100644 --- a/types.go +++ b/types.go @@ -17,10 +17,11 @@ type BotCommand struct { // Command is the type to store commands type Command struct { - Name string - Help string - Keywords []string - Exec func(BotCommand) bool + Name string + RequiresAdmin bool + Help string + Keywords []string + Exec func(BotCommand) bool } // Config struct used for bot