Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gitbook lens search command #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions commands/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ func (c *Commands) RegisterHandlers() {
c.RegisterCommand("intocm", "Convert Inches to Centimeters", false, c.handleInchToCentimeter)
c.RegisterCommand("google4u", "Displays a letmegooglethat link", false, c.handleGoogle4U)

//Product Helpers
c.RegisterCommand("askdocs", "Ask docs.hak5.org a question", false, c.handleAskDocs)
c.RegisterCommand("docs", "Link Docs", false, c.handleDocs)
c.RegisterCommand("github", "Link Github", false, c.handleGithub)
c.RegisterCommand("payloads", "Link Github + PayloadHub", false, c.handlePayloads)
c.RegisterCommand("payloadhub", "Link PayloadHub", false, c.handlePayloadHub)
c.RegisterCommand("support", "Link Support", false, c.handleSupport)
c.RegisterCommand("shop", "Link Shop", false, c.handleShop)
c.RegisterCommand("buy", "Link Shop", false, c.handleShop)
c.RegisterCommand("invite", "Link direct channel invite", false, c.handleInvite)
// TODO payloadstudio view

// Admin Commands
c.RegisterCommand("ping", "Ping Scuzzy", true, c.handlePing)
c.RegisterCommand("rules", "Display the Server Rules", true, c.handleRules)
Expand All @@ -68,6 +80,8 @@ func (c *Commands) RegisterHandlers() {
c.RegisterCommand("saveconfig", "Save Configuration to Disk", true, c.handleSaveConfig)
c.RegisterCommand("reloadconfig", "Reload Configuration", true, c.handleReloadConfig)
c.RegisterCommand("addrole", "Add a joinable role", true, c.handleAddCustomRole)
c.RegisterCommand("moveto", "Move a users message to another channel (deletes originals)", true, c.handleMoveto)
c.RegisterCommand("bringto", "Copy a users message to another channel (keeps originals) ", true, c.handleBringto)
}

func (c *Commands) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) error {
Expand Down
90 changes: 90 additions & 0 deletions commands/moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,93 @@ func (c *Commands) handleUnIgnoreUser(s *discordgo.Session, m *discordgo.Message

return nil
}

func (c *Commands) handleMoveto(s *discordgo.Session, m *discordgo.MessageCreate) error {
args := strings.Split(m.Content, " ")
if len(args) < 2 {
return errors.New("missing arg")
}
input := m.Content[strings.Index(m.Content, " "):len(m.Content)]

repliedTo, err := s.ChannelMessage(m.ChannelID, m.MessageReference.MessageID)
if err != nil {
return errors.New("error getting target message")
}

moveToId := ""
channels, _ := s.GuildChannels(c.Config.GuildID)
for _, c := range channels {
if c.Type != discordgo.ChannelTypeGuildText {
continue
}
if strings.Contains(input, c.ID) {
moveToId = c.ID
break
}
}

if moveToId == "" {
return errors.New("failed to link channels")
}

_, err = s.ChannelMessageSend(m.ChannelID, "Moderator <@"+m.Author.ID+"> moved <@"+repliedTo.Author.ID+">'s message to <#"+moveToId+"> to continue the conversation there")
if err != nil {
return errors.New("error announcing move")
}

_, err = s.ChannelMessageSend(moveToId, "Moderator <@"+m.Author.ID+"> moved this message from <#"+m.ChannelID+">\n\nOriginal message:\n\n <@"+repliedTo.Author.ID+">: "+repliedTo.Content)
if err != nil {
return errors.New("error moving message")
}

err = s.ChannelMessageDelete(m.ChannelID, m.ID)
if err != nil {
return errors.New("error (re)moving command message")
}

err = s.ChannelMessageDelete(m.ChannelID, m.MessageReference.MessageID)
if err != nil {
return errors.New("error (re)moving message")
}
return nil
}

func (c *Commands) handleBringto(s *discordgo.Session, m *discordgo.MessageCreate) error {
args := strings.Split(m.Content, " ")
if len(args) < 2 {
return errors.New("missing arg")
}
input := m.Content[strings.Index(m.Content, " "):len(m.Content)]

repliedTo, err := s.ChannelMessage(m.ChannelID, m.MessageReference.MessageID)
if err != nil {
return errors.New("error getting target message")
}

moveToId := ""
channels, _ := s.GuildChannels(c.Config.GuildID)
for _, c := range channels {
if c.Type != discordgo.ChannelTypeGuildText {
continue
}
if strings.Contains(input, c.ID) {
moveToId = c.ID
break
}
}

if moveToId == "" {
return errors.New("failed to link channels")
}

_, err = s.ChannelMessageSend(m.ChannelID, "<@"+m.Author.ID+"> copied <@"+repliedTo.Author.ID+">'s message to <#"+moveToId+"> to continue the conversation there.")
if err != nil {
return errors.New("error announcing move")
}

_, err = s.ChannelMessageSend(moveToId, "<@"+m.Author.ID+"> copied this message from <#"+m.ChannelID+">\n\nOriginal message:\n\n <@"+repliedTo.Author.ID+">: "+repliedTo.Content)
if err != nil {
return errors.New("error moving message")
}
return nil
}
194 changes: 194 additions & 0 deletions commands/products.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package commands

import (
"bytes"
"encoding/json"
"errors"
"github.com/bwmarrin/discordgo"
"github.com/foxtrot/scuzzy/models"
"log"
"net/http"
"strings"
)

func (c *Commands) getProductFromChannelID(cid string) models.Product {
switch cid {
case models.Ducky.ChannelID:
return models.Ducky
case models.PayloadStudio.ChannelID:
return models.PayloadStudio
case models.Bunny.ChannelID:
return models.Bunny
case models.Croc.ChannelID:
return models.Croc
case models.Shark.ChannelID:
return models.Shark
case models.CloudC2.ChannelID:
return models.CloudC2
case models.Crab.ChannelID:
return models.Crab
case models.Pineapple.ChannelID:
return models.Pineapple
case models.Coconut.ChannelID:
return models.Coconut
case models.Squirrel.ChannelID:
return models.Squirrel
case models.Turtle.ChannelID:
return models.Turtle
case models.OMG.ChannelID:
return models.OMG
default:
return models.Default
}
}

func (c *Commands) handleAskDocs(s *discordgo.Session, m *discordgo.MessageCreate) error {
args := strings.Split(m.Content, " ")

if len(args) < 2 {
return errors.New("missing question")
}

input := m.Content[strings.Index(m.Content, " "):len(m.Content)]

model := c.getProductFromChannelID(m.ChannelID)

// Default guard
if model.SpaceID == models.Default.SpaceID {
_, err := s.ChannelMessageSend(m.ChannelID, "AI Search not enabled for this product, but you can still look for yourself here: "+model.Docslink)
if err != nil {
return err
}
return nil
}

pre := "<:thonk:666272807524761620> Asking the"
post := "docs..."
askEndpoint := models.GITBOOKAPI + model.SpaceID + models.SEARCHENDPOINT
searchingIn := pre + " " + model.Emoji + " " + model.ProductName + " " + post
_, err := s.ChannelMessageSend(m.ChannelID, searchingIn)
if err != nil {
return err
}

j, err := json.Marshal(&models.AISearchRequest{Query: input})
if err != nil {
return errors.New("error marshalling input")
}

r, err := http.NewRequest("POST", askEndpoint, bytes.NewBuffer(j))
if err != nil {
return errors.New("error creating request")
}

r.Header.Add("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(r)
if err != nil {
return errors.New("error creating request client")
}

defer res.Body.Close()

resp := &models.AISearchResult{}
derr := json.NewDecoder(res.Body).Decode(resp)
if derr != nil {
return errors.New("error getting response from docs")
}

if res.StatusCode != http.StatusOK {
return errors.New("error getting response from docs: " + res.Status)
}

prepend := "<:9999iq:1050238565671514172> AI Generated Response:\n\n"
_, err = s.ChannelMessageSend(m.ChannelID, prepend+resp.Answer.Text+"\n\n"+model.Docslink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}

return nil
}

func (c *Commands) handleDocs(s *discordgo.Session, m *discordgo.MessageCreate) error {
model := c.getProductFromChannelID(m.ChannelID)
_, err := s.ChannelMessageSend(m.ChannelID, model.Emoji+" "+model.ProductName+" Docs: "+model.Docslink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
return nil
}

func (c *Commands) handleGithub(s *discordgo.Session, m *discordgo.MessageCreate) error {
model := c.getProductFromChannelID(m.ChannelID)
_, err := s.ChannelMessageSend(m.ChannelID, model.Emoji+" "+model.ProductName+" Github: "+model.Githublink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
return nil
}

func (c *Commands) handlePayloads(s *discordgo.Session, m *discordgo.MessageCreate) error {
model := c.getProductFromChannelID(m.ChannelID)
_, err := s.ChannelMessageSend(m.ChannelID, model.Emoji+" "+model.ProductName+" Github: "+model.Githublink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
_, err = s.ChannelMessageSend(m.ChannelID, model.Emoji+" "+model.ProductName+" PayloadHub: "+model.Payloadslink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
return nil
}

func (c *Commands) handlePayloadHub(s *discordgo.Session, m *discordgo.MessageCreate) error {
model := c.getProductFromChannelID(m.ChannelID)
_, err := s.ChannelMessageSend(m.ChannelID, model.Emoji+" "+model.ProductName+" PayloadHub: "+model.Payloadslink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
return nil
}

func (c *Commands) handleSupport(s *discordgo.Session, m *discordgo.MessageCreate) error {
model := c.getProductFromChannelID(m.ChannelID)
_, err := s.ChannelMessageSend(m.ChannelID, model.Emoji+" "+model.ProductName+" Support: "+model.SupportLink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
return nil
}

func (c *Commands) handleInvite(s *discordgo.Session, m *discordgo.MessageCreate) error {
model := c.getProductFromChannelID(m.ChannelID)
if model.InviteLink == models.Default.InviteLink {
_, err := s.ChannelMessageSend(m.ChannelID, model.Emoji+"Invite Link: "+model.InviteLink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
} else {
_, err := s.ChannelMessageSend(m.ChannelID, model.Emoji+" "+model.ProductName+" Channel Invite Link: "+model.InviteLink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
}
return nil
}

func (c *Commands) handleShop(s *discordgo.Session, m *discordgo.MessageCreate) error {
model := c.getProductFromChannelID(m.ChannelID)
_, err := s.ChannelMessageSend(m.ChannelID, "Shop "+model.Emoji+" "+model.ProductName+" "+model.Shoplink)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
return nil
}
19 changes: 19 additions & 0 deletions models/gitbook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package models

type AISearchRequest struct {
Query string `json:"query"`
// PreviousQueries []string `json:"previousQueries"`
}

type AISearchResult struct {
Answer struct {
Text string `json:"text"`
FollowupQuestions []string `json:"followupQuestions"`
Pages []struct {
Page string `json:"page"`
Revision string `json:"revision"`
Space string `json:"space"`
Sections []string `json:"sections"`
} `json:"pages"`
} `json:"answer"`
}
Loading