diff -r a22f5c0ac3a9 commands.go --- a/commands.go Wed Dec 26 16:28:08 2018 -0600 +++ b/commands.go Tue Jan 15 22:57:52 2019 -0700 @@ -1,6 +1,7 @@ package main import ( + "regexp" "strconv" "strings" "github.com/bwmarrin/discordgo" @@ -12,6 +13,38 @@ return line } switch line[:2] { + case "s/": + r := regexp.MustCompile(`s\/(\w+)\/(\w+)\/`) + n := r.FindStringSubmatchIndex(line) + if len(n) < 1 { + return "" + } + msg := State.Messages + for i := len(msg)-1; i >= 0; i-- { + if (msg[i].ChannelID != State.Channel.ID && msg[i].GuildID != State.Guild.ID) { + break + } + if msg[i].Author.ID != Session.User.ID { + break + } + if strings.Contains(msg[i].Content, line[n[2]:n[3]]) { + cmd, err := regexp.Compile("(" + line[n[2]:n[3]] + ")") + if err != nil { + Msg(ErrorMsg, "%s - invalid regex\n", line) + } + rep := cmd.ReplaceAllString(msg[i].Content, line[n[4]:n[5]]) + data := discordgo.NewMessageEdit(msg[i].ChannelID, msg[i].ID) + data = data.SetContent(rep) + _, err = State.Session.DiscordGo.ChannelMessageEditComplex(data) + if err != nil { + Msg(ErrorMsg, "%s\n", err) + return "" + } + Msg(TextMsg, "%s -> %s\n", msg[i].Content, rep) + return "" + } + } + return line case ":?": // Show help menu Msg(TextMsg, "Commands: ") @@ -80,7 +113,7 @@ Msg(InfoMsg, "Printing last %d messages!\n", Amount) State.RetrieveMessages(Amount) PrintMessages(Amount) - return line + return "" case ":u": session := State.Session user := session.User @@ -88,8 +121,10 @@ _, err := State.Session.DiscordGo.UserUpdate(user.Email, session.Password, newName, user.Avatar, "") if err != nil { Msg(ErrorMsg, "[:u] Argument Error: %s\n", err) + return "" } - return line + Msg(TextMsg, "name -> %s\n", newName) + return "" } return line }