2021-05-17 22:42:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
dg "github.com/bwmarrin/discordgo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DiscordDaemon struct {
|
2021-05-23 18:50:03 +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, inviteChannelID, inviteChannelName string
|
|
|
|
guildID string
|
|
|
|
serverChannelName, serverName 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
|
|
|
}
|
2021-05-22 14:32:51 +00:00
|
|
|
|
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)
|
2021-05-23 18:50:03 +00:00
|
|
|
d.bot.Identify.Intents = dg.IntentsGuildMessages | dg.IntentsDirectMessages | dg.IntentsGuildMembers | dg.IntentsGuildInvites
|
2021-05-17 22:42:33 +00:00
|
|
|
if err := d.bot.Open(); err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to start daemon: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-23 21:26:56 +00:00
|
|
|
// Wait for everything to populate, it's slow sometimes.
|
2021-05-23 00:09:03 +00:00
|
|
|
for d.bot.State == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-23 21:26:56 +00:00
|
|
|
for d.bot.State.User == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-17 22:42:33 +00:00
|
|
|
d.username = d.bot.State.User.Username
|
2021-05-23 21:26:56 +00:00
|
|
|
for d.bot.State.Guilds == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
// Choose the last guild (server), for now we don't really support multiple anyway
|
2021-05-23 18:50:03 +00:00
|
|
|
d.guildID = d.bot.State.Guilds[len(d.bot.State.Guilds)-1].ID
|
|
|
|
guild, err := d.bot.Guild(d.guildID)
|
2021-05-21 20:35:25 +00:00
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to get guild: %v", err)
|
|
|
|
}
|
|
|
|
d.serverChannelName = guild.Name
|
2021-05-23 18:50:03 +00:00
|
|
|
d.serverName = guild.Name
|
2021-05-21 20:35:25 +00:00
|
|
|
if channel := d.app.config.Section("discord").Key("channel").String(); channel != "" {
|
|
|
|
d.channelName = channel
|
2021-05-23 16:31:20 +00:00
|
|
|
d.serverChannelName += "/" + channel
|
2021-05-21 20:35:25 +00:00
|
|
|
}
|
2021-05-23 18:50:03 +00:00
|
|
|
if d.app.config.Section("discord").Key("provide_invite").MustBool(false) {
|
|
|
|
if invChannel := d.app.config.Section("discord").Key("invite_channel").String(); invChannel != "" {
|
|
|
|
d.inviteChannelName = invChannel
|
|
|
|
}
|
|
|
|
}
|
2021-05-17 22:42:33 +00:00
|
|
|
defer d.bot.Close()
|
|
|
|
<-d.ShutdownChannel
|
|
|
|
d.ShutdownChannel <- "Down"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-23 18:50:03 +00:00
|
|
|
// NewTempInvite creates an invite link, and returns the invite URL, as well as the URL for the server icon.
|
|
|
|
func (d *DiscordDaemon) NewTempInvite(ageSeconds, maxUses int) (inviteURL, iconURL string) {
|
|
|
|
var inv *dg.Invite
|
|
|
|
var err error
|
|
|
|
if d.inviteChannelName == "" {
|
|
|
|
d.app.err.Println("Discord: Cannot create invite without channel specified in settings.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if d.inviteChannelID == "" {
|
|
|
|
channels, err := d.bot.GuildChannels(d.guildID)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Couldn't get channel list: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, channel := range channels {
|
|
|
|
// channel, err := d.bot.Channel(ch.ID)
|
|
|
|
// if err != nil {
|
|
|
|
// d.app.err.Printf("Discord: Couldn't get channel: %v", err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
if channel.Name == d.inviteChannelName {
|
|
|
|
d.inviteChannelID = channel.ID
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
d.app.err.Printf("Discord: Couldn't find invite channel \"%s\"", d.inviteChannelName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// channel, err := d.bot.Channel(d.inviteChannelID)
|
|
|
|
// if err != nil {
|
|
|
|
// d.app.err.Printf("Discord: Couldn't get invite channel: %v", err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
inv, err = d.bot.ChannelInviteCreate(d.inviteChannelID, dg.Invite{
|
|
|
|
// Guild: d.bot.State.Guilds[len(d.bot.State.Guilds)-1],
|
|
|
|
// Channel: channel,
|
|
|
|
// Inviter: d.bot.State.User,
|
|
|
|
MaxAge: ageSeconds,
|
|
|
|
MaxUses: maxUses,
|
|
|
|
Temporary: false,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to create invite: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inviteURL = "https://discord.gg/" + inv.Code
|
|
|
|
guild, err := d.bot.Guild(d.guildID)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to get guild: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
iconURL = guild.IconURL()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-22 20:42:15 +00:00
|
|
|
// Returns the user(s) roughly corresponding to the username (if they are in the guild).
|
|
|
|
// if no discriminator (#xxxx) is given in the username and there are multiple corresponding users, a list of all matching users is returned.
|
|
|
|
func (d *DiscordDaemon) GetUsers(username string) []*dg.Member {
|
|
|
|
members, err := d.bot.GuildMembers(
|
2021-05-23 18:50:03 +00:00
|
|
|
d.guildID,
|
2021-05-22 20:42:15 +00:00
|
|
|
"",
|
|
|
|
1000,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to get members: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
hasDiscriminator := strings.Contains(username, "#")
|
|
|
|
var users []*dg.Member
|
|
|
|
for _, member := range members {
|
2021-05-23 15:16:31 +00:00
|
|
|
if hasDiscriminator {
|
|
|
|
if member.User.Username+"#"+member.User.Discriminator == username {
|
|
|
|
return []*dg.Member{member}
|
2021-05-22 20:42:15 +00:00
|
|
|
}
|
2021-05-23 15:16:31 +00:00
|
|
|
}
|
|
|
|
if strings.Contains(member.User.Username, username) {
|
|
|
|
users = append(users, member)
|
2021-05-22 20:42:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) NewUser(ID string) (user DiscordUser, ok bool) {
|
|
|
|
u, err := d.bot.User(ID)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to get user: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.ID = ID
|
|
|
|
user.Username = u.Username
|
|
|
|
user.Contact = true
|
|
|
|
user.Discriminator = u.Discriminator
|
|
|
|
channel, err := d.bot.UserChannelCreate(ID)
|
|
|
|
if err != nil {
|
|
|
|
d.app.err.Printf("Discord: Failed to create DM channel: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.ChannelID = channel.ID
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) Shutdown() {
|
|
|
|
d.Stopped = true
|
|
|
|
d.ShutdownChannel <- "Down"
|
|
|
|
<-d.ShutdownChannel
|
|
|
|
close(d.ShutdownChannel)
|
|
|
|
}
|
|
|
|
|
2021-05-17 22:42:33 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2021-05-23 15:16:31 +00:00
|
|
|
func (d *DiscordDaemon) SendDM(message *Message, userID ...string) error {
|
|
|
|
channels := make([]string, len(userID))
|
|
|
|
for i, id := range userID {
|
|
|
|
channel, err := d.bot.UserChannelCreate(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
channels[i] = channel.ID
|
|
|
|
}
|
|
|
|
return d.Send(message, channels...)
|
|
|
|
}
|
|
|
|
|
2021-05-21 20:35:25 +00:00
|
|
|
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 != "" {
|
2021-05-22 14:32:51 +00:00
|
|
|
msg, embeds = StripAltText(message.Markdown, true)
|
2021-05-21 20:59:50 +00:00
|
|
|
} 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
|
|
|
}
|