tweeter

Mock Twitter embed generator for Discord
git clone git://git.lleb.me/tweeter
Log | Files | Refs | LICENSE

commit 08592ef82e1265a5de73832db584cc6b1fc831ad
Author: cyckl <cyckl@pm.me>
Date:   Tue,  6 Oct 2020 17:09:10 -0700

First commit of v2

Diffstat:
A.gitignore | 2++
ALICENSE | 29+++++++++++++++++++++++++++++
Aimg/example.png | 0
Aimg/tweeter.png | 0
Amain.go | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atweeter-go | 0
6 files changed, 167 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +*.bat +*.sh diff --git a/LICENSE b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2020, Daniel Calderon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/img/example.png b/img/example.png Binary files differ. diff --git a/img/tweeter.png b/img/tweeter.png Binary files differ. diff --git a/main.go b/main.go @@ -0,0 +1,136 @@ +package main + +import ( + "flag" + "fmt" + "math/rand" + "os" + "os/signal" + "strconv" + "strings" + "syscall" + "time" + + "github.com/bwmarrin/discordgo" +) + +var ( + token string + version string = "2.0.0" +) + +func init() { + flag.StringVar(&token, "t", "", "Bot token") + flag.Parse() +} + +func main() { + // Create new Discord session using bot token + dg, err := discordgo.New("Bot " + token) + if err != nil { + fmt.Println("error creating discord session,", err) + return + } + + // Register the tweet func as a callback for MessageCreate events + dg.AddHandler(tweet) + dg.AddHandler(about) + + // For this example we only care about recieving message events + dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildMessages) + + // Open websocket conn to Discord and listen + err = dg.Open() + if err != nil { + fmt.Println("error opening connection,", err) + return + } + + // Wait here until EOF + fmt.Println("╔═══════════════════════╗") + fmt.Println("║ Tweeter by cyckl ║") + fmt.Println(fmt.Sprintf("║ Running version %s ║", version)) + fmt.Println("╚═══════════════════════╝") + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc + + // Close Discord session cleanly + dg.Close() +} + +func randInt(min, max int) int { + rand.Seed(time.Now().UnixNano()) + return min + rand.Intn(max-min) +} + +func tweet(s *discordgo.Session, m *discordgo.MessageCreate) { + // Ignore bot messages, good practice + if m.Author.ID == s.State.User.ID { + return + } + + // Tweet + if strings.HasPrefix(m.Content, ".t ") { + // Nickname handling + tweetAuthor := "undefined" + if m.Member.Nick != "" { + tweetAuthorValues := []string{m.Member.Nick, " (", m.Author.Username, ")"} + tweetAuthor = strings.Join(tweetAuthorValues, "") + } else { + tweetAuthor = m.Author.Username + } + + // Fill embed and send it + s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{ + Author: &discordgo.MessageEmbedAuthor{ + Name: tweetAuthor, + IconURL: discordgo.EndpointUserAvatar(m.Author.ID, m.Author.Avatar), + }, + Color: 1942002, + Description: strings.TrimPrefix(m.Content, ".t "), + Footer: &discordgo.MessageEmbedFooter{ + Text: "Twitter", + IconURL: "https://abs.twimg.com/icons/apple-touch-icon-192x192.png", + }, + + Fields: []*discordgo.MessageEmbedField{ + {Name: "Retweets", Value: strconv.Itoa(randInt(25000, 50000)), Inline: true}, + {Name: "Likes", Value: strconv.Itoa(randInt(50000, 100000)), Inline: true}, + }, + }) + } +} + +func about(s *discordgo.Session, m *discordgo.MessageCreate) { + // Ignore bot messages, good practice + if m.Author.ID == s.State.User.ID { + return + } + + // Tweet + if m.Content == "t.about" { + // Fill embed and send it + s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{ + Thumbnail: &discordgo.MessageEmbedThumbnail{ + URL: "https://github.com/cyckl/tweeter/raw/master/img/tweeter.png", + }, + Title: "Tweeter by cyckl", + Author: &discordgo.MessageEmbedAuthor{ + Name: "About", + IconURL: "https://github.com/cyckl/tweeter/raw/master/img/tweeter.png", + }, + Color: 16729402, + Description: "Tweeter is a mock Twitter embed generator for Discord.", + Footer: &discordgo.MessageEmbedFooter{ + Text: "This is beta software. Please be patient.", + }, + + Fields: []*discordgo.MessageEmbedField{ + {Name: "Version", Value: version, Inline: true}, + {Name: "Build date", Value: "2020-10-06", Inline: true}, + {Name: "Github", Value: "https://github.com/cyckl/tweeter", Inline: false}, + }, + }) + } +} diff --git a/tweeter-go b/tweeter-go Binary files differ.