Skip to content
Permalink
Browse files

Create main.go

  • Loading branch information...
Fritsbie committed Jul 18, 2019
1 parent ac64aae commit 38607b9906e43cd1d11010840d979c62dc870c01
Showing with 232 additions and 0 deletions.
  1. +232 −0 main.go
232 main.go
@@ -0,0 +1,232 @@
package main

import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/bvinc/go-sqlite-lite/sqlite3"
"github.com/bwmarrin/discordgo"
)

//********** Insert your own Token Here **********//
var botToken = "----------------------------------"

//Global Variables for Discord Session & Sqlite Connection.
var dg *discordgo.Session
var conn *sqlite3.Conn

func main() {

//Connect to the Sqlite Database
var era error
conn, era = sqlite3.Open("zuccLife.db")
if era != nil {
fmt.Println("Error opening the Database", era)
}
defer conn.Close()

//Createa a Discord session
var err error
dg, err = discordgo.New("Bot " + botToken)
if err != nil {
fmt.Println("Error creating the Discord session", err)
return
}

//If a Message Arrives(yummyyyyy..), direct it to the messageArrived function.
dg.AddHandler(messageArrived)

//Open a Websocket
err = dg.Open()
if err != nil {
fmt.Println("Error opening the connection", err)
return
}

// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc

// Cleanly close down the Discord session.
dg.Close()
}

//Whenever a Message Arrives, here I'll process it.
func messageArrived(s *discordgo.Session, m *discordgo.MessageCreate) {

//It's Zucc's message, ignore...
if m.Author.ID == s.State.User.ID {
return
}

//Values parsed from the Incoming Message.
uID := m.Author.ID
uName := m.Author.Username
guildID := m.GuildID
guildNameEntr, _ := s.State.Guild(guildID)
channelID := m.ChannelID
channelNameEntr, _ := s.State.Channel(channelID)
messageID := m.ID
message := m.Content
t := time.Now()
v := t.Format("2006-01-02 15:04:05")

//Record the message right away!
var erol error
erol = conn.Exec(`INSERT INTO messages VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, messageID, guildID, channelID, guildNameEntr.Name, channelNameEntr.Name, uID, uName, m.Content, v)
if erol != nil {
fmt.Println("I couldn't record the message. Oppss", erol)
s.ChannelMessageSend(channelID, "What kind of message did you send "+uName+". I can't record it???")
}

//Update Bot Status. In Discord it'll look as (Zucc is listening to Fritsbie's private life...)
dg.UpdateListeningStatus(uName + "'s private life...")

//Now, let's parse the Message itself.
switch {

//!help command (types as !halp <MEMES>)
case message == "!halp":
s.ChannelMessageSend(channelID, "**Zucc commands** - *everything is secret, unlike your personal life*\n**!pistolZucc** : Displays DELET picture of Zucc.\n**zucced** : Zucc this\n**zuccLatest <username>** : Displays latest 3 messages of the <username> user\n**!zuccRandom <username>** : Displays a random message of the <username>")

//Check if the message contains forsenKek emoji for 3x times.
case strings.Contains(message, ":forsenKek: :forsenKek: :forsenKek:"):
s.ChannelMessageSend(channelID, "Stop trolling "+uName)

//Check if message contains !pistolZucc. It creates an embed message with Zucc image and a Title & Description.
case strings.Contains(message, "!pistolZucc") == true:
embed := &discordgo.MessageEmbed{
//Author: &discordgo.MessageEmbedAuthor{},
Color: 11534336, // Red Colour
Description: "You must delet this",
Image: &discordgo.MessageEmbedImage{
URL: "http://i65.tinypic.com/2125bm.jpg",
},
Timestamp: time.Now().Format(time.RFC3339),
Title: "ZUCC ALERT",
}
s.ChannelMessageSendEmbed(channelID, embed)

//Check if the message contains zucced.
case strings.Contains(message, "zucced") == true:
embed := &discordgo.MessageEmbed{
//Author: &discordgo.MessageEmbedAuthor{},
Color: 11534336, // Red Colour
Description: "Everying is and will get zucced!",
Image: &discordgo.MessageEmbedImage{
URL: "http://i64.tinypic.com/16iikus.jpg",
},
Timestamp: time.Now().Format(time.RFC3339),
Title: "ZUCCED",
}

s.ChannelMessageSendEmbed(channelID, embed)

//Check if the message contains !zuccLatest
//Here I get Latest 3 messages of a user and sent it back.
//Example !zuccLatest Martinsteiner
//Retrieves Martinsteiner's latest 3 messages.
case strings.Contains(message, "!zuccLatest "):
//Parse the string, separate the command !zuccLatest(11 letters) + 1 and the username
reinRune := message[12:len(message)]
//Statement -> Select the data, Order by the time in Descending order(means tha latest messages)
stmt, err := conn.Prepare(`SELECT guildname, message, channelname, time FROM messages WHERE sendername = ? ORDER BY time DESC LIMIT 3`, reinRune)
if err != nil {
s.ChannelMessageSend(channelID, "**Zucc had some error** while fetching your private life...")
}
defer stmt.Close()

var outPut strings.Builder
outPut.WriteString("**Zucc recorded messages** for ||*" + reinRune + "*||")

for {
hasRow, err := stmt.Step()
if err != nil {
s.ChannelMessageSend(m.ChannelID, "**Zucc had some error** while fetching your private life...")
}
if !hasRow {
break
}

var message string
var guildname string
var channelname string
var time string
err = stmt.Scan(&guildname, &message, &channelname, &time)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "**Zucc had some error** while fetching your private life...")
}

outPut.WriteString("\n*")
outPut.WriteString(guildname)
outPut.WriteString("/")
outPut.WriteString(channelname)
outPut.WriteString(":")
outPut.WriteString(time)
outPut.WriteString("*\n**")
outPut.WriteString(message)
outPut.WriteString("**\n\n")
}

s.ChannelMessageSend(m.ChannelID, outPut.String())

//Same as above, instead we get a random message.
//It only gets 1 message
//You can increase this number by editing LIMIT 1 to LIMIT 3 etc.
case strings.Contains(message, "!zuccRandom "):
reinRune := message[12:len(message)]

stmt, err := conn.Prepare(`SELECT guildname, message, channelname, time FROM messages WHERE sendername = ? ORDER BY Random() LIMIT 1`, reinRune)
if err != nil {
s.ChannelMessageSend(channelID, "**Zucc had some error** while fetching your private life...")
}
defer stmt.Close()

var outPut strings.Builder
outPut.WriteString("**Zucc knows what you did in the past **||*" + reinRune + "*||")

for {
hasRow, err := stmt.Step()
if err != nil {
s.ChannelMessageSend(m.ChannelID, "**Zucc had some error** while fetching your private life...")
}
if !hasRow {
break
}

var message string
var guildname string
var channelname string
var time string
err = stmt.Scan(&guildname, &message, &channelname, &time)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "**Zucc had some error** while fetching your private life...")
}

outPut.WriteString("\n*")
outPut.WriteString(guildname)
outPut.WriteString("/")
outPut.WriteString(channelname)
outPut.WriteString(":")
outPut.WriteString(time)
outPut.WriteString("*\n**")
outPut.WriteString(message)
outPut.WriteString("**\n\n")
}

s.ChannelMessageSend(m.ChannelID, outPut.String())

default:
//I was going to something here
//But I decided not to
//Lol

}

}

0 comments on commit 38607b9

Please sign in to comment.
You can’t perform that action at this time.