From eb20b6aa9aa68599cfd6074536fcf3ac553d5126 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Wed, 9 Oct 2019 20:44:43 +0800 Subject: [PATCH] go: Initial Commit Added a few commands that can be used by an admin to send or edit messages as the bot. --- GoBot/.gitignore | 1 + GoBot/admin.go | 40 ++++++++++++++++++++++++++++++++++++++++ GoBot/go.mod | 5 +++++ GoBot/go.sum | 6 ++++++ GoBot/main.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 GoBot/.gitignore create mode 100644 GoBot/admin.go create mode 100644 GoBot/go.mod create mode 100644 GoBot/go.sum create mode 100644 GoBot/main.go diff --git a/GoBot/.gitignore b/GoBot/.gitignore new file mode 100644 index 0000000..c8266e8 --- /dev/null +++ b/GoBot/.gitignore @@ -0,0 +1 @@ +token.txt diff --git a/GoBot/admin.go b/GoBot/admin.go new file mode 100644 index 0000000..e223b95 --- /dev/null +++ b/GoBot/admin.go @@ -0,0 +1,40 @@ +package main + +import ( + "strings" + + "github.com/bwmarrin/discordgo" +) + +//adminID is my Discord user ID (chanbakjsd#7968). +const adminID = "218983355746746369" + +func sendAs(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { + if m.Author.ID != adminID { + return + } + if len(command) < 3 { + return + } + message := strings.SplitN(m.Content, " ", 3) + msg, err := s.ChannelMessageSend(message[1], message[2]) + if err != nil { + s.ChannelMessageSend(m.ChannelID, err.Error()) + } + s.ChannelMessageSend(m.ChannelID, "The message has been sent. Discord ID: "+msg.ID) +} + +func editAs(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { + if m.Author.ID != adminID { + return + } + if len(command) < 4 { + return + } + message := strings.SplitN(m.Content, " ", 4) + _, err := s.ChannelMessageEdit(message[1], message[2], message[3]) + if err != nil { + s.ChannelMessageSend(m.ChannelID, err.Error()) + } + s.ChannelMessageSend(m.ChannelID, "The message has been edited.") +} diff --git a/GoBot/go.mod b/GoBot/go.mod new file mode 100644 index 0000000..9a73011 --- /dev/null +++ b/GoBot/go.mod @@ -0,0 +1,5 @@ +module TerraOceanBot + +go 1.13 + +require github.com/bwmarrin/discordgo v0.19.0 diff --git a/GoBot/go.sum b/GoBot/go.sum new file mode 100644 index 0000000..ef56b25 --- /dev/null +++ b/GoBot/go.sum @@ -0,0 +1,6 @@ +github.com/bwmarrin/discordgo v0.19.0 h1:kMED/DB0NR1QhRcalb85w0Cu3Ep2OrGAqZH1R5awQiY= +github.com/bwmarrin/discordgo v0.19.0/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q= +github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/GoBot/main.go b/GoBot/main.go new file mode 100644 index 0000000..a1e4626 --- /dev/null +++ b/GoBot/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "os/signal" + "strings" + "syscall" + + "github.com/bwmarrin/discordgo" +) + +func main() { + token, _ := ioutil.ReadFile("token.txt") + dg, err := discordgo.New("Bot " + strings.TrimSpace(string(token))) + if err != nil { + panic(err) + } + dg.AddHandler(messageCreate) + if err := dg.Open(); err != nil { + panic(err) + } + + fmt.Println("Bot is now running. Press CTRL-C to exit.") + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc + + dg.Close() +} + +func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + if m.Author.ID == s.State.User.ID { + return + } + + command := strings.Split(m.Content, " ") + switch command[0] { + case "!sendas": + sendAs(s, m, command) + case "!editas": + editAs(s, m, command) + } +}