go: Initial Commit

Added a few commands that can be used by an admin to send or edit messages as the bot.
pull/6/head
Luther Wen Xu 2019-10-09 20:44:43 +07:00
parent d2b6a6009e
commit eb20b6aa9a
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
5 changed files with 97 additions and 0 deletions

1
GoBot/.gitignore vendored

@ -0,0 +1 @@
token.txt

@ -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.")
}

@ -0,0 +1,5 @@
module TerraOceanBot
go 1.13
require github.com/bwmarrin/discordgo v0.19.0

@ -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=

@ -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)
}
}