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

discord: Use GenerateInviteCode in /inv

This commit is contained in:
Harvey Tindall 2023-10-11 11:35:08 +01:00
parent e528f7c348
commit 7813c8c68b
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2

View File

@ -2,12 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
"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"
) )
@ -599,16 +597,9 @@ func (d *DiscordDaemon) cmdInvite(s *dg.Session, i *dg.InteractionCreate, lang s
currentTime := time.Now() currentTime := time.Now()
validTill := currentTime.Add(time.Minute * time.Duration(expiryMinutes)) validTill := currentTime.Add(time.Minute * time.Duration(expiryMinutes))
// make sure code doesn't begin with number
inviteCode := shortuuid.New()
_, err = strconv.Atoi(string(inviteCode[0]))
for err == nil {
inviteCode = shortuuid.New()
_, err = strconv.Atoi(string(inviteCode[0]))
}
invite := Invite{ invite := Invite{
Code: inviteCode, Code: GenerateInviteCode(),
Created: currentTime, Created: currentTime,
RemainingUses: 1, RemainingUses: 1,
UserExpiry: false, UserExpiry: false,
@ -624,20 +615,20 @@ func (d *DiscordDaemon) cmdInvite(s *dg.Session, i *dg.InteractionCreate, lang s
} }
if recipient != nil && d.app.config.Section("invite_emails").Key("enabled").MustBool(false) { if recipient != nil && d.app.config.Section("invite_emails").Key("enabled").MustBool(false) {
d.app.debug.Printf("%s: Sending invite message", inviteCode) d.app.debug.Printf("%s: Sending invite message", invite.Code)
invname, err := d.bot.GuildMember(d.guildID, recipient.ID) invname, err := d.bot.GuildMember(d.guildID, recipient.ID)
invite.SendTo = invname.User.Username invite.SendTo = invname.User.Username
msg, err := d.app.email.constructInvite(inviteCode, invite, d.app, false) msg, err := d.app.email.constructInvite(invite.Code, invite, d.app, false)
if err != nil { if err != nil {
invite.SendTo = fmt.Sprintf("Failed to send to %s", RenderDiscordUsername(recipient)) invite.SendTo = fmt.Sprintf("Failed to send to %s", RenderDiscordUsername(recipient))
d.app.err.Printf("%s: Failed to construct invite message: %v", inviteCode, err) d.app.err.Printf("%s: Failed to construct invite message: %v", invite.Code, err)
//add response message //add response message
} else { } else {
var err error var err error
err = d.app.discord.SendDM(msg, recipient.ID) err = d.app.discord.SendDM(msg, recipient.ID)
if err != nil { if err != nil {
invite.SendTo = fmt.Sprintf("Failed to send to %s", RenderDiscordUsername(recipient)) invite.SendTo = fmt.Sprintf("Failed to send to %s", RenderDiscordUsername(recipient))
d.app.err.Printf("%s: %s: %v", inviteCode, invite.SendTo, err) d.app.err.Printf("%s: %s: %v", invite.Code, invite.SendTo, err)
err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{ err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
Type: dg.InteractionResponseChannelMessageWithSource, Type: dg.InteractionResponseChannelMessageWithSource,
Data: &dg.InteractionResponseData{ Data: &dg.InteractionResponseData{
@ -649,7 +640,7 @@ func (d *DiscordDaemon) cmdInvite(s *dg.Session, i *dg.InteractionCreate, lang s
d.app.err.Printf("Discord: Failed to send message to \"%s\": %v", RenderDiscordUsername(requester), err) d.app.err.Printf("Discord: Failed to send message to \"%s\": %v", RenderDiscordUsername(requester), err)
} }
} else { } else {
d.app.info.Printf("%s: Sent invite email to \"%s\"", inviteCode, RenderDiscordUsername(recipient)) d.app.info.Printf("%s: Sent invite email to \"%s\"", invite.Code, RenderDiscordUsername(recipient))
err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{ err := s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
Type: dg.InteractionResponseChannelMessageWithSource, Type: dg.InteractionResponseChannelMessageWithSource,
Data: &dg.InteractionResponseData{ Data: &dg.InteractionResponseData{