From 941ae498dd2b65d967a7363ebeab04c3ae42036c Mon Sep 17 00:00:00 2001 From: Sean Hinchee Date: Wed, 16 Jan 2019 18:20:29 -0600 Subject: [PATCH] add s/word/words/g style editing of last message sent --- README.md | 10 +++++++--- commands.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c7c35f1aaf55c7281dfc3a4cf5dfa69f4aed8694..e5008e8af964dc0c62ff29d95721559946db3a77 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,13 @@ Commands available in chat: | :p | Pulls up the private channel menu | | :n name | Change nickname to `name` | +You can regex the last message sent using a format such as: + + s/forsynth/forsyth/g + ## Config -A normal $home/lib/disco.ndb looks something like: +A basic $home/lib/disco.ndb looks something like: ``` auth=pass @@ -53,7 +57,7 @@ Note that the auth= tuple accepts for authentication using a factotum key and will ignore the password= tuple. -The factotum key should resemble something to the effect of: +If used, the factotum key should resemble something to the effect of: proto=pass server=discordapp.com service=discord user=youremail@domain.com !password=hunter2 @@ -69,7 +73,7 @@ A: If you want to use `go get` on 9front to install disco and its dependencies ( Q: What if I can't login because of a captcha error? -A: You'll need to sign in to Discord via the web app (thus solving a captcha) using a browser with html5/js. I recommend an http proxy such as [this](https://bitbucket.org/henesy/http-proxy). +A: You'll need to sign in to Discord via the web app (thus solving a captcha) using a browser with html5/js. I recommend an http proxy such as [this](https://bitbucket.org/henesy/http-proxy) in conjunction with a system with such a browser.. Q: What if I get an error about signing in from a new location? diff --git a/commands.go b/commands.go index 23f938a3ab340b2602ea66c326cce0264aa59be6..037fd3683f8537bc6d2f0ee61244be1160867f5b 100644 --- a/commands.go +++ b/commands.go @@ -1,6 +1,7 @@ package main import ( + "regexp" "strconv" "strings" "github.com/bwmarrin/discordgo" @@ -12,6 +13,41 @@ func ParseForCommands(line string) string { return line } switch line[:2] { + case "s/": + r := regexp.MustCompile(`s\/((\w|\s)+)\/((\w|\s|)+)\/`) + 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, "") + if len(line) == 5 { + 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: ")