Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "os/signal"
- "regexp"
- "strconv"
- "strings"
- "syscall"
- "time"
- "github.com/bwmarrin/discordgo"
- "gopkg.in/go-playground/validator.v9"
- "gopkg.in/yaml.v2"
- )
- const (
- configureFilePath = "/etc/discord-reminder.yml"
- )
- type (
- Configure struct {
- AllowServersIDs []int `validate:"required" yaml:"allowServerIDs"`
- AllowRemindsPerServer int `validate:"required" yaml:"allowRemindPerServer"`
- AccessToken string `validate:"required" yaml:"accessToken"`
- }
- )
- func getConfigureObjectByFile() (Configure, error) {
- var c Configure
- buf, err := ioutil.ReadFile(configureFilePath)
- if err != nil {
- return c, err
- }
- if err := yaml.UnmarshalStrict(buf, &c); err != nil {
- return c, err
- }
- if err := validator.New().Struct(&c); err != nil {
- return c, err
- }
- return c, nil
- }
- func createNotificationFunctionWithParam(
- s *discordgo.Session,
- channelId string,
- userId string,
- memo string,
- ) func() {
- log.Print("[Reminder] Notification!")
- return func(){
- s.ChannelMessageSend(
- channelId,
- fmt.Sprintf(
- "<@%s> %s",
- userId,
- memo,
- ),
- )
- }
- }
- func main() {
- log.SetFlags(log.LstdFlags | log.Lshortfile)
- // Configure parsing ...
- log.Print("[SYSTEM] Read configure file...")
- c, err := getConfigureObjectByFile()
- if err != nil {
- log.Fatal(err)
- }
- // Bot initialization
- log.Print("[SYSTEM] Initialization...")
- dg, err := discordgo.New("Bot " + c.AccessToken)
- if err != nil {
- log.Fatal("[SYSTEM] ERROR: creating Discord session:", err)
- }
- jst, err := time.LoadLocation("Asia/Tokyo")
- if err != nil {
- log.Fatal("[SYSTEM] ERROR: Failed to LoadLocation:", err)
- }
- reminderConfigureRegex := regexp.MustCompile(`^!r \d\d? .+$`)
- // Handler
- dg.AddHandler(func (
- s *discordgo.Session,
- m *discordgo.MessageCreate,
- ) {
- if m.Author.ID == s.State.User.ID {
- return
- }
- if reminderConfigureRegex.Match([]byte(m.Content)) {
- splittedCommand := strings.Split(m.Content, " ")
- now := time.Now().In(jst)
- memo := strings.Join(splittedCommand[2:], " ")
- hour, err := strconv.Atoi(splittedCommand[1])
- if err != nil {
- return
- }
- if 24 <= hour {
- return
- }
- notificationTime := time.Date(
- now.Year(),
- now.Month(),
- now.Day(),
- hour,
- 0, // min
- 0, // sec
- 0, // nsec
- jst,
- )
- // !(now < notification time)
- if notificationTime.Before(now) {
- notificationTime = notificationTime.Add(time.Hour * 24)
- }
- diff := notificationTime.Sub(now)
- time.AfterFunc(
- diff,
- createNotificationFunctionWithParam(
- s,
- m.ChannelID,
- m.Author.ID,
- memo,
- ),
- )
- s.ChannelMessageSend(
- m.ChannelID,
- fmt.Sprintf(
- "<@%s> Configured reminder after %s.",
- m.Author.ID,
- diff,
- ),
- )
- log.Print(fmt.Sprintf(
- "[Reminder] Configured reminder after %s",
- diff,
- ))
- return
- }
- })
- // Bot start
- if err := dg.Open(); err != nil {
- log.Fatal("[SYSTEM] error open websocket: ", err)
- }
- // Wait
- log.Print("[SYSTEM] Bot is now running. Press ^C to exit.")
- sc := make(chan os.Signal, 1)
- signal.Notify(
- sc,
- syscall.SIGINT,
- syscall.SIGTERM,
- os.Interrupt,
- os.Kill,
- )
- <-sc
- dg.Close()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement