daily pastebin goal
7%
SHARE
TWEET

Untitled

a guest Jun 4th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "bytes"
  5.     "encoding/json"
  6.     "io/ioutil"
  7.     "log"
  8.     "math/rand"
  9.     "os"
  10.     "strconv"
  11.     "strings"
  12.     "time"
  13.  
  14.     "github.com/go-telegram-bot-api/telegram-bot-api"
  15. )
  16.  
  17. // Users :: struct to hold an array of all the users
  18. type Users struct {
  19.     Users []User `json:"User"`
  20. }
  21.  
  22. // User :: struct to hold settings for a single user
  23. type User struct {
  24.     Name  string   `json:"Name"`
  25.     Procs []string `json:"Procs"`
  26. }
  27.  
  28. var commands = map[string]string{"/answer": "answer",
  29.     "/whitepower": "whitepower",
  30.     "/translate":  "translate",
  31.     "/roll":       "roll",
  32.     "/version":    "version",
  33.     "/blackpower": "blackpower",
  34.     "/commands":   "commands",
  35. }
  36.  
  37. var answers []string
  38. var randomAnswers []string
  39. var currentUser string
  40. var users Users
  41.  
  42. var nigVersion = "Version: 0.1 Go Go Go Go NigNado"
  43.  
  44. func main() {
  45.     bot, err := tgbotapi.NewBotAPI("440922504:AAFO9A8OoyN8RW_cKiceFTXK5BUxbR9XDeA")
  46.     if err != nil {
  47.         log.Panic(err)
  48.     }
  49.  
  50.     bot.Debug = false
  51.     rand.Seed(time.Now().UTC().UnixNano())
  52.  
  53.     log.Printf("Authorized on account %s", bot.Self.UserName)
  54.  
  55.     loadAnswers()
  56.  
  57.     u := tgbotapi.NewUpdate(0)
  58.     u.Timeout = 60
  59.  
  60.     updates, err := bot.GetUpdatesChan(u)
  61.  
  62.     for update := range updates {
  63.         if update.Message == nil {
  64.             continue
  65.         }
  66.  
  67.         chatID := update.Message.Chat.ID
  68.         messageType := messageType(update.Message.Text)
  69.  
  70.         log.Printf("[ChatID: %v - %s - ID:%v] %s", chatID, update.Message.From.UserName, update.Message.From.ID, update.Message.Text)
  71.  
  72.         if messageType == "command" {
  73.             processCommand(update.Message.Text, chatID, bot, update.Message.From)
  74.         }
  75.         //Answers with cooldown need to be implemented
  76.         if messageType == "chat" {
  77.  
  78.         }
  79.     }
  80. }
  81.  
  82. //Loads the textfiles of answers and sends the strings to be filled into the array
  83. func loadAnswers() {
  84.  
  85.     pwd, _ := os.Getwd()
  86.  
  87.     b, err1 := ioutil.ReadFile(pwd + "/answers")
  88.     if err1 != nil {
  89.         log.Panic(err1)
  90.     }
  91.     answerString := string(b)
  92.  
  93.     b, err := ioutil.ReadFile(pwd + "/random_answers")
  94.     if err != nil {
  95.         log.Panic(err)
  96.     }
  97.     randomAnswerString := string(b)
  98.  
  99.     b, err2 := ioutil.ReadFile(pwd + "/personal.json")
  100.     if err2 != nil {
  101.         log.Panic(err2)
  102.     }
  103.  
  104.     if err := json.Unmarshal(b, &users); err != nil {
  105.         log.Fatal(err)
  106.     }
  107.  
  108.     fillAnswers(answerString, randomAnswerString)
  109. }
  110.  
  111. //Checks what type of message has been sent.
  112. func messageType(msg string) string {
  113.  
  114.     command := getCommand(msg)
  115.  
  116.     for x := range commands {
  117.         if x == command[0] {
  118.             return "command"
  119.         }
  120.     }
  121.     return "chat"
  122. }
  123.  
  124. //Fills the arrays of answers from the string taken from the text file.
  125. func fillAnswers(answerstring string, randomAnswerString string) {
  126.  
  127.     answers = strings.Split(answerstring, "\n")
  128.     randomAnswers = strings.Split(randomAnswerString, "\n")
  129. }
  130.  
  131. //Processes the command and does the required action.
  132. func processCommand(command string, chatID int64, bot *tgbotapi.BotAPI, user *tgbotapi.User) {
  133.  
  134.     command = strings.ToLower(command)
  135.  
  136.     if strings.Contains(command, "/whitepower") {
  137.         msg := tgbotapi.NewMessage(chatID, "BLACK POWER!!")
  138.         bot.Send(msg)
  139.     }
  140.     if strings.Contains(command, "/blackpower") {
  141.         msg := tgbotapi.NewMessage(chatID, "FUCK YEAH. BLACK POWER!")
  142.         bot.Send(msg)
  143.     }
  144.     if strings.Contains(command, "/version") {
  145.         msg := tgbotapi.NewMessage(chatID, nigVersion)
  146.         bot.Send(msg)
  147.     }
  148.     if strings.Contains(command, "/commands") {
  149.         msg := tgbotapi.NewMessage(chatID, commandList())
  150.         bot.Send(msg)
  151.     }
  152.     if strings.Contains(command, "/roll") {
  153.         rollDice(command, chatID, bot, user)
  154.     }
  155.     if strings.Contains(command, "/answer") {
  156.         msg := tgbotapi.NewMessage(chatID, answers[rand.Intn(len(answers))])
  157.         bot.Send(msg)
  158.     }
  159.     if strings.Contains(command, "/translate") {
  160.         msg := tgbotapi.NewMessage(chatID, "Noten yeten implementeden")
  161.         bot.Send(msg)
  162.     }
  163. }
  164.  
  165. //Invokes a random with the requested seed and communicates this to the chat
  166. func rollDice(text string, chatID int64, bot *tgbotapi.BotAPI, user *tgbotapi.User) {
  167.  
  168.     textInfo := strings.Split(text, " ")
  169.  
  170.     if len(textInfo) == 2 {
  171.         if checkRange(textInfo[1]) {
  172.             range1, _ := strconv.Atoi(textInfo[1])
  173.             msg := tgbotapi.NewMessage(chatID, "")
  174.             msg.Text = user.UserName + " Rolled: " + strconv.Itoa(rand.Intn(range1))
  175.             bot.Send(msg)
  176.  
  177.         } else {
  178.             msg := tgbotapi.NewMessage(chatID, "Please don't try to break me you wanker")
  179.             bot.Send(msg)
  180.         }
  181.     }
  182.     //Needs error handling
  183.     if len(textInfo) == 3 {
  184.         if checkRange(textInfo[1]) && checkRange(textInfo[2]) {
  185.             range1, _ := strconv.Atoi(textInfo[1])
  186.             range2, _ := strconv.Atoi(textInfo[2])
  187.             if range1 > range2 {
  188.                 msg := tgbotapi.NewMessage(chatID, "Make sure the first item isn't bigger than the second one")
  189.                 bot.Send(msg)
  190.             } else {
  191.                 msg := tgbotapi.NewMessage(chatID, "")
  192.                 msg.Text = user.UserName + " Rolled: " + strconv.Itoa(randInt(range1, range2))
  193.                 bot.Send(msg)
  194.  
  195.             }
  196.         } else {
  197.             msg := tgbotapi.NewMessage(chatID, "You're a fucking bellend")
  198.             bot.Send(msg)
  199.         }
  200.     }
  201.     if len(textInfo) == 1 {
  202.         msg := tgbotapi.NewMessage(chatID, "")
  203.         msg.Text = user.UserName + " Rolled: " + strconv.Itoa(rand.Intn(100))
  204.         bot.Send(msg)
  205.     }
  206. }
  207.  
  208. //Prevents errors during the converting.
  209. func checkRange(rollRange string) bool {
  210.  
  211.     newRange, err := strconv.Atoi(rollRange)
  212.     if err != nil {
  213.         return false
  214.     }
  215.     if newRange < 0 {
  216.         return false
  217.     }
  218.     return true
  219. }
  220.  
  221. //Functions as pythons randInt function with a min and max range.
  222. func randInt(min int, max int) int {
  223.     //Adding 1 so the max seed is also included in the roll.
  224.     max++
  225.     return min + rand.Intn(max-min)
  226. }
  227.  
  228. //Builds a string with all of the available commands.
  229. func commandList() string {
  230.     var buffer bytes.Buffer
  231.  
  232.     buffer.WriteString("The following commands are available: \n")
  233.  
  234.     for x := range commands {
  235.         buffer.WriteString(x + " \n")
  236.     }
  237.  
  238.     return buffer.String()
  239. }
  240.  
  241. //Splits up the text to see if it is a command
  242. func getCommand(msg string) []string {
  243.  
  244.     command := strings.Split(msg, " ")[:1]
  245.  
  246.     return command
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top