refactor: moved role distribution to new logic

This commit is contained in:
kageru 2018-06-06 17:28:43 +02:00
parent 39ce67d4a6
commit a30c1b5d65
Signed by untrusted user: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 58 additions and 139 deletions

View File

@ -2,9 +2,9 @@ package main
import (
"fmt"
"time"
"github.com/bwmarrin/discordgo"
"strings"
"time"
"log"
"regexp"
)
@ -109,8 +109,10 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
Sets command cooldowns if necessary and also clears them again.
*/
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command Command, commandIndex int) {
if (!command.DMOnly || (getChannel(session.State, message.ChannelID).Type == discordgo.ChannelTypeDM)) &&
(!command.AdminOnly || (message.Author.ID == config.AdminID)) && !command.IsOnCooldown {
if (message.Author.ID == config.AdminID) || // no restrictions for admins
(!command.AdminOnly && !command.IsOnCooldown &&
(!command.DMOnly || (getChannel(session.State, message.ChannelID).Type == discordgo.ChannelTypeDM))) {
log.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author))
fmt.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author))
if command.Cooldown > 0 {
@ -167,3 +169,26 @@ func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, m.Content)
}
func giveAgeRole(s *discordgo.Session, m *discordgo.MessageCreate) {
Member, _ := s.GuildMember(config.ServerID, m.Author.ID)
dm, _ := s.UserChannelCreate(Member.User.ID)
for command, role := range config.RoleCommands {
if m.Content == command {
// Found the command that was triggered
// Now check if the user already has one of the roles
for _, newRole := range config.RoleCommands {
for _, curRole := range Member.Roles {
// If the user already has one of the available roles, tell him and exit
if newRole == curRole {
s.ChannelMessageSend(dm.ID, "Baka, du kannst nur eine der Rollen haben.")
log.Printf("Denied Role %s to %s. User already has %s", roleName(s.State, curRole), userToString(m.Author), roleName(s.State, curRole))
return
}
}
}
log.Printf("Giving Role %s to %s", roleName(s.State, role), userToString(m.Author))
s.ChannelMessageSend(dm.ID, "Haaai, Ryoukai desu~")
s.GuildMemberRoleAdd(config.ServerID, m.Author.ID, role)
}
}
}

116
events.go
View File

@ -3,9 +3,7 @@ package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"strings"
"log"
"regexp"
)
func onJoin(s *discordgo.Session, member *discordgo.GuildMemberAdd) {
@ -37,15 +35,6 @@ func onDM(s *discordgo.Session, m *discordgo.MessageCreate) {
fmt.Sprintf("Received DM from %s with content: “%s”", userToString(m.Author), m.Content)
Member, _ := s.GuildMember(config.ServerID, m.Author.ID)
dm, _ := s.UserChannelCreate(Member.User.ID)
if m.Content == "!welcome" {
s.ChannelMessageSendEmbed(dm.ID, getWelcomeEmbed())
return
}
if strings.HasPrefix(m.Content, "!complain") {
redirectComplaint(s, m)
s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage)
return
}
for comm, role := range config.RoleCommands {
if m.Content == comm {
for _, irole := range config.RoleCommands {
@ -64,108 +53,3 @@ func onDM(s *discordgo.Session, m *discordgo.MessageCreate) {
}
}
func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
log.Printf("<Self> %s", m.Content)
return
}
if getChannel(s.State, m.ChannelID).Type == discordgo.ChannelTypeDM {
onDM(s, m)
}
if m.Author.ID == config.AdminID {
//replyGodmode(s, m)
} else if m.ChannelID == config.WelcomeChannel {
s.ChannelMessageDelete(m.ChannelID, m.ID)
if m.Content == "!accept" {
unlockUser(s, m.Author.ID)
}
return
}
// In case this doesn’t work with your font: the last character is a winking emoji.
winks, _ := regexp.MatchString("([()|DoO];|;[()|DoOpP]|:wink:|😉)", m.Content)
if winks {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("<@%s> Oboe!", m.Author.ID))
s.ChannelMessageDelete(m.ChannelID, m.ID)
channel := getChannel(s.State, m.ChannelID)
log.Printf("Deleted message by %s in %s. Content: “%s”", userToString(m.Author), channelToString(channel), m.Content)
return
}
// As per our majesty’s command:
if m.Content == "\\o" {
s.ChannelMessageSend(m.ChannelID, "o/")
log.Printf("o/ at %s", userToString(m.Author))
} else if m.Content == "o/" {
s.ChannelMessageSend(m.ChannelID, "\\o")
log.Printf("\\o at %s", userToString(m.Author))
} else if m.Content == "ayy" {
s.ChannelMessageSend(m.ChannelID, "lmao")
log.Printf("ayy lmao at %s", userToString(m.Author))
}
}
// Admin stuff down here. This is very server-specific
func adminshit (s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Content == fmt.Sprintf("<@%s> <3", s.State.User.ID) {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("<@%s> <3", config.AdminID))
}
/*
if m.Content == "print_rules()" {
channel := getChannel(s.State, m.ChannelID)
log.Printf("print_rules() triggered by %s in %s.", userToString(m.Author), channelToString(channel))
embedColor := 0xffb90f // kageru gold
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: embedColor,
Description: config.WelcomeEmbed.Message,
Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField{
Name: config.WelcomeEmbed.QuestionsTitle,
Value: config.WelcomeEmbed.QuestionsText,
Inline: true,
},
&discordgo.MessageEmbedField{
Name: config.WelcomeEmbed.BugsTitle,
Value: fmt.Sprintf(config.WelcomeEmbed.BugsText, config.AdminID),
Inline: true,
},
},
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: config.WelcomeEmbed.Image,
},
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
embed = &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: embedColor,
Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField{
Name: config.WelcomeEmbed.RulesTitle,
Value: config.WelcomeEmbed.RulesText,
Inline: true,
},
},
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
embed = &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: embedColor,
Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField{
Name: config.WelcomeEmbed.RulesTitle,
Value: config.WelcomeEmbed.RulesText2,
Inline: true,
},
},
}
s.ChannelMessageSendEmbed(m.ChannelID, embed)
}
*/
}

50
main.go
View File

@ -34,26 +34,7 @@ func main() {
defer f.Close()
log.SetOutput(f)
// Moderation
registerCommand(Command{Trigger: "^[^`]*([()|DoO];|;[()|DoOpP]|:wink:|😉)[^`]*$", Output: "<@%s> Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex})
// Misc commands
registerCommand(Command{Trigger: "o/", Output: "\\o", Type: CommandTypeFullMatch, Cooldown: 5})
registerCommand(Command{Trigger: "\\o", Output: "o/", Type: CommandTypeFullMatch})
registerCommand(Command{Trigger: "\\o/", Output: "/o\\", Type: CommandTypeFullMatch})
registerCommand(Command{Trigger: "<:selphyDango:441001954542616576>", Output: ":notes: Dango, Dango, Dango, Dango, Dango Daikazoku :notes:", Type: CommandTypeFullMatch})
registerCommand(Command{Trigger: "praise the sun", Output: "If only I could be so grossly incandescent \\\\[T]/", Type: CommandTypeContains, IgnoreCase: true})
registerCommand(Command{Trigger: "echo", Type: CommandTypePrefix, Function: echoMessage})
registerCommand(Command{Trigger: "!welcome", OutputEmbed: getWelcomeEmbed(), Type: CommandTypeFullMatch, DMOnly: true})
registerCommand(Command{Trigger: "<@%s> <3", Output: "<@%s> <3", Type: CommandTypeFullMatch, AdminOnly: true, OutputIsReply: true, RequiresMention: true})
registerCommand(Command{Trigger: "!complain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint})
addCommands()
fmt.Println("bot running. selphyWoo")
sc := make(chan os.Signal, 1)
@ -63,6 +44,35 @@ func main() {
dg.Close()
}
// I’ll just put all of the commands here for now.
func addCommands() {
// Moderation
registerCommand(Command{Trigger: "^[^`]*([()|DoO];|;[()|DoOpP]|:wink:|😉)[^`]*$", Output: "<@%s> Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex})
registerCommand(Command{Trigger: "!complain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint})
registerCommand(Command{Trigger: "!beschwerde", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint})
for comm, _ := range config.RoleCommands {
registerCommand(Command{Trigger: comm, Type: CommandTypeFullMatch, DMOnly: true, Function: giveAgeRole})
}
// Misc commands
registerCommand(Command{Trigger: "o/", Output: "\\o", Type: CommandTypeFullMatch, Cooldown: 10})
registerCommand(Command{Trigger: "\\o", Output: "o/", Type: CommandTypeFullMatch, Cooldown: 10})
registerCommand(Command{Trigger: "\\o/", Output: "/o\\", Type: CommandTypeFullMatch, Cooldown: 10})
registerCommand(Command{Trigger: "<:selphyDango:441001954542616576>", Output: ":notes: Dango, Dango, Dango, Dango, Dango Daikazoku :notes:", Type: CommandTypeFullMatch, Cooldown: 1200})
registerCommand(Command{Trigger: "praise the sun", Output: "If only I could be so grossly incandescent \\\\[T]/", Type: CommandTypeContains, IgnoreCase: true, Cooldown: 30})
// Information
registerCommand(Command{Trigger: "!welcome", OutputEmbed: getWelcomeEmbed(), Type: CommandTypeFullMatch, DMOnly: true})
// Admin
registerCommand(Command{Trigger: "<@%s> <3", Output: "<@%s> <3", Type: CommandTypeFullMatch, AdminOnly: true, OutputIsReply: true, RequiresMention: true})
// Debug
registerCommand(Command{Trigger: "echo", Type: CommandTypePrefix, Function: echoMessage})
}
/*func undelete(s *discordgo.Session, m *discordgo.MessageDelete) {
channel, _ := s.State.Channel(m.ChannelID)
message, _ := s.State.Message(m.ChannelID, m.ID)