Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bytes"
- "encoding/json"
- "io/ioutil"
- "log"
- "math/rand"
- "os"
- "strconv"
- "strings"
- "time"
- "github.com/go-telegram-bot-api/telegram-bot-api"
- )
- // Users :: struct to hold an array of all the users
- type Users struct {
- Users []User `json:"User"`
- }
- // User :: struct to hold settings for a single user
- type User struct {
- Name string `json:"Name"`
- Procs []string `json:"Procs"`
- }
- var commands = map[string]string{"/answer": "answer",
- "/whitepower": "whitepower",
- "/translate": "translate",
- "/roll": "roll",
- "/version": "version",
- "/blackpower": "blackpower",
- "/commands": "commands",
- }
- var answers []string
- var randomAnswers []string
- var currentUser string
- var users Users
- var nigVersion = "Version: 0.1 Go Go Go Go NigNado"
- func main() {
- bot, err := tgbotapi.NewBotAPI("440922504:AAFO9A8OoyN8RW_cKiceFTXK5BUxbR9XDeA")
- if err != nil {
- log.Panic(err)
- }
- bot.Debug = false
- rand.Seed(time.Now().UTC().UnixNano())
- log.Printf("Authorized on account %s", bot.Self.UserName)
- loadAnswers()
- u := tgbotapi.NewUpdate(0)
- u.Timeout = 60
- updates, err := bot.GetUpdatesChan(u)
- for update := range updates {
- if update.Message == nil {
- continue
- }
- chatID := update.Message.Chat.ID
- messageType := messageType(update.Message.Text)
- log.Printf("[ChatID: %v - %s - ID:%v] %s", chatID, update.Message.From.UserName, update.Message.From.ID, update.Message.Text)
- if messageType == "command" {
- processCommand(update.Message.Text, chatID, bot, update.Message.From)
- }
- //Answers with cooldown need to be implemented
- if messageType == "chat" {
- }
- }
- }
- //Loads the textfiles of answers and sends the strings to be filled into the array
- func loadAnswers() {
- pwd, _ := os.Getwd()
- b, err1 := ioutil.ReadFile(pwd + "/answers")
- if err1 != nil {
- log.Panic(err1)
- }
- answerString := string(b)
- b, err := ioutil.ReadFile(pwd + "/random_answers")
- if err != nil {
- log.Panic(err)
- }
- randomAnswerString := string(b)
- b, err2 := ioutil.ReadFile(pwd + "/personal.json")
- if err2 != nil {
- log.Panic(err2)
- }
- if err := json.Unmarshal(b, &users); err != nil {
- log.Fatal(err)
- }
- fillAnswers(answerString, randomAnswerString)
- }
- //Checks what type of message has been sent.
- func messageType(msg string) string {
- command := getCommand(msg)
- for x := range commands {
- if x == command[0] {
- return "command"
- }
- }
- return "chat"
- }
- //Fills the arrays of answers from the string taken from the text file.
- func fillAnswers(answerstring string, randomAnswerString string) {
- answers = strings.Split(answerstring, "\n")
- randomAnswers = strings.Split(randomAnswerString, "\n")
- }
- //Processes the command and does the required action.
- func processCommand(command string, chatID int64, bot *tgbotapi.BotAPI, user *tgbotapi.User) {
- command = strings.ToLower(command)
- if strings.Contains(command, "/whitepower") {
- msg := tgbotapi.NewMessage(chatID, "BLACK POWER!!")
- bot.Send(msg)
- }
- if strings.Contains(command, "/blackpower") {
- msg := tgbotapi.NewMessage(chatID, "FUCK YEAH. BLACK POWER!")
- bot.Send(msg)
- }
- if strings.Contains(command, "/version") {
- msg := tgbotapi.NewMessage(chatID, nigVersion)
- bot.Send(msg)
- }
- if strings.Contains(command, "/commands") {
- msg := tgbotapi.NewMessage(chatID, commandList())
- bot.Send(msg)
- }
- if strings.Contains(command, "/roll") {
- rollDice(command, chatID, bot, user)
- }
- if strings.Contains(command, "/answer") {
- msg := tgbotapi.NewMessage(chatID, answers[rand.Intn(len(answers))])
- bot.Send(msg)
- }
- if strings.Contains(command, "/translate") {
- msg := tgbotapi.NewMessage(chatID, "Noten yeten implementeden")
- bot.Send(msg)
- }
- }
- //Invokes a random with the requested seed and communicates this to the chat
- func rollDice(text string, chatID int64, bot *tgbotapi.BotAPI, user *tgbotapi.User) {
- textInfo := strings.Split(text, " ")
- if len(textInfo) == 2 {
- if checkRange(textInfo[1]) {
- range1, _ := strconv.Atoi(textInfo[1])
- msg := tgbotapi.NewMessage(chatID, "")
- msg.Text = user.UserName + " Rolled: " + strconv.Itoa(rand.Intn(range1))
- bot.Send(msg)
- } else {
- msg := tgbotapi.NewMessage(chatID, "Please don't try to break me you wanker")
- bot.Send(msg)
- }
- }
- //Needs error handling
- if len(textInfo) == 3 {
- if checkRange(textInfo[1]) && checkRange(textInfo[2]) {
- range1, _ := strconv.Atoi(textInfo[1])
- range2, _ := strconv.Atoi(textInfo[2])
- if range1 > range2 {
- msg := tgbotapi.NewMessage(chatID, "Make sure the first item isn't bigger than the second one")
- bot.Send(msg)
- } else {
- msg := tgbotapi.NewMessage(chatID, "")
- msg.Text = user.UserName + " Rolled: " + strconv.Itoa(randInt(range1, range2))
- bot.Send(msg)
- }
- } else {
- msg := tgbotapi.NewMessage(chatID, "You're a fucking bellend")
- bot.Send(msg)
- }
- }
- if len(textInfo) == 1 {
- msg := tgbotapi.NewMessage(chatID, "")
- msg.Text = user.UserName + " Rolled: " + strconv.Itoa(rand.Intn(100))
- bot.Send(msg)
- }
- }
- //Prevents errors during the converting.
- func checkRange(rollRange string) bool {
- newRange, err := strconv.Atoi(rollRange)
- if err != nil {
- return false
- }
- if newRange < 0 {
- return false
- }
- return true
- }
- //Functions as pythons randInt function with a min and max range.
- func randInt(min int, max int) int {
- //Adding 1 so the max seed is also included in the roll.
- max++
- return min + rand.Intn(max-min)
- }
- //Builds a string with all of the available commands.
- func commandList() string {
- var buffer bytes.Buffer
- buffer.WriteString("The following commands are available: \n")
- for x := range commands {
- buffer.WriteString(x + " \n")
- }
- return buffer.String()
- }
- //Splits up the text to see if it is a command
- func getCommand(msg string) []string {
- command := strings.Split(msg, " ")[:1]
- return command
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement