More commands
This commit is contained in:
76
commands/fortune.go
Normal file
76
commands/fortune.go
Normal file
@ -0,0 +1,76 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type FortuneCommand struct{}
|
||||
|
||||
func (f FortuneCommand) Name() string {
|
||||
return "fortune"
|
||||
}
|
||||
|
||||
func (f FortuneCommand) Help() string {
|
||||
return "Get your fortune. Usage: /fortune"
|
||||
}
|
||||
|
||||
func (f FortuneCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
fortune, err := getRandomFortune()
|
||||
if err != nil {
|
||||
log.Printf("Error getting fortune: %v", err)
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "❌ Error loading fortune database. Please try again later.")
|
||||
bot.Send(msg)
|
||||
return
|
||||
}
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, fortune)
|
||||
bot.Send(msg)
|
||||
}
|
||||
|
||||
func getRandomFortune() (string, error) {
|
||||
// Try current json directory first, then fallback to old_json
|
||||
file, err := os.Open("json/fortune.json")
|
||||
if err != nil {
|
||||
// Try old_json directory
|
||||
file, err = os.Open("old_json/fortune.json")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open fortune.json: %v", err)
|
||||
}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Read the file content
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read fortune.json: %v", err)
|
||||
}
|
||||
|
||||
// Parse JSON array
|
||||
var fortunes []string
|
||||
if err := json.Unmarshal(data, &fortunes); err != nil {
|
||||
return "", fmt.Errorf("failed to parse fortune.json: %v", err)
|
||||
}
|
||||
|
||||
if len(fortunes) == 0 {
|
||||
return "", fmt.Errorf("fortune database is empty")
|
||||
}
|
||||
|
||||
// Seed random number generator
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// Pick a random fortune
|
||||
randomIndex := rand.Intn(len(fortunes))
|
||||
return fortunes[randomIndex], nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(FortuneCommand{})
|
||||
}
|
@ -17,7 +17,7 @@ func (i InfoCommand) Help() string {
|
||||
}
|
||||
|
||||
func (i InfoCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "This bot does dumb stuff and chemistry. Now version 2.0, rewritten in Go.")
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "This bot does dumb stuff and chemistry. Now version 2.0, rewritten in Go.\n\nSauce: https://gitea.boner.be/nignogbot/nignoggobot\n\nComplaints and support: @fatboners")
|
||||
_, err := bot.Send(msg)
|
||||
if err != nil {
|
||||
log.Println("Failed to send info message:", err)
|
||||
|
66
commands/kekget.go
Normal file
66
commands/kekget.go
Normal file
@ -0,0 +1,66 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type KekgetCommand struct{}
|
||||
|
||||
func (k KekgetCommand) Name() string {
|
||||
return "kekget"
|
||||
}
|
||||
|
||||
func (k KekgetCommand) Help() string {
|
||||
return "Try to get a KEK or KKK, or even a multiKEK. Usage: /kekget"
|
||||
}
|
||||
|
||||
func (k KekgetCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
result := generateKekget()
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, result)
|
||||
bot.Send(msg)
|
||||
}
|
||||
|
||||
func generateKekget() string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// Generate random length between 1 and 20
|
||||
length := rand.Intn(20) + 1
|
||||
|
||||
var text strings.Builder
|
||||
for i := 0; i < length; i++ {
|
||||
if rand.Float64() > 0.5 {
|
||||
text.WriteString("K")
|
||||
} else {
|
||||
text.WriteString("E")
|
||||
}
|
||||
}
|
||||
|
||||
result := text.String()
|
||||
|
||||
// Check for special patterns
|
||||
switch result {
|
||||
case "KEK":
|
||||
return fmt.Sprintf("%s\nYOU WIN TOPKEK!!!", result)
|
||||
case "KKK":
|
||||
return fmt.Sprintf("%s\nYOU WIN TOPKKK HEIL HITLER!!!", result)
|
||||
case "KEKKEK":
|
||||
return fmt.Sprintf("%s\nYOU WIN DOUBLE TOPKEKKEK!!!", result)
|
||||
case "KEKKEKKEK":
|
||||
return fmt.Sprintf("%s\nYOU WIN ULTIMATE TRIPLE TOPKEKKEKKEK!!!", result)
|
||||
case "KEKKEKKEKKEK":
|
||||
return fmt.Sprintf("%s\nQUADDRUPPLE TOPKEKKEKKEKKEK!!! YOU ARE GAY!!!", result)
|
||||
case "KEKKEKKEKKEKKEK":
|
||||
return fmt.Sprintf("%s\nQUINTUPLE TOPKEKKEKKEKKEKKEK!!! UNBELIEVABLE M8!!!", result)
|
||||
default:
|
||||
return fmt.Sprintf("%s\nLength: %d", result, len(result))
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(KekgetCommand{})
|
||||
}
|
24
commands/lenny.go
Normal file
24
commands/lenny.go
Normal file
@ -0,0 +1,24 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type LennyCommand struct{}
|
||||
|
||||
func (l LennyCommand) Name() string {
|
||||
return "lenny"
|
||||
}
|
||||
|
||||
func (l LennyCommand) Help() string {
|
||||
return "( ͡° ͜ʖ ͡°) Usage: /lenny"
|
||||
}
|
||||
|
||||
func (l LennyCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "( ͡° ͜ʖ ͡°)")
|
||||
bot.Send(msg)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(LennyCommand{})
|
||||
}
|
81
commands/troll.go
Normal file
81
commands/troll.go
Normal file
@ -0,0 +1,81 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type TrollCommand struct{}
|
||||
|
||||
func (t TrollCommand) Name() string {
|
||||
return "troll"
|
||||
}
|
||||
|
||||
func (t TrollCommand) Help() string {
|
||||
return "Get a random troll message from the database. Usage: /troll"
|
||||
}
|
||||
|
||||
func (t TrollCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
// Send "typing" action
|
||||
typingAction := tgbotapi.NewChatAction(update.Message.Chat.ID, tgbotapi.ChatTyping)
|
||||
bot.Send(typingAction)
|
||||
|
||||
trollMessage, err := getRandomTrollMessage()
|
||||
if err != nil {
|
||||
log.Printf("Error getting troll message: %v", err)
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "❌ Error loading troll database. Please try again later.")
|
||||
bot.Send(msg)
|
||||
return
|
||||
}
|
||||
|
||||
// Limit message length for Telegram (max 4096 characters)
|
||||
if len(trollMessage) > 4000 {
|
||||
trollMessage = trollMessage[:3997] + "..."
|
||||
}
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, trollMessage)
|
||||
bot.Send(msg)
|
||||
}
|
||||
|
||||
func getRandomTrollMessage() (string, error) {
|
||||
// Open the troll database file
|
||||
file, err := os.Open("json/trolldb.json")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open trolldb.json: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Read the file content
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read trolldb.json: %v", err)
|
||||
}
|
||||
|
||||
// Parse JSON array
|
||||
var trollMessages []string
|
||||
if err := json.Unmarshal(data, &trollMessages); err != nil {
|
||||
return "", fmt.Errorf("failed to parse trolldb.json: %v", err)
|
||||
}
|
||||
|
||||
if len(trollMessages) == 0 {
|
||||
return "", fmt.Errorf("troll database is empty")
|
||||
}
|
||||
|
||||
// Seed random number generator
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// Pick a random message
|
||||
randomIndex := rand.Intn(len(trollMessages))
|
||||
return trollMessages[randomIndex], nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(TrollCommand{})
|
||||
}
|
133
commands/xkcd.go
Normal file
133
commands/xkcd.go
Normal file
@ -0,0 +1,133 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type XkcdCommand struct{}
|
||||
|
||||
type XkcdComic struct {
|
||||
Num int `json:"num"`
|
||||
Title string `json:"title"`
|
||||
SafeTitle string `json:"safe_title"`
|
||||
Img string `json:"img"`
|
||||
Alt string `json:"alt"`
|
||||
Transcript string `json:"transcript"`
|
||||
Link string `json:"link"`
|
||||
News string `json:"news"`
|
||||
Year string `json:"year"`
|
||||
Month string `json:"month"`
|
||||
Day string `json:"day"`
|
||||
}
|
||||
|
||||
func (x XkcdCommand) Name() string {
|
||||
return "xkcd"
|
||||
}
|
||||
|
||||
func (x XkcdCommand) Help() string {
|
||||
return "Get a random XKCD comic. Usage: /xkcd"
|
||||
}
|
||||
|
||||
func (x XkcdCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
// Send "typing" action
|
||||
typingAction := tgbotapi.NewChatAction(update.Message.Chat.ID, tgbotapi.ChatTyping)
|
||||
bot.Send(typingAction)
|
||||
|
||||
comic, err := getRandomXkcdComic()
|
||||
if err != nil {
|
||||
log.Printf("Error getting XKCD comic: %v", err)
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "❌ Error fetching XKCD comic. Please try again later.")
|
||||
bot.Send(msg)
|
||||
return
|
||||
}
|
||||
|
||||
// Create caption
|
||||
caption := createXkcdCaption(comic)
|
||||
|
||||
// Send photo with caption
|
||||
photo := tgbotapi.NewPhoto(update.Message.Chat.ID, tgbotapi.FileURL(comic.Img))
|
||||
photo.Caption = caption
|
||||
bot.Send(photo)
|
||||
}
|
||||
|
||||
func getRandomXkcdComic() (*XkcdComic, error) {
|
||||
// First get the latest comic to know the range
|
||||
resp, err := http.Get("https://xkcd.com/info.0.json")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get latest comic info: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("XKCD API returned status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response: %v", err)
|
||||
}
|
||||
|
||||
var latest XkcdComic
|
||||
if err := json.Unmarshal(body, &latest); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse latest comic: %v", err)
|
||||
}
|
||||
|
||||
// Generate random comic number (avoiding 404 which doesn't exist)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
var randomNum int
|
||||
for {
|
||||
randomNum = rand.Intn(latest.Num) + 1
|
||||
if randomNum != 404 { // Comic 404 doesn't exist
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Get the random comic
|
||||
url := fmt.Sprintf("https://xkcd.com/%d/info.0.json", randomNum)
|
||||
resp, err = http.Get(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get comic %d: %v", randomNum, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("comic %d returned status: %d", randomNum, resp.StatusCode)
|
||||
}
|
||||
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read comic response: %v", err)
|
||||
}
|
||||
|
||||
var comic XkcdComic
|
||||
if err := json.Unmarshal(body, &comic); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse comic: %v", err)
|
||||
}
|
||||
|
||||
return &comic, nil
|
||||
}
|
||||
|
||||
func createXkcdCaption(comic *XkcdComic) string {
|
||||
url := fmt.Sprintf("https://xkcd.com/%d/", comic.Num)
|
||||
|
||||
// Try to include alt text if caption isn't too long
|
||||
fullCaption := fmt.Sprintf("%s\n\n%s\n\n%s", comic.Title, comic.Alt, url)
|
||||
if len(fullCaption) <= 1000 { // Keep it reasonable for Telegram
|
||||
return fullCaption
|
||||
}
|
||||
|
||||
// Fallback to just title and URL if too long
|
||||
return fmt.Sprintf("%s\n\n%s", comic.Title, url)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(XkcdCommand{})
|
||||
}
|
Reference in New Issue
Block a user