-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
385 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package component | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/cake4everyone/cake4everybot/util" | ||
) | ||
|
||
// GenericComponents is the Component handler for generic components | ||
type GenericComponents struct { | ||
util.InteractionUtil | ||
member *discordgo.Member | ||
user *discordgo.User | ||
originalAuthor *discordgo.User | ||
data discordgo.MessageComponentInteractionData | ||
} | ||
|
||
// Handle handles the functionality of a component interaction | ||
func (gc GenericComponents) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
gc.InteractionUtil = util.InteractionUtil{Session: s, Interaction: i} | ||
gc.member = i.Member | ||
gc.user = i.User | ||
if i.Member != nil { | ||
gc.user = i.Member.User | ||
} else if i.User != nil { | ||
gc.member = &discordgo.Member{User: i.User} | ||
} | ||
gc.data = i.MessageComponentData() | ||
|
||
if gc.Interaction.Message.Type == discordgo.MessageTypeChatInputCommand { | ||
gc.originalAuthor = gc.Interaction.Message.Interaction.User | ||
} else { | ||
gc.originalAuthor = gc.Interaction.Message.Author | ||
} | ||
|
||
ids := strings.Split(gc.data.CustomID, ".") | ||
// pop the first level identifier | ||
util.ShiftL(ids) | ||
|
||
switch util.ShiftL(ids) { | ||
case "delete": | ||
if gc.RequireOriginalAuthor() { | ||
gc.handleDelete() | ||
} | ||
return | ||
default: | ||
log.Printf("Unknown component interaction ID: %s", gc.data.CustomID) | ||
gc.ReplyError() | ||
} | ||
} | ||
|
||
// ID returns the component ID to identify the module | ||
func (gc GenericComponents) ID() string { | ||
return "generic" | ||
} | ||
|
||
func (gc GenericComponents) handleDelete() { | ||
err := gc.Session.ChannelMessageDelete(gc.Interaction.ChannelID, gc.Interaction.Message.ID) | ||
if err != nil { | ||
log.Printf("ERROR: could not delete message %s/%s: %+v", gc.Interaction.ChannelID, gc.Interaction.Message.ID, err) | ||
gc.ReplyError() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,55 @@ | ||
package faq | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/cake4everyone/cake4everybot/database" | ||
"github.com/cake4everyone/cake4everybot/logger" | ||
"github.com/cake4everyone/cake4everybot/util" | ||
) | ||
|
||
var log = logger.New("FAQ") | ||
|
||
var ( | ||
// lastFAQs is a cache for all the FAQs | ||
lastFAQs = make(map[string]map[string]string) | ||
// lastFAQTime is the time when the lastFAQs map was updated | ||
lastFAQTime time.Time | ||
) | ||
|
||
type faqBase struct { | ||
util.InteractionUtil | ||
member *discordgo.Member | ||
user *discordgo.User | ||
} | ||
|
||
func (faq faqBase) getAllFAQs() (map[string]string, error) { | ||
if time.Since(lastFAQTime) < 2*time.Minute { | ||
return lastFAQs[faq.Interaction.GuildID], nil | ||
} | ||
delete(lastFAQs, faq.Interaction.GuildID) | ||
lastFAQs[faq.Interaction.GuildID] = make(map[string]string) | ||
|
||
row, err := database.Query("SELECT question, answer FROM faq WHERE guild_id=?", faq.Interaction.GuildID) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return lastFAQs[faq.Interaction.GuildID], nil | ||
} else if err != nil { | ||
return lastFAQs[faq.Interaction.GuildID], fmt.Errorf("getting all FAQs from guild %s: %w", faq.Interaction.GuildID, err) | ||
} | ||
defer row.Close() | ||
|
||
for row.Next() { | ||
var question, answer string | ||
if err := row.Scan(&question, &answer); err != nil { | ||
return lastFAQs[faq.Interaction.GuildID], fmt.Errorf("scanning row: %w", err) | ||
} | ||
lastFAQs[faq.Interaction.GuildID][question] = answer | ||
} | ||
|
||
lastFAQTime = time.Now() | ||
return lastFAQs[faq.Interaction.GuildID], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package faq | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/cake4everyone/cake4everybot/util" | ||
) | ||
|
||
func (c Component) handleAllQuestions() { | ||
faqs, err := c.getAllFAQs() | ||
if err != nil { | ||
log.Printf("ERROR: getting all FAQs: %v", err) | ||
c.ReplyError() | ||
return | ||
} | ||
|
||
e := &discordgo.MessageEmbed{ | ||
Color: 0xFAB1FD, | ||
Title: "FAQs", | ||
} | ||
util.SetEmbedFooter(c.Session, tp+"display", e) | ||
|
||
var components []discordgo.MessageComponent | ||
var i int | ||
for question := range faqs { | ||
i++ | ||
util.AddEmbedField(e, fmt.Sprintf("%d", i), question, true) | ||
components = append(components, util.CreateButtonComponent(fmt.Sprintf("faq.show_question.%s", question), fmt.Sprint(i), discordgo.PrimaryButton, nil)) | ||
} | ||
components = []discordgo.MessageComponent{discordgo.ActionsRow{Components: components}} | ||
|
||
c.ReplyComponentsEmbedUpdate(components, e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package faq | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
) | ||
|
||
func (cmd Chat) handleAutocomplete(input string) { | ||
faqs, err := cmd.getAllFAQs() | ||
if err != nil { | ||
log.Printf("ERROR: getting all FAQs: %v", err) | ||
cmd.ReplyError() | ||
return | ||
} | ||
|
||
options := make([]*discordgo.ApplicationCommandOptionChoice, 0, len(faqs)) | ||
for question := range faqs { | ||
if input == "" || strings.Contains(strings.ToLower(question), strings.ToLower(input)) { | ||
options = append(options, &discordgo.ApplicationCommandOptionChoice{ | ||
Name: question, | ||
Value: question, | ||
}) | ||
} | ||
} | ||
cmd.ReplyAutocomplete(options) | ||
} |
Oops, something went wrong.