1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 17:10:10 +00:00

Update discord.go

This commit is contained in:
Violet Scheen 2023-09-30 11:25:36 -04:00 committed by GitHub
parent 3538935d3b
commit c43d5cf1b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,9 +3,11 @@ package main
import ( import (
"fmt" "fmt"
"strings" "strings"
"strconv"
"time" "time"
dg "github.com/bwmarrin/discordgo" dg "github.com/bwmarrin/discordgo"
"github.com/lithammer/shortuuid/v3"
"github.com/timshannon/badgerhold/v4" "github.com/timshannon/badgerhold/v4"
) )
@ -50,7 +52,7 @@ func newDiscordDaemon(app *appContext) (*DiscordDaemon, error) {
dd.commandHandlers[app.config.Section("discord").Key("start_command").MustString("start")] = dd.cmdStart dd.commandHandlers[app.config.Section("discord").Key("start_command").MustString("start")] = dd.cmdStart
dd.commandHandlers["lang"] = dd.cmdLang dd.commandHandlers["lang"] = dd.cmdLang
dd.commandHandlers["pin"] = dd.cmdPIN dd.commandHandlers["pin"] = dd.cmdPIN
dd.commandHandlers["invite"] = dd.cmdInvite dd.commandHandlers["inv"] = dd.cmdInvite
for _, user := range app.storage.GetDiscord() { for _, user := range app.storage.GetDiscord() {
dd.users[user.ID] = user dd.users[user.ID] = user
} }
@ -128,8 +130,8 @@ func (d *DiscordDaemon) run() {
d.inviteChannelName = invChannel d.inviteChannelName = invChannel
} }
} }
d.bot.Activity.Name = "/" + app.config.Section("discord").Key("start_command").MustString("start") //dg.Activity.Name = "/" + d.app.config.Section("discord").Key("start_command").MustString("start")
d.bot.Activity.Type = dg.ActivityTypeGame //dg.Activity.Type = dg.ActivityTypeGame
defer d.deregisterCommands() defer d.deregisterCommands()
defer d.bot.Close() defer d.bot.Close()
@ -342,7 +344,7 @@ func (d *DiscordDaemon) registerCommands() {
}, },
}, },
{ {
Name: "invite", Name: "inv",
Description: "Send an invite to a discord user (admin only).", Description: "Send an invite to a discord user (admin only).",
Options: []*dg.ApplicationCommandOption{ Options: []*dg.ApplicationCommandOption{
{ {
@ -351,24 +353,24 @@ func (d *DiscordDaemon) registerCommands() {
Description: "User to Invite", Description: "User to Invite",
Required: true, Required: true,
}, },
{ //{
Type: dg.ApplicationCommandOptionInteger, // Type: dg.ApplicationCommandOptionInteger,
Name: "expire after", // Name: "expire after",
Description: "Time in minutes before expiration", // Description: "Time in minutes before expiration",
Required: false, // Required: false,
}, //},
{ //{
Type: dg.ApplicationCommandOptionString, // Type: dg.ApplicationCommandOptionString,
Name: "label", // Name: "label",
Description: "Label to apply to the user created with this invite", // Description: "Label to apply to the user created with this invite",
Required: false, // Required: false,
}, //},
{ //{
Type: dg.ApplicationCommandOptionString, // Type: dg.ApplicationCommandOptionString,
Name: "Profile", // Name: "profile",
Description: "Profile to apply to the user created with this invite", // Description: "Profile to apply to the user created with this invite",
Required: false, // Required: false,
}, //},
}, },
}, },
} }
@ -536,33 +538,35 @@ func (d *DiscordDaemon) cmdLang(s *dg.Session, i *dg.InteractionCreate, lang str
} }
} }
func (d *DiscordDaemon) cmdInvite(app *appContext, s *dg.Session, i *dg.InteractionCreate, lang string) { func (d *DiscordDaemon) cmdInvite(s *dg.Session, i *dg.InteractionCreate, lang string) {
channel, err := s.UserChannelCreate(i.Interaction.Member.User.ID)
requestor := d.MustGetUser(channel.ID, i.Interaction.Member.User.ID, i.Interaction.Member.User.Discriminator, i.Interaction.Member.User.Username) requestor := 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] = requestor d.users[i.Interaction.Member.User.ID] = requestor
invuser := i.ApplicationCommandData().Options[0].StringValue() invuser := fmt.Sprintf("%v", i.ApplicationCommandData().Options[0].Value)
label := i.ApplicationCommandData().Options[2].StringValue() d.app.debug.Println(invuser)
profile := i.ApplicationCommandData().Options[3].StringValue() // label := i.ApplicationCommandData().Options[2].StringValue()
if i.ApplicationCommandData().Options[1] != "" { // profile := i.ApplicationCommandData().Options[3].StringValue()
expmin := i.ApplicationCommandData().Options[1] // mins, err := strconv.Atoi(i.ApplicationCommandData().Options[1].StringValue())
} else { expmin := 30
expmin := 30 // if mins > 0 {
} // expmin = mins
// Check whether requestor is linked to the admin account // }
// Check whether requestor is linked to the admin account*
// variation of app.GenerateInvite, some parts commented to potentially add back in later // variation of app.GenerateInvite, some parts commented to potentially add back in later
app.debug.Println("Generating new invite with options: %s: %i: %s: %s", invuser, expmin, profile, label) // d.app.debug.Println("Generating new invite with options: %s: %i: %s: %s", invuser, expmin, profile, label)
currentTime := time.Now() currentTime := time.Now()
validTill = currentTime.Add(time.Minute*time.Duration(expmin)) validTill := currentTime.Add(time.Minute*time.Duration(expmin))
// make sure code doesn't begin with number // make sure code doesn't begin with number
inviteCode := shortuuid.New() inviteCode := shortuuid.New()
_, err := strconv.Atoi(string(inviteCode[0])) _, err = strconv.Atoi(string(inviteCode[0]))
for err == nil { for err == nil {
inviteCode = shortuuid.New() inviteCode = shortuuid.New()
_, err = strconv.Atoi(string(inviteCode[0])) _, err = strconv.Atoi(string(inviteCode[0]))
} }
var invite Invite var invite Invite
if label != "" { //if label != "" {
invite.Label = label // invite.Label = label
} //}
invite.Created = currentTime invite.Created = currentTime
invite.RemainingUses = 1 invite.RemainingUses = 1
invite.UserExpiry = false invite.UserExpiry = false
@ -573,48 +577,35 @@ func (d *DiscordDaemon) cmdInvite(app *appContext, s *dg.Session, i *dg.Interact
invite.UserMinutes = req.UserMinutes invite.UserMinutes = req.UserMinutes
}*/ }*/
invite.ValidTill = validTill invite.ValidTill = validTill
if invuser != "" && app.config.Section("invite_emails").Key("enabled").MustBool(false) { if invuser != "" && d.app.config.Section("invite_emails").Key("enabled").MustBool(false) {
addressValid := false
discord := "" discord := ""
app.debug.Printf("%s: Sending invite message", inviteCode) d.app.debug.Printf("%s: Sending invite message", inviteCode)
if discordEnabled && !strings.Contains(invuser, "@") { invname, err := d.bot.GuildMember(d.guildID, invuser)
users := app.discord.GetUsers(invuser) invite.SendTo = invname.User.Username
if len(users) == 0 {
invite.SendTo = fmt.Sprintf("Failed: User not found: \"%s\"", invuser) msg, err := d.app.email.constructInvite(inviteCode, invite, d.app, false)
} else if len(users) > 1 { if err != nil {
invite.SendTo = fmt.Sprintf("Failed: Multiple users found: \"%s\"", invuser) invite.SendTo = fmt.Sprintf("Failed to send to %s", invuser)
} else { d.app.err.Printf("%s: Failed to construct invite message: %v", inviteCode, err)
invite.SendTo = invuser } else {
addressValid = true var err error
discord = users[0].User.ID err = d.app.discord.SendDM(msg, discord)
}
}
if addressValid {
msg, err := app.email.constructInvite(inviteCode, invite, app, false)
if err != nil { if err != nil {
invite.SendTo = fmt.Sprintf("Failed to send to %s", invuser) invite.SendTo = fmt.Sprintf("Failed to send to %s", invuser)
app.err.Printf("%s: Failed to construct invite message: %v", inviteCode, err) d.app.err.Printf("%s: %s: %v", inviteCode, invite.SendTo, err)
} else { } else {
var err error d.app.info.Printf("%s: Sent invite email to \"%s\"", inviteCode, invuser)
err = app.discord.SendDM(msg, discord)
if err != nil {
invite.SendTo = fmt.Sprintf("Failed to send to %s", invuser)
app.err.Printf("%s: %s: %v", inviteCode, invite.SendTo, err)
} else {
app.info.Printf("%s: Sent invite email to \"%s\"", inviteCode, invuser)
}
} }
} }
} }
if profile != "" { //if profile != "" {
if _, ok := app.storage.GetProfileKey(profile); ok { // if _, ok := d.app.storage.GetProfileKey(profile); ok {
invite.Profile = profile // invite.Profile = profile
} else { // } else {
invite.Profile = "Default" // invite.Profile = "Default"
} // }
} //}
app.storage.SetInvitesKey(inviteCode, invite) d.app.storage.SetInvitesKey(inviteCode, invite)
respondBool(200, true, gc)
} }
func (d *DiscordDaemon) messageHandler(s *dg.Session, m *dg.MessageCreate) { func (d *DiscordDaemon) messageHandler(s *dg.Session, m *dg.MessageCreate) {