2021-05-17 22:42:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
dg "github.com/bwmarrin/discordgo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DiscordDaemon struct {
|
2021-05-21 20:35:25 +00:00
|
|
|
Stopped bool
|
|
|
|
ShutdownChannel chan string
|
|
|
|
bot *dg.Session
|
|
|
|
username string
|
|
|
|
tokens []string
|
|
|
|
verifiedTokens map[string]DiscordUser // Map of tokens to discord users.
|
|
|
|
channelID, channelName string
|
|
|
|
serverChannelName string
|
|
|
|
users map[string]DiscordUser // Map of user IDs to users. Added to on first interaction, and loaded from app.storage.discord on start.
|
|
|
|
app *appContext
|
2021-05-17 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newDiscordDaemon(app *appContext) (*DiscordDaemon, error) {
|
|
|
|
token := app.config.Section("discord").Key("token").String()
|
|
|
|
if token == "" {
|
|
|
|
return nil, fmt.Errorf("token was blank")
|
|
|
|
}
|
|
|
|
bot, err := dg.New("Bot " + token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dd := &DiscordDaemon{
|
|
|
|
Stopped: false,
|
|
|
|
ShutdownChannel: make(chan string),
|
|
|
|
bot: bot,
|
2021-05-21 20:35:25 +00:00
|
|
|
tokens: []string{},
|
|
|
|
verifiedTokens: map[string]DiscordUser{},
|
|
|
|
users: map[string]DiscordUser{},
|
2021-05-17 22:42:33 +00:00
|
|
|
app: app,
|
|
|
|
}
|
|
|
|
for _, user := range app.storage.discord {
|
2021-05-21 20:35:25 +00:00
|
|
|
dd.users[user.ID] = user
|
2021-05-17 22:42:33 +00:00
|
|
|
}
|
|
|
|
return dd, nil
|
|
|
|
}
|
|
|
|
|
2021-05-21 20:35:25 +00:00
|
|
|
// NewAuthToken generates an 8-character pin in the form "A1-2B-CD".
|
|
|
|
func (d *DiscordDaemon) NewAuthToken() string {
|
2021-05-17 22:42:33 +00:00
|
|
|
pin := genAuthToken()
|
2021-05-21 20:35:25 +00:00
|
|
|
d.tokens = append(d.tokens, pin)
|
|
|
|
return pin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) NewUnknownUser(channelID, userID, discrim, username string) DiscordUser {
|
|
|
|
user := DiscordUser{
|
|
|
|
ChannelID: channelID,
|
|
|
|
ID: userID,
|
|
|
|
Username: username,
|
|
|
|
Discriminator: discrim,
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) MustGetUser(channelID, userID, discrim, username string) DiscordUser {
|
|
|
|
if user, ok := d.users[userID]; ok {
|
|
|
|
return user
|
2021-05-17 22:42:33 +00:00
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
return d.NewUnknownUser(channelID, userID, discrim, username)
|
2021-05-17 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) run() {
|
|
|
|
d.bot.AddHandler(d.messageHandler)
|
|
|
|
d.bot.Identify.Intents = dg.IntentsGuildMessages | dg.IntentsDirectMessages
|
|
|
|
if err := d.bot.Open(); err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to start daemon: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.username = d.bot.State.User.Username
|
2021-05-21 20:35:25 +00:00
|
|
|
// Choose the last guild (server), for now we don't really support multiple anyway
|
|
|
|
guild, err := d.bot.Guild(d.bot.State.Guilds[len(d.bot.State.Guilds)-1].ID)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to get guild: %v", err)
|
|
|
|
}
|
|
|
|
d.serverChannelName = guild.Name
|
|
|
|
if channel := d.app.config.Section("discord").Key("channel").String(); channel != "" {
|
|
|
|
d.channelName = channel
|
|
|
|
}
|
2021-05-17 22:42:33 +00:00
|
|
|
defer d.bot.Close()
|
|
|
|
<-d.ShutdownChannel
|
|
|
|
d.ShutdownChannel <- "Down"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) Shutdown() {
|
|
|
|
d.Stopped = true
|
|
|
|
d.ShutdownChannel <- "Down"
|
|
|
|
<-d.ShutdownChannel
|
|
|
|
close(d.ShutdownChannel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) messageHandler(s *dg.Session, m *dg.MessageCreate) {
|
2021-05-21 20:35:25 +00:00
|
|
|
if m.GuildID != "" && d.channelName != "" {
|
|
|
|
if d.channelID == "" {
|
|
|
|
channel, err := s.Channel(m.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Couldn't get channel, will monitor all: %v", err)
|
|
|
|
d.channelName = ""
|
|
|
|
}
|
|
|
|
if channel.Name == d.channelName {
|
|
|
|
d.channelID = channel.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.channelID != m.ChannelID {
|
|
|
|
d.app.debug.Printf("Discord: Ignoring message as not in specified channel")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-05-17 22:42:33 +00:00
|
|
|
if m.Author.ID == s.State.User.ID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sects := strings.Split(m.Content, " ")
|
|
|
|
if len(sects) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lang := d.app.storage.lang.chosenTelegramLang
|
2021-05-21 20:35:25 +00:00
|
|
|
if user, ok := d.users[m.Author.ID]; ok {
|
|
|
|
if _, ok := d.app.storage.lang.Telegram[user.Lang]; ok {
|
|
|
|
lang = user.Lang
|
|
|
|
}
|
2021-05-17 22:42:33 +00:00
|
|
|
}
|
|
|
|
switch msg := sects[0]; msg {
|
2021-05-21 20:35:25 +00:00
|
|
|
case d.app.config.Section("discord").Key("start_command").MustString("!start"):
|
2021-05-17 22:42:33 +00:00
|
|
|
d.commandStart(s, m, lang)
|
2021-05-18 17:41:42 +00:00
|
|
|
case "!lang":
|
|
|
|
d.commandLang(s, m, sects, lang)
|
2021-05-17 22:42:33 +00:00
|
|
|
default:
|
2021-05-18 17:41:42 +00:00
|
|
|
d.commandPIN(s, m, sects, lang)
|
2021-05-17 22:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) commandStart(s *dg.Session, m *dg.MessageCreate, lang string) {
|
|
|
|
channel, err := s.UserChannelCreate(m.Author.ID)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to create private channel with \"%s\": %v", m.Author.Username, err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
user := d.MustGetUser(channel.ID, m.Author.ID, m.Author.Discriminator, m.Author.Username)
|
|
|
|
d.users[m.Author.ID] = user
|
2021-05-17 22:42:33 +00:00
|
|
|
content := d.app.storage.lang.Telegram[lang].Strings.get("startMessage") + "\n"
|
2021-05-18 17:41:42 +00:00
|
|
|
content += d.app.storage.lang.Telegram[lang].Strings.template("languageMessage", tmpl{"command": "!lang"})
|
2021-05-17 22:42:33 +00:00
|
|
|
_, err = s.ChannelMessageSend(channel.ID, content)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to send message to \"%s\": %v", m.Author.Username, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 17:41:42 +00:00
|
|
|
func (d *DiscordDaemon) commandLang(s *dg.Session, m *dg.MessageCreate, sects []string, lang string) {
|
|
|
|
if len(sects) == 1 {
|
|
|
|
list := "!lang <lang>\n"
|
|
|
|
for code := range d.app.storage.lang.Telegram {
|
|
|
|
list += fmt.Sprintf("%s: %s\n", code, d.app.storage.lang.Telegram[code].Meta.Name)
|
|
|
|
}
|
|
|
|
_, err := s.ChannelMessageSendReply(
|
|
|
|
m.ChannelID,
|
|
|
|
list,
|
|
|
|
m.Reference(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to send message to \"%s\": %v", m.Author.Username, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := d.app.storage.lang.Telegram[sects[1]]; ok {
|
2021-05-21 20:35:25 +00:00
|
|
|
var user DiscordUser
|
2021-05-18 17:41:42 +00:00
|
|
|
for jfID, user := range d.app.storage.discord {
|
|
|
|
if user.ID == m.Author.ID {
|
|
|
|
user.Lang = sects[1]
|
|
|
|
d.app.storage.discord[jfID] = user
|
|
|
|
if err := d.app.storage.storeDiscordUsers(); err != nil {
|
|
|
|
d.app.err.Printf("Failed to store Discord users: %v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
d.users[m.Author.ID] = user
|
2021-05-18 17:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) commandPIN(s *dg.Session, m *dg.MessageCreate, sects []string, lang string) {
|
2021-05-21 20:35:25 +00:00
|
|
|
if _, ok := d.users[m.Author.ID]; ok {
|
|
|
|
channel, err := s.Channel(m.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to get channel: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if channel.Type != dg.ChannelTypeDM {
|
|
|
|
d.app.debug.Println("Discord: Ignoring message as not a DM")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.app.debug.Println("Discord: Ignoring message as user was not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tokenIndex := -1
|
|
|
|
for i, token := range d.tokens {
|
|
|
|
if sects[0] == token {
|
|
|
|
tokenIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tokenIndex == -1 {
|
|
|
|
_, err := s.ChannelMessageSend(
|
2021-05-17 22:42:33 +00:00
|
|
|
m.ChannelID,
|
|
|
|
d.app.storage.lang.Telegram[lang].Strings.get("invalidPIN"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to send message to \"%s\": %v", m.Author.Username, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
_, err := s.ChannelMessageSend(
|
2021-05-17 22:42:33 +00:00
|
|
|
m.ChannelID,
|
|
|
|
d.app.storage.lang.Telegram[lang].Strings.get("pinSuccess"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to send message to \"%s\": %v", m.Author.Username, err)
|
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
d.verifiedTokens[sects[0]] = d.users[m.Author.ID]
|
|
|
|
d.tokens[len(d.tokens)-1], d.tokens[tokenIndex] = d.tokens[tokenIndex], d.tokens[len(d.tokens)-1]
|
|
|
|
d.tokens = d.tokens[:len(d.tokens)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) Send(message *Message, channelID ...string) error {
|
2021-05-21 20:59:50 +00:00
|
|
|
msg := ""
|
|
|
|
var embeds []*dg.MessageEmbed
|
|
|
|
if message.Markdown != "" {
|
|
|
|
var links []Link
|
|
|
|
msg, links = StripAltText(message.Markdown, true)
|
|
|
|
embeds = make([]*dg.MessageEmbed, len(links))
|
|
|
|
for i := range links {
|
|
|
|
embeds[i] = &dg.MessageEmbed{
|
|
|
|
URL: links[i].URL,
|
|
|
|
Title: links[i].Alt,
|
|
|
|
Type: dg.EmbedTypeLink,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg = message.Text
|
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
for _, id := range channelID {
|
2021-05-21 20:59:50 +00:00
|
|
|
var err error
|
|
|
|
if len(embeds) != 0 {
|
|
|
|
_, err = d.bot.ChannelMessageSendComplex(
|
|
|
|
id,
|
|
|
|
&dg.MessageSend{
|
|
|
|
Content: msg,
|
|
|
|
Embed: embeds[0],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := 1; i < len(embeds); i++ {
|
|
|
|
_, err := d.bot.ChannelMessageSendEmbed(id, embeds[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
} else {
|
2021-05-21 20:59:50 +00:00
|
|
|
_, err := d.bot.ChannelMessageSend(
|
|
|
|
id,
|
|
|
|
msg,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-05-17 22:42:33 +00:00
|
|
|
}
|