grim/wasdead

Parents 21336f609455
Children e0b5d3efe80c
!uptime command is now a thing, I just needed this mainly for testing, and maybe we should look into getting a database setup later so the bot can ya know remember what channelID it should talk to within that guild for notifications so it doesn't just spam and get rate limited
  • +27 -11
    main.go
  • --- a/main.go Wed Mar 20 03:49:43 2019 +0000
    +++ b/main.go Tue Mar 19 21:40:24 2019 -0700
    @@ -8,17 +8,20 @@
    "os/signal"
    "strings"
    "syscall"
    + "time"
    "github.com/bwmarrin/discordgo"
    )
    var (
    - Token string
    - logger *log.Logger
    + Token string
    + logger *log.Logger
    + startTime time.Time
    )
    func init() {
    logger = log.New(os.Stderr, " ", log.Ldate|log.Ltime)
    + startTime = time.Now()
    flag.StringVar(&Token, "t", "", "Bot Token")
    flag.Parse()
    @@ -34,6 +37,11 @@
    logger.Println(v...)
    }
    +func logError(v ...interface{}) {
    + logger.SetPrefix("ERROR ")
    + logger.Println(v...)
    +}
    +
    func main() {
    logDebug("Bot Token:", Token)
    @@ -71,18 +79,26 @@
    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!")
    + if strings.EqualFold(m.Content, "!uptime") {
    + duration := time.Now().Sub(startTime)
    + err := sendMessage(s, m.ChannelID, fmt.Sprintf(
    + "Uptime is: **%02d:%02d:%02d** (since **%s**)",
    + int(duration.Hours()),
    + int(duration.Minutes())%60,
    + int(duration.Seconds())%60,
    + startTime.Format(time.Stamp)))
    +
    + logError(err)
    }
    }
    +
    +func sendMessage(sess *discordgo.Session, channelid, message string) error {
    + logInfo("SENDING MESSAGE:", message)
    + _, err := sess.ChannelMessageSend(channelid, message)
    + return err
    +}