Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.74 KB | None | 0 0
  1. package bot
  2.  
  3. import (
  4.     "fmt"
  5.     "time"
  6.     "strconv"
  7.     "strings"
  8.     "github.com/bwmarrin/discordgo"
  9.     "../config"
  10. )
  11.  
  12. var (
  13.     botID string
  14. )
  15.  
  16. func Start() {
  17.     goBot, err := discordgo.New("Bot " + config.Token)
  18.     errCheck("error creating bot session", err)
  19.  
  20.     user, err := goBot.User("@me")
  21.     errCheck("error retrieving account", err)
  22.  
  23.     botID = user.ID
  24.  
  25.     goBot.AddHandler(botInit)
  26.     goBot.AddHandler(commandNewInHandler)
  27.     goBot.AddHandler(commandNewDelHandler)
  28.  
  29.     err = goBot.Open()
  30.     errCheck("Error opening connection to Discord", err)
  31.  
  32.     fmt.Println("Bot is running!")
  33. }
  34.  
  35. func errCheck(msg string, err error) {
  36.     if err != nil {
  37.         fmt.Printf("%s: %+v", msg, err)
  38.         panic(err)
  39.     }
  40. }
  41.  
  42. func botInit(discord *discordgo.Session, ready *discordgo.Ready) {
  43.     err := discord.UpdateStatus(0, "A friendly helpful bot!")
  44.     if err != nil {
  45.         errCheck("Error attempting to set my status", err)
  46.     }
  47.     servers := discord.State.Guilds
  48.     fmt.Printf("AnimeHater Bot has started on %d servers", len(servers))
  49. }
  50.  
  51. func commandNewInHandler(discord *discordgo.Session, message *discordgo.MessageCreate) {
  52.     if message.Author.ID == botID || message.Author.Bot {
  53.         return
  54.     }
  55.  
  56.     if strings.HasPrefix(message.Content, config.BotPrefix) {
  57.         command := strings.TrimPrefix(message.Content, config.BotPrefix)
  58.  
  59.         if command == "Greetings" {
  60.             _, _ = discord.ChannelMessageSend(message.ChannelID, "Здарова, очередняра!")
  61.         }
  62.  
  63.         if command == "Time" {
  64.  
  65.             weekdayRu := make(map[string]string)
  66.             weekdayRu["Monday"] = "Пн"
  67.             weekdayRu["Tuesday"] = "Вт"
  68.             weekdayRu["Wednesday"] = "Ср"
  69.             weekdayRu["Thursday"] = "Чт"
  70.             weekdayRu["Friday"] = "Пт"
  71.             weekdayRu["Saturday"] = "Сб"
  72.             weekdayRu["Sunday"] = "Вс"
  73.  
  74.             monthRU := make(map[string]string)
  75.             monthRU["January"] = "Январь"
  76.             monthRU["February"] = "Февраль"
  77.             monthRU["March"] = "Март"
  78.             monthRU["April"] = "Апрель"
  79.             monthRU["May"] = "Май"
  80.             monthRU["June"] = "Июнь"
  81.             monthRU["July"] = "Июль"
  82.             monthRU["August"] = "Август"
  83.             monthRU["September"] = "Сентябрь"
  84.             monthRU["October"] = "Октябрь"
  85.             monthRU["November"] = "Ноябрь"
  86.             monthRU["December"] = "Декабрь"
  87.  
  88.             loc, _ := time.LoadLocation("Europe/Moscow")
  89.             now := time.Now().In(loc)
  90.  
  91.             _, _ = discord.ChannelMessageSend(
  92.                 message.ChannelID, "Текущее время по Москве: " + weekdayRu[now.Weekday().String()] + " " + monthRU[now.Month().String()] + " "+
  93.                     strconv.Itoa(now.Day())+ " "+ now.Format("15:04:05"))
  94.         }
  95.     }
  96. }
  97.  
  98. func commandNewDelHandler(discord *discordgo.Session, message *discordgo.MessageDelete) {
  99.     _, _ = discord.ChannelMessageSend(message.ChannelID, "Message\n" + message.Content + "\nwas deleted")
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement