grim/wasdead

First push
draft
2019-03-19, Ruin
95d17a704244
Parents
Children 21336f609455
First push
  • +31 -0
    .hgignore
  • +88 -0
    main.go
  • --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/.hgignore Tue Mar 19 20:43:14 2019 -0700
    @@ -0,0 +1,31 @@
    +syntax: glob
    +# This line is a comment, and will be skipped.
    +# Empty lines are skipped too.
    +# Backup files left behind by the Emacs editor.
    +*~
    +
    +# Lock files used by the Emacs editor.
    +# Notice that the "#" character is quoted with a backslash.
    +# This prevents it from being interpreted as starting a comment.
    +.\#*
    +
    +# Temporary files used by the vim editor.
    +.*.swp
    +
    +# A hidden file created by the Mac OS X Finder.
    +.DS_Store
    +
    +# copied from go repo
    +*.[568ao]
    +*.ao
    +*.so
    +*.pyc
    +._*
    +.nfs.*
    +[568a].out
    +*.orig
    +core
    +_obj
    +_test
    +.vscode
    +debug
    \ No newline at end of file
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/main.go Tue Mar 19 20:43:14 2019 -0700
    @@ -0,0 +1,88 @@
    +package main
    +
    +import (
    + "flag"
    + "fmt"
    + "log"
    + "os"
    + "os/signal"
    + "strings"
    + "syscall"
    +
    + "github.com/bwmarrin/discordgo"
    +)
    +
    +var (
    + Token string
    + logger *log.Logger
    +)
    +
    +func init() {
    + logger = log.New(os.Stderr, " ", log.Ldate|log.Ltime)
    +
    + flag.StringVar(&Token, "t", "", "Bot Token")
    + flag.Parse()
    +}
    +
    +func logDebug(v ...interface{}) {
    + logger.SetPrefix("DEBUG ")
    + logger.Println(v...)
    +}
    +
    +func logInfo(v ...interface{}) {
    + logger.SetPrefix("INFO ")
    + logger.Println(v...)
    +}
    +
    +func main() {
    + logDebug("Bot Token:", Token)
    +
    + dg, err := discordgo.New("Bot " + strings.TrimSpace(Token))
    + if err != nil {
    + fmt.Println("error creating Discord session,", err)
    + return
    + }
    +
    + dg.AddHandler(newPresenceEvent)
    + dg.AddHandler(messageCreate)
    +
    + err = dg.Open()
    + if err != nil {
    + log.Panic(err)
    + return
    + }
    +
    + 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 newPresenceEvent(sess *discordgo.Session, evt *discordgo.PresenceUpdate) {
    + logDebug("PRESENSE UPDATE fired for user-ID:", evt.User.ID)
    + logDebug(fmt.Sprintf("PRESENSE UPDATE data: %#v", evt))
    +
    + if evt.Game != nil {
    + logDebug(fmt.Sprintf("PRESENSE UPDATE game: %#v", evt.Game))
    + }
    +}
    +
    +func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
    + logDebug("Message: ", m.Content)
    + // Ignore all messages created by the bot itself
    + // This isn't required in this specific example but it's a good practice.
    + if m.Author.ID == s.State.User.ID {
    + return
    + }
    + // If the message is "ping" reply with "Pong!"
    + if m.Content == "ping" {
    + s.ChannelMessageSend(m.ChannelID, "Pong!")
    + }
    +
    + // If the message is "pong" reply with "Ping!"
    + if m.Content == "pong" {
    + s.ChannelMessageSend(m.ChannelID, "Ping!")
    + }
    +}