2021-05-17 22:42:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-08-09 20:40:03 +00:00
|
|
|
"net/http"
|
2023-10-10 12:45:29 +00:00
|
|
|
"strings"
|
2023-06-21 16:59:58 +00:00
|
|
|
"time"
|
2021-05-17 22:42:33 +00:00
|
|
|
|
|
|
|
dg "github.com/bwmarrin/discordgo"
|
2024-08-01 19:15:54 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2023-06-24 16:01:52 +00:00
|
|
|
"github.com/timshannon/badgerhold/v4"
|
2021-05-17 22:42:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DiscordDaemon struct {
|
2024-08-03 20:23:59 +00:00
|
|
|
Stopped bool
|
|
|
|
ShutdownChannel chan string
|
|
|
|
bot *dg.Session
|
|
|
|
username string
|
|
|
|
tokens map[string]VerifToken // Map of pins to tokens.
|
|
|
|
verifiedTokens map[string]DiscordUser // Map of token pins to discord users.
|
|
|
|
Channel, InviteChannel struct{ ID, Name 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.
|
|
|
|
roleID string
|
|
|
|
app *appContext
|
|
|
|
commandHandlers map[string]func(s *dg.Session, i *dg.InteractionCreate, lang string)
|
|
|
|
commandIDs []string
|
|
|
|
commandDescriptions []*dg.ApplicationCommand
|
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,
|
2023-06-21 17:26:08 +00:00
|
|
|
tokens: map[string]VerifToken{},
|
2021-05-21 20:35:25 +00:00
|
|
|
verifiedTokens: map[string]DiscordUser{},
|
|
|
|
users: map[string]DiscordUser{},
|
2021-05-17 22:42:33 +00:00
|
|
|
app: app,
|
2022-01-13 22:34:47 +00:00
|
|
|
roleID: app.config.Section("discord").Key("apply_role").String(),
|
2022-01-26 20:00:40 +00:00
|
|
|
commandHandlers: map[string]func(s *dg.Session, i *dg.InteractionCreate, lang string){},
|
|
|
|
commandIDs: []string{},
|
2021-05-17 22:42:33 +00:00
|
|
|
}
|
2022-01-26 20:00:40 +00:00
|
|
|
dd.commandHandlers[app.config.Section("discord").Key("start_command").MustString("start")] = dd.cmdStart
|
|
|
|
dd.commandHandlers["lang"] = dd.cmdLang
|
|
|
|
dd.commandHandlers["pin"] = dd.cmdPIN
|
2023-09-30 15:25:36 +00:00
|
|
|
dd.commandHandlers["inv"] = dd.cmdInvite
|
2023-06-20 11:19:24 +00:00
|
|
|
for _, user := range app.storage.GetDiscord() {
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-09 20:40:03 +00:00
|
|
|
// SetTransport sets the http.Transport to use for requests. Can be used to set a proxy.
|
|
|
|
func (d *DiscordDaemon) SetTransport(t *http.Transport) {
|
|
|
|
d.bot.Client.Transport = t
|
|
|
|
}
|
|
|
|
|
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()
|
2023-06-21 17:26:08 +00:00
|
|
|
d.tokens[pin] = VerifToken{Expiry: time.Now().Add(VERIF_TOKEN_EXPIRY_SEC * time.Second), JellyfinID: ""}
|
|
|
|
return pin
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAssignedAuthToken generates an 8-character pin in the form "A1-2B-CD",
|
|
|
|
// and assigns it for access only with the given Jellyfin ID.
|
|
|
|
func (d *DiscordDaemon) NewAssignedAuthToken(id string) string {
|
|
|
|
pin := genAuthToken()
|
|
|
|
d.tokens[pin] = VerifToken{Expiry: time.Now().Add(VERIF_TOKEN_EXPIRY_SEC * time.Second), JellyfinID: id}
|
2021-05-21 20:35:25 +00:00
|
|
|
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() {
|
2022-01-26 20:00:40 +00:00
|
|
|
d.bot.AddHandler(d.commandHandler)
|
|
|
|
|
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 {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedStartDaemon, lm.Discord, err)
|
2021-05-17 22:42:33 +00:00
|
|
|
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 {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedGetDiscordGuild, err)
|
2021-05-21 20:35:25 +00:00
|
|
|
}
|
|
|
|
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 != "" {
|
2024-08-03 20:23:59 +00:00
|
|
|
d.Channel.Name = 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 != "" {
|
2024-08-03 20:23:59 +00:00
|
|
|
d.InviteChannel.Name = invChannel
|
2021-05-23 18:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-30 16:10:38 +00:00
|
|
|
err = d.bot.UpdateGameStatus(0, "/"+d.app.config.Section("discord").Key("start_command").MustString("start"))
|
2022-01-26 20:00:40 +00:00
|
|
|
defer d.deregisterCommands()
|
2021-05-17 22:42:33 +00:00
|
|
|
defer d.bot.Close()
|
2022-01-26 20:00:40 +00:00
|
|
|
|
2023-06-11 18:48:03 +00:00
|
|
|
go d.registerCommands()
|
2022-01-26 20:00:40 +00:00
|
|
|
|
2021-05-17 22:42:33 +00:00
|
|
|
<-d.ShutdownChannel
|
|
|
|
d.ShutdownChannel <- "Down"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-13 22:34:47 +00:00
|
|
|
// ListRoles returns a list of available (excluding bot and @everyone) roles in a guild as a list of containing an array of the guild ID and its name.
|
|
|
|
func (d *DiscordDaemon) ListRoles() (roles [][2]string, err error) {
|
|
|
|
var r []*dg.Role
|
|
|
|
r, err = d.bot.GuildRoles(d.guildID)
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedGetDiscordRoles, err)
|
2022-01-13 22:34:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, role := range r {
|
|
|
|
if role.Name != d.username && role.Name != "@everyone" {
|
|
|
|
roles = append(roles, [2]string{role.ID, role.Name})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// roles = make([][2]string, len(r))
|
|
|
|
// for i, role := range r {
|
|
|
|
// roles[i] = [2]string{role.ID, role.Name}
|
|
|
|
// }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyRole applies the member role to the given user if set.
|
|
|
|
func (d *DiscordDaemon) ApplyRole(userID string) error {
|
|
|
|
if d.roleID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return d.bot.GuildMemberRoleAdd(d.guildID, userID, d.roleID)
|
|
|
|
}
|
|
|
|
|
2024-08-04 14:37:01 +00:00
|
|
|
// RemoveRole removes the member role to the given user if set.
|
|
|
|
func (d *DiscordDaemon) RemoveRole(userID string) error {
|
|
|
|
if d.roleID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return d.bot.GuildMemberRoleRemove(d.guildID, userID, d.roleID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRoleDisabled removes the role if "disabled", and applies if "!disabled".
|
|
|
|
func (d *DiscordDaemon) SetRoleDisabled(userID string, disabled bool) (err error) {
|
|
|
|
if disabled {
|
|
|
|
err = d.RemoveRole(userID)
|
|
|
|
} else {
|
|
|
|
err = d.ApplyRole(userID)
|
|
|
|
}
|
|
|
|
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
|
2024-08-03 20:23:59 +00:00
|
|
|
if d.InviteChannel.Name == "" {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Println(lm.FailedCreateDiscordInviteChannel, lm.InviteChannelEmpty)
|
2021-05-23 18:50:03 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
if d.InviteChannel.ID == "" {
|
2021-05-23 18:50:03 +00:00
|
|
|
channels, err := d.bot.GuildChannels(d.guildID)
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedGetDiscordChannels, err)
|
2021-05-23 18:50:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, channel := range channels {
|
|
|
|
// channel, err := d.bot.Channel(ch.ID)
|
|
|
|
// if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
// d.app.err.Printf(lm.FailedGetDiscordChannel, ch.ID, err)
|
2021-05-23 18:50:03 +00:00
|
|
|
// return
|
|
|
|
// }
|
2024-08-03 20:23:59 +00:00
|
|
|
if channel.Name == d.InviteChannel.Name {
|
|
|
|
d.InviteChannel.ID = channel.ID
|
2021-05-23 18:50:03 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2024-08-03 20:23:59 +00:00
|
|
|
d.app.err.Printf(lm.FailedGetDiscordChannel, d.InviteChannel.Name, lm.NotFound)
|
2021-05-23 18:50:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// channel, err := d.bot.Channel(d.inviteChannelID)
|
|
|
|
// if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
// d.app.err.Printf(lm.FailedGetDiscordChannel, d.inviteChannelID, err)
|
2021-05-23 18:50:03 +00:00
|
|
|
// return
|
|
|
|
// }
|
2024-08-03 20:23:59 +00:00
|
|
|
inv, err = d.bot.ChannelInviteCreate(d.InviteChannel.ID, dg.Invite{
|
2021-05-23 18:50:03 +00:00
|
|
|
// 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 {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedGenerateDiscordInvite, err)
|
2021-05-23 18:50:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
inviteURL = "https://discord.gg/" + inv.Code
|
|
|
|
guild, err := d.bot.Guild(d.guildID)
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedGetDiscordGuild, err)
|
2021-05-23 18:50:03 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-20 10:47:54 +00:00
|
|
|
iconURL = guild.IconURL("256")
|
2021-05-23 18:50:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-12 15:32:40 +00:00
|
|
|
// RenderDiscordUsername returns String of discord username, with support for new discriminator-less versions.
|
|
|
|
func RenderDiscordUsername[DcUser *dg.User | DiscordUser](user DcUser) string {
|
|
|
|
u, ok := interface{}(user).(*dg.User)
|
|
|
|
var discriminator, username string
|
|
|
|
if ok {
|
|
|
|
discriminator = u.Discriminator
|
|
|
|
username = u.Username
|
|
|
|
} else {
|
|
|
|
u2 := interface{}(user).(DiscordUser)
|
|
|
|
discriminator = u2.Discriminator
|
|
|
|
username = u2.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
if discriminator == "0" {
|
|
|
|
return "@" + username
|
|
|
|
}
|
|
|
|
return username + "#" + discriminator
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedGetDiscordGuildMembers, err)
|
2021-05-22 20:42:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
hasDiscriminator := strings.Contains(username, "#")
|
2023-06-12 15:32:40 +00:00
|
|
|
hasAt := strings.HasPrefix(username, "@")
|
|
|
|
if hasAt {
|
|
|
|
username = username[1:]
|
|
|
|
}
|
2021-05-22 20:42:15 +00:00
|
|
|
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
|
|
|
}
|
2023-06-12 15:32:40 +00:00
|
|
|
if hasAt {
|
|
|
|
if member.User.Username == username && member.User.Discriminator == "0" {
|
|
|
|
return []*dg.Member{member}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedGetUser, ID, lm.Discord, err)
|
2021-05-22 20:42:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user.ID = ID
|
|
|
|
user.Username = u.Username
|
|
|
|
user.Contact = true
|
|
|
|
user.Discriminator = u.Discriminator
|
|
|
|
channel, err := d.bot.UserChannelCreate(ID)
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedCreateDiscordDMChannel, ID, err)
|
2021-05-22 20:42:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user.ChannelID = channel.ID
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) Shutdown() {
|
|
|
|
d.Stopped = true
|
|
|
|
d.ShutdownChannel <- "Down"
|
|
|
|
<-d.ShutdownChannel
|
|
|
|
close(d.ShutdownChannel)
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:00:40 +00:00
|
|
|
func (d *DiscordDaemon) registerCommands() {
|
2023-10-11 11:00:38 +00:00
|
|
|
d.commandDescriptions = []*dg.ApplicationCommand{
|
2022-01-26 20:00:40 +00:00
|
|
|
{
|
|
|
|
Name: d.app.config.Section("discord").Key("start_command").MustString("start"),
|
|
|
|
Description: "Start the Discord linking process. The bot will send further instructions.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "lang",
|
|
|
|
Description: "Set the language for the bot.",
|
|
|
|
Options: []*dg.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
Type: dg.ApplicationCommandOptionString,
|
|
|
|
Name: "language",
|
|
|
|
Description: "Language Name",
|
|
|
|
Required: true,
|
|
|
|
Choices: []*dg.ApplicationCommandOptionChoice{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pin",
|
|
|
|
Description: "Send PIN for Discord verification.",
|
|
|
|
Options: []*dg.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
Type: dg.ApplicationCommandOptionString,
|
|
|
|
Name: "pin",
|
|
|
|
Description: "Verification PIN (e.g AB-CD-EF)",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-09-28 21:55:47 +00:00
|
|
|
{
|
2023-09-30 15:25:36 +00:00
|
|
|
Name: "inv",
|
2023-09-28 21:55:47 +00:00
|
|
|
Description: "Send an invite to a discord user (admin only).",
|
|
|
|
Options: []*dg.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
Type: dg.ApplicationCommandOptionUser,
|
|
|
|
Name: "user",
|
2023-10-10 14:15:25 +00:00
|
|
|
Description: "User to Invite.",
|
2023-09-28 21:55:47 +00:00
|
|
|
Required: true,
|
2023-10-10 13:52:54 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-30 16:10:38 +00:00
|
|
|
Type: dg.ApplicationCommandOptionInteger,
|
2023-10-10 13:52:54 +00:00
|
|
|
Name: "expiry",
|
2023-10-10 14:15:25 +00:00
|
|
|
Description: "Time in minutes before expiration.",
|
2023-09-30 16:10:38 +00:00
|
|
|
Required: false,
|
2023-09-28 21:55:47 +00:00
|
|
|
},
|
2023-10-10 13:52:54 +00:00
|
|
|
/* Label should be automatically set to something like "Discord invite for @username"
|
2023-09-30 16:10:38 +00:00
|
|
|
{
|
|
|
|
Type: dg.ApplicationCommandOptionString,
|
|
|
|
Name: "label",
|
2023-10-10 13:52:54 +00:00
|
|
|
Description: "Label given to this invite (shown on the Admin page)",
|
|
|
|
Required: false,
|
|
|
|
}, */
|
|
|
|
{
|
|
|
|
Type: dg.ApplicationCommandOptionString,
|
|
|
|
Name: "user_label",
|
2023-10-10 14:15:25 +00:00
|
|
|
Description: "Label given to users created with this invite.",
|
2023-09-30 16:10:38 +00:00
|
|
|
Required: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: dg.ApplicationCommandOptionString,
|
|
|
|
Name: "profile",
|
2023-10-11 11:00:38 +00:00
|
|
|
Description: "Profile to apply to the created user.",
|
2023-09-30 16:10:38 +00:00
|
|
|
Required: false,
|
2023-10-10 13:52:54 +00:00
|
|
|
},
|
2023-09-28 21:55:47 +00:00
|
|
|
},
|
|
|
|
},
|
2022-01-26 20:00:40 +00:00
|
|
|
}
|
2023-10-11 11:00:38 +00:00
|
|
|
d.commandDescriptions[1].Options[0].Choices = make([]*dg.ApplicationCommandOptionChoice, len(d.app.storage.lang.Telegram))
|
2022-01-26 20:00:40 +00:00
|
|
|
i := 0
|
|
|
|
for code := range d.app.storage.lang.Telegram {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.debug.Printf(lm.RegisterDiscordChoice, lm.Lang, d.app.storage.lang.Telegram[code].Meta.Name+":"+code)
|
2023-10-11 11:00:38 +00:00
|
|
|
d.commandDescriptions[1].Options[0].Choices[i] = &dg.ApplicationCommandOptionChoice{
|
2022-01-26 20:00:40 +00:00
|
|
|
Name: d.app.storage.lang.Telegram[code].Meta.Name,
|
|
|
|
Value: code,
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:15:25 +00:00
|
|
|
profiles := d.app.storage.GetProfiles()
|
2023-10-11 11:00:38 +00:00
|
|
|
d.commandDescriptions[3].Options[3].Choices = make([]*dg.ApplicationCommandOptionChoice, len(profiles))
|
2023-10-10 14:15:25 +00:00
|
|
|
for i, profile := range profiles {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.debug.Printf(lm.RegisterDiscordChoice, lm.Profile, profile.Name)
|
2023-10-11 11:00:38 +00:00
|
|
|
d.commandDescriptions[3].Options[3].Choices[i] = &dg.ApplicationCommandOptionChoice{
|
2023-10-10 14:15:25 +00:00
|
|
|
Name: profile.Name,
|
|
|
|
Value: profile.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:14:16 +00:00
|
|
|
// d.deregisterCommands()
|
2022-01-26 20:00:40 +00:00
|
|
|
|
2023-10-11 11:00:38 +00:00
|
|
|
d.commandIDs = make([]string, len(d.commandDescriptions))
|
2022-01-26 22:14:16 +00:00
|
|
|
// cCommands, err := d.bot.ApplicationCommandBulkOverwrite(d.bot.State.User.ID, d.guildID, commands)
|
|
|
|
// if err != nil {
|
|
|
|
// d.app.err.Printf("Discord: Cannot create commands: %v", err)
|
|
|
|
// }
|
2023-10-11 11:00:38 +00:00
|
|
|
for i, cmd := range d.commandDescriptions {
|
2022-01-26 22:14:16 +00:00
|
|
|
command, err := d.bot.ApplicationCommandCreate(d.bot.State.User.ID, d.guildID, cmd)
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedRegisterDiscordCommand, cmd.Name, err)
|
2022-01-26 22:14:16 +00:00
|
|
|
} else {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.debug.Printf(lm.RegisterDiscordCommand, cmd.Name)
|
2022-01-26 22:14:16 +00:00
|
|
|
d.commandIDs[i] = command.ID
|
|
|
|
}
|
2022-01-26 20:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) deregisterCommands() {
|
|
|
|
existingCommands, err := d.bot.ApplicationCommands(d.bot.State.User.ID, d.guildID)
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedGetDiscordCommands, err)
|
2022-01-26 20:00:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, cmd := range existingCommands {
|
2023-10-10 14:15:25 +00:00
|
|
|
if err := d.bot.ApplicationCommandDelete(d.bot.State.User.ID, d.guildID, cmd.ID); err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedDeregDiscordCommand, cmd.Name, err)
|
2022-01-26 20:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 11:00:38 +00:00
|
|
|
// UpdateCommands updates commands which have defined lists of options, to be used when changes occur.
|
|
|
|
func (d *DiscordDaemon) UpdateCommands() {
|
|
|
|
// Reload Profile List
|
|
|
|
profiles := d.app.storage.GetProfiles()
|
|
|
|
d.commandDescriptions[3].Options[3].Choices = make([]*dg.ApplicationCommandOptionChoice, len(profiles))
|
|
|
|
for i, profile := range profiles {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.debug.Printf(lm.RegisterDiscordChoice, lm.Profile, profile.Name)
|
2023-10-11 11:00:38 +00:00
|
|
|
d.commandDescriptions[3].Options[3].Choices[i] = &dg.ApplicationCommandOptionChoice{
|
|
|
|
Name: profile.Name,
|
|
|
|
Value: profile.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd, err := d.bot.ApplicationCommandEdit(d.bot.State.User.ID, d.guildID, d.commandIDs[3], d.commandDescriptions[3])
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedRegisterDiscordChoices, lm.Profile, err)
|
2023-10-11 11:00:38 +00:00
|
|
|
} else {
|
|
|
|
d.commandIDs[3] = cmd.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:00:40 +00:00
|
|
|
func (d *DiscordDaemon) commandHandler(s *dg.Session, i *dg.InteractionCreate) {
|
|
|
|
if h, ok := d.commandHandlers[i.ApplicationCommandData().Name]; ok {
|
2024-08-03 20:23:59 +00:00
|
|
|
if i.GuildID != "" && d.Channel.Name != "" {
|
|
|
|
if d.Channel.ID == "" {
|
2022-01-26 20:00:40 +00:00
|
|
|
channel, err := s.Channel(i.ChannelID)
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedGetDiscordChannel, i.ChannelID, err)
|
|
|
|
d.app.err.Println(lm.MonitorAllDiscordChannels)
|
2024-08-03 20:23:59 +00:00
|
|
|
d.Channel.Name = ""
|
2022-01-26 20:00:40 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
if channel.Name == d.Channel.Name {
|
|
|
|
d.Channel.ID = channel.ID
|
2022-01-26 20:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
if d.Channel.ID != i.ChannelID {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.debug.Printf(lm.IgnoreOutOfChannelMessage, lm.Discord)
|
2022-01-26 20:00:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i.Interaction.Member.User.ID == s.State.User.ID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lang := d.app.storage.lang.chosenTelegramLang
|
|
|
|
if user, ok := d.users[i.Interaction.Member.User.ID]; ok {
|
|
|
|
if _, ok := d.app.storage.lang.Telegram[user.Lang]; ok {
|
|
|
|
lang = user.Lang
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h(s, i, lang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmd* methods handle slash-commands, msg* methods handle ! commands.
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) cmdStart(s *dg.Session, i *dg.InteractionCreate, lang string) {
|
|
|
|
channel, err := s.UserChannelCreate(i.Interaction.Member.User.ID)
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedCreateDiscordDMChannel, i.Interaction.Member.User.ID, err)
|
2022-01-26 20:00:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user := d.MustGetUser(channel.ID, i.Interaction.Member.User.ID, i.Interaction.Member.User.Discriminator, i.Interaction.Member.User.Username)
|
|
|
|
d.users[i.Interaction.Member.User.ID] = user
|
|
|
|
|
|
|
|
content := d.app.storage.lang.Telegram[lang].Strings.get("discordStartMessage") + "\n"
|
|
|
|
content += d.app.storage.lang.Telegram[lang].Strings.template("languageMessageDiscord", tmpl{"command": "/lang"})
|
|
|
|
err = s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
|
|
|
|
// Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &dg.InteractionResponseData{
|
|
|
|
Content: content,
|
|
|
|
Flags: 64, // Ephemeral
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedReply, lm.Discord, i.Interaction.Member.User.ID, err)
|
2022-01-26 20:00:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) cmdPIN(s *dg.Session, i *dg.InteractionCreate, lang string) {
|
|
|
|
pin := i.ApplicationCommandData().Options[0].StringValue()
|
2023-06-21 17:26:08 +00:00
|
|
|
user, ok := d.tokens[pin]
|
|
|
|
if !ok || time.Now().After(user.Expiry) {
|
2022-01-26 20:00:40 +00:00
|
|
|
err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
|
|
|
|
// Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &dg.InteractionResponseData{
|
|
|
|
Content: d.app.storage.lang.Telegram[lang].Strings.get("invalidPIN"),
|
|
|
|
Flags: 64, // Ephemeral
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedReply, lm.Discord, i.Interaction.Member.User.ID, err)
|
2022-01-26 20:00:40 +00:00
|
|
|
}
|
2023-06-21 16:59:58 +00:00
|
|
|
delete(d.tokens, pin)
|
2022-01-26 20:00:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
|
|
|
|
// Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &dg.InteractionResponseData{
|
|
|
|
Content: d.app.storage.lang.Telegram[lang].Strings.get("pinSuccess"),
|
|
|
|
Flags: 64, // Ephemeral
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedReply, lm.Discord, i.Interaction.Member.User.ID, err)
|
2022-01-26 20:00:40 +00:00
|
|
|
}
|
2023-06-21 17:26:08 +00:00
|
|
|
dcUser := d.users[i.Interaction.Member.User.ID]
|
|
|
|
dcUser.JellyfinID = user.JellyfinID
|
|
|
|
d.verifiedTokens[pin] = dcUser
|
2023-06-21 16:59:58 +00:00
|
|
|
delete(d.tokens, pin)
|
2022-01-26 20:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) cmdLang(s *dg.Session, i *dg.InteractionCreate, lang string) {
|
|
|
|
code := i.ApplicationCommandData().Options[0].StringValue()
|
|
|
|
if _, ok := d.app.storage.lang.Telegram[code]; ok {
|
|
|
|
var user DiscordUser
|
2023-06-24 16:01:52 +00:00
|
|
|
for _, u := range d.app.storage.GetDiscord() {
|
2022-01-26 20:00:40 +00:00
|
|
|
if u.ID == i.Interaction.Member.User.ID {
|
|
|
|
u.Lang = code
|
|
|
|
lang = code
|
2023-06-24 16:01:52 +00:00
|
|
|
d.app.storage.SetDiscordKey(u.JellyfinID, u)
|
2022-01-26 20:00:40 +00:00
|
|
|
user = u
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.users[i.Interaction.Member.User.ID] = user
|
|
|
|
err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
|
|
|
|
// Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &dg.InteractionResponseData{
|
|
|
|
Content: d.app.storage.lang.Telegram[lang].Strings.template("languageSet", tmpl{"language": d.app.storage.lang.Telegram[lang].Meta.Name}),
|
|
|
|
Flags: 64, // Ephemeral
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedReply, lm.Discord, i.Interaction.Member.User.ID, err)
|
2022-01-26 20:00:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 15:25:36 +00:00
|
|
|
func (d *DiscordDaemon) cmdInvite(s *dg.Session, i *dg.InteractionCreate, lang string) {
|
|
|
|
channel, err := s.UserChannelCreate(i.Interaction.Member.User.ID)
|
2023-10-11 10:38:55 +00:00
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedCreateDiscordDMChannel, i.Interaction.Member.User.ID, err)
|
2023-10-11 10:38:55 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-10 12:45:29 +00:00
|
|
|
requester := d.MustGetUser(channel.ID, i.Interaction.Member.User.ID, i.Interaction.Member.User.Discriminator, i.Interaction.Member.User.Username)
|
|
|
|
d.users[i.Interaction.Member.User.ID] = requester
|
|
|
|
recipient := i.ApplicationCommandData().Options[0].UserValue(s)
|
|
|
|
// d.app.debug.Println(invuser)
|
2023-09-30 16:10:38 +00:00
|
|
|
//label := i.ApplicationCommandData().Options[2].StringValue()
|
|
|
|
//profile := i.ApplicationCommandData().Options[3].StringValue()
|
|
|
|
//mins, err := strconv.Atoi(i.ApplicationCommandData().Options[1].StringValue())
|
|
|
|
//if mins > 0 {
|
|
|
|
// expmin = mins
|
|
|
|
//}
|
2023-10-10 15:14:57 +00:00
|
|
|
// Check whether requestor is linked to the admin account
|
2023-10-10 12:45:29 +00:00
|
|
|
requesterEmail, ok := d.app.storage.GetEmailsKey(requester.JellyfinID)
|
2024-08-01 19:15:54 +00:00
|
|
|
if !(ok && requesterEmail.Admin) {
|
|
|
|
d.app.err.Printf(lm.FailedGenerateInvite, fmt.Sprintf(lm.NonAdminUser, requester.JellyfinID))
|
|
|
|
// FIXME: add response message
|
2023-10-10 15:14:57 +00:00
|
|
|
return
|
2023-10-04 03:22:20 +00:00
|
|
|
}
|
2023-10-10 13:52:54 +00:00
|
|
|
|
|
|
|
var expiryMinutes int64 = 30
|
|
|
|
userLabel := ""
|
|
|
|
profileName := ""
|
|
|
|
|
|
|
|
for i, opt := range i.ApplicationCommandData().Options {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch opt.Name {
|
|
|
|
case "expiry":
|
|
|
|
expiryMinutes = opt.IntValue()
|
|
|
|
case "user_label":
|
|
|
|
userLabel = opt.StringValue()
|
|
|
|
case "profile":
|
|
|
|
profileName = opt.StringValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:37:35 +00:00
|
|
|
currentTime := time.Now()
|
2023-10-10 13:52:54 +00:00
|
|
|
|
|
|
|
validTill := currentTime.Add(time.Minute * time.Duration(expiryMinutes))
|
|
|
|
|
|
|
|
invite := Invite{
|
2023-10-11 10:35:08 +00:00
|
|
|
Code: GenerateInviteCode(),
|
2023-10-10 13:52:54 +00:00
|
|
|
Created: currentTime,
|
|
|
|
RemainingUses: 1,
|
|
|
|
UserExpiry: false,
|
|
|
|
ValidTill: validTill,
|
|
|
|
UserLabel: userLabel,
|
|
|
|
Profile: "Default",
|
2024-08-01 19:15:54 +00:00
|
|
|
Label: fmt.Sprintf("%s: %s", lm.Discord, RenderDiscordUsername(recipient)),
|
2023-10-10 13:52:54 +00:00
|
|
|
}
|
|
|
|
if profileName != "" {
|
|
|
|
if _, ok := d.app.storage.GetProfileKey(profileName); ok {
|
|
|
|
invite.Profile = profileName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 12:45:29 +00:00
|
|
|
if recipient != nil && d.app.config.Section("invite_emails").Key("enabled").MustBool(false) {
|
|
|
|
invname, err := d.bot.GuildMember(d.guildID, recipient.ID)
|
2023-09-30 15:25:36 +00:00
|
|
|
invite.SendTo = invname.User.Username
|
2023-10-11 10:35:08 +00:00
|
|
|
msg, err := d.app.email.constructInvite(invite.Code, invite, d.app, false)
|
2023-09-30 15:25:36 +00:00
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
invite.SendTo = fmt.Sprintf(lm.FailedConstructInviteMessage, invite.Code, err)
|
|
|
|
d.app.err.Println(invite.SendTo)
|
2023-10-11 10:38:55 +00:00
|
|
|
err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
|
|
|
|
Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &dg.InteractionResponseData{
|
|
|
|
Content: d.app.storage.lang.Telegram[lang].Strings.get("sentInviteFailure"),
|
|
|
|
Flags: 64, // Ephemeral
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedReply, lm.Discord, requester.ID, err)
|
2023-10-11 10:38:55 +00:00
|
|
|
}
|
2023-09-30 15:25:36 +00:00
|
|
|
} else {
|
|
|
|
var err error
|
2023-10-10 12:45:29 +00:00
|
|
|
err = d.app.discord.SendDM(msg, recipient.ID)
|
2023-09-28 23:37:35 +00:00
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
invite.SendTo = fmt.Sprintf(lm.FailedSendInviteMessage, invite.Code, RenderDiscordUsername(recipient), err)
|
|
|
|
d.app.err.Println(invite.SendTo)
|
2023-10-10 12:45:29 +00:00
|
|
|
err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
|
|
|
|
Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &dg.InteractionResponseData{
|
|
|
|
Content: d.app.storage.lang.Telegram[lang].Strings.get("sentInviteFailure"),
|
|
|
|
Flags: 64, // Ephemeral
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedReply, lm.Discord, requester.ID, err)
|
2023-10-10 12:45:29 +00:00
|
|
|
}
|
2023-09-28 23:37:35 +00:00
|
|
|
} else {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.info.Printf(lm.SentInviteMessage, invite.Code, RenderDiscordUsername(recipient))
|
2023-10-10 12:45:29 +00:00
|
|
|
err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
|
|
|
|
Type: dg.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &dg.InteractionResponseData{
|
|
|
|
Content: d.app.storage.lang.Telegram[lang].Strings.get("sentInvite"),
|
|
|
|
Flags: 64, // Ephemeral
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-01 19:15:54 +00:00
|
|
|
d.app.err.Printf(lm.FailedReply, lm.Discord, requester.ID, err)
|
2023-10-10 12:45:29 +00:00
|
|
|
}
|
2023-09-28 23:37:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 15:25:36 +00:00
|
|
|
//if profile != "" {
|
2023-10-11 10:38:55 +00:00
|
|
|
d.app.storage.SetInvitesKey(invite.Code, invite)
|
2023-09-28 21:55:47 +00:00
|
|
|
}
|
2023-10-10 12:45:29 +00:00
|
|
|
|
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
|
|
|
}
|
2023-06-21 16:59:58 +00:00
|
|
|
|
|
|
|
// UserVerified returns whether or not a token with the given PIN has been verified, and the user itself.
|
2024-08-03 20:23:59 +00:00
|
|
|
func (d *DiscordDaemon) UserVerified(pin string) (ContactMethodUser, bool) {
|
|
|
|
u, ok := d.verifiedTokens[pin]
|
2023-06-21 16:59:58 +00:00
|
|
|
// delete(d.verifiedTokens, pin)
|
2024-08-03 20:23:59 +00:00
|
|
|
return &u, ok
|
2023-06-21 16:59:58 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 17:26:08 +00:00
|
|
|
// AssignedUserVerified returns whether or not a user with the given PIN has been verified, and the token itself.
|
|
|
|
// Returns false if the given Jellyfin ID does not match the one in the user.
|
|
|
|
func (d *DiscordDaemon) AssignedUserVerified(pin string, jfID string) (user DiscordUser, ok bool) {
|
|
|
|
user, ok = d.verifiedTokens[pin]
|
|
|
|
if ok && user.JellyfinID != jfID {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
// delete(d.verifiedUsers, pin)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-21 16:59:58 +00:00
|
|
|
// UserExists returns whether or not a user with the given ID exists.
|
2023-06-24 16:01:52 +00:00
|
|
|
func (d *DiscordDaemon) UserExists(id string) bool {
|
|
|
|
c, err := d.app.storage.db.Count(&DiscordUser{}, badgerhold.Where("ID").Eq(id))
|
|
|
|
return err != nil || c > 0
|
2023-06-21 16:59:58 +00:00
|
|
|
}
|
|
|
|
|
2024-08-03 20:23:59 +00:00
|
|
|
// Exists returns whether or not the given user exists.
|
|
|
|
func (d *DiscordDaemon) Exists(user ContactMethodUser) bool {
|
|
|
|
return d.UserExists(user.MethodID().(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVerifiedToken removes the token with the given PIN.
|
|
|
|
func (d *DiscordDaemon) DeleteVerifiedToken(PIN string) {
|
|
|
|
delete(d.verifiedTokens, PIN)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) PIN(req newUserDTO) string { return req.DiscordPIN }
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) Name() string { return lm.Discord }
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) Required() bool {
|
|
|
|
return d.app.config.Section("discord").Key("required").MustBool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) UniqueRequired() bool {
|
|
|
|
return d.app.config.Section("discord").Key("require_unique").MustBool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordDaemon) PostVerificationTasks(PIN string, u ContactMethodUser) error {
|
|
|
|
err := d.ApplyRole(u.MethodID().(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(lm.FailedSetDiscordMemberRole, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiscordUser) Name() string { return RenderDiscordUsername(*d) }
|
|
|
|
func (d *DiscordUser) SetMethodID(id any) { d.ID = id.(string) }
|
|
|
|
func (d *DiscordUser) MethodID() any { return d.ID }
|
|
|
|
func (d *DiscordUser) SetJellyfin(id string) { d.JellyfinID = id }
|
|
|
|
func (d *DiscordUser) Jellyfin() string { return d.JellyfinID }
|
|
|
|
func (d *DiscordUser) SetAllowContactFromDTO(req newUserDTO) { d.Contact = req.DiscordContact }
|
|
|
|
func (d *DiscordUser) SetAllowContact(contact bool) { d.Contact = contact }
|
|
|
|
func (d *DiscordUser) AllowContact() bool { return d.Contact }
|
|
|
|
func (d *DiscordUser) Store(st *Storage) {
|
|
|
|
st.SetDiscordKey(d.Jellyfin(), *d)
|
2023-06-21 16:59:58 +00:00
|
|
|
}
|