Main/Features: Implement single handler function, Add deleted message logging.

master
Marc Egerton 2020-05-25 10:12:24 +01:00
parent bf65b0b968
commit df7be2f636
4 changed files with 87 additions and 16 deletions

View File

@ -23,5 +23,7 @@
{ "color": "pink", "id": "697965484313935966" }
],
"rules_text": "The Hak5 community is a place where pentesters, students, coders, enthusiasts and all-around Hak5 fans come together to help each other, inspire one another and collectively share feedback with Hak5. It's a welcoming place! We just ask that you follow these simple rules:\n\n1. BE GOOD. BE NICE. BEHAVE\nThis isn't a place for trolling it's a place to help an encourage each other, and to provide constructive feedback. Remember, nobody was born 1337. We all started somewhere.\n\n2. DON'T SPAM\nPlease keep your posts relevant to the topic, thread or board you're posting on. Don't post random junk, troll bait or off topic ramblings that's what YouTube is for ;)\n\n3. VIEWS EXPRESSED ARE NOT THAT OF HAK5\nWe don't prescreen any information submitted by community members. We retain the right, but not the responsibility, to edit or remove posts which violate the community guidelines. Further, Hak5 does not provide formal product support on the community forums. Hak5 may provide general product or technical information, however any information provided is offered on an \"AS IS\" basis without warranties of any kind. This disclaimer is in addition to the disclaimers and limitation of liability set forth in the Terms of Service. Similarly, community contributions such as payloads come with absolutely no warranty. You are solely responsible for the outcome of their execution.\n\nNo advertising or solicitiaion and please keep chat both ethical & legal.\n\nPlease do not post any personal order information in chat.\nIf you have questions about an order of you've placed with the Hak5 Shop please contact support via the links provided on our website https://shop.hak5.org/"
"rules_text": "The Hak5 community is a place where pentesters, students, coders, enthusiasts and all-around Hak5 fans come together to help each other, inspire one another and collectively share feedback with Hak5. It's a welcoming place! We just ask that you follow these simple rules:\n\n1. BE GOOD. BE NICE. BEHAVE\nThis isn't a place for trolling it's a place to help an encourage each other, and to provide constructive feedback. Remember, nobody was born 1337. We all started somewhere.\n\n2. DON'T SPAM\nPlease keep your posts relevant to the topic, thread or board you're posting on. Don't post random junk, troll bait or off topic ramblings that's what YouTube is for ;)\n\n3. VIEWS EXPRESSED ARE NOT THAT OF HAK5\nWe don't prescreen any information submitted by community members. We retain the right, but not the responsibility, to edit or remove posts which violate the community guidelines. Further, Hak5 does not provide formal product support on the community forums. Hak5 may provide general product or technical information, however any information provided is offered on an \"AS IS\" basis without warranties of any kind. This disclaimer is in addition to the disclaimers and limitation of liability set forth in the Terms of Service. Similarly, community contributions such as payloads come with absolutely no warranty. You are solely responsible for the outcome of their execution.\n\nNo advertising or solicitiaion and please keep chat both ethical & legal.\n\nPlease do not post any personal order information in chat.\nIf you have questions about an order of you've placed with the Hak5 Shop please contact support via the links provided on our website https://shop.hak5.org/",
"logging_channel": "714369335254188053"
}

View File

@ -62,6 +62,9 @@ func main() {
log.Fatal("[!] Error: " + err.Error())
}
// Enable Message Caching (Last 1024 Events)
bot.State.MaxMessageCount = 1024
// Open Connection
err = bot.Open()
if err != nil {
@ -85,8 +88,7 @@ func main() {
// Register Handlers
f.RegisterHandlers()
bot.AddHandler(f.ProcessCommand)
bot.AddHandler(f.ProcessUserJoin)
bot.AddHandler(f.ProcessMessage)
log.Printf("[*] Bot Running.\n")

View File

@ -1,6 +1,7 @@
package features
import (
"errors"
"github.com/bwmarrin/discord.go"
"log"
"strings"
@ -45,23 +46,23 @@ func (f *Features) RegisterHandlers() {
f.RegisterCommand("ban", f.handleBanUser)
}
func (f *Features) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
func (f *Features) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) error {
cKey := f.Config.CommandKey
cCmd := strings.Split(m.Content, " ")[0]
// Ignore the bot itself
if m.Author.ID == s.State.User.ID {
return
return nil
}
// Ignore anything not starting with the command prefix
if !strings.HasPrefix(cCmd, cKey) {
return
return nil
}
// Ignore Direct Messages
if m.Member == nil {
return
return nil
}
cName := strings.Split(cCmd, cKey)[1]
@ -73,33 +74,97 @@ func (f *Features) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCrea
if err != nil {
log.Printf("[*] Command %s (Requested by %s) had error: '%s'\n", cName, m.Author.Username, err.Error())
eMsg := f.CreateDefinedEmbed("Error ("+cName+")", err.Error(), "error", m.Author)
eMsg := f.CreateDefinedEmbed("[!] Error ("+cName+")", err.Error(), "error", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
log.Fatal(err)
return err
}
}
}
return nil
}
func (f *Features) ProcessUserJoin(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
func (f *Features) ProcessMessageDelete(s *discordgo.Session, m *discordgo.MessageDelete) error {
msgChannelID := m.ChannelID
if m.BeforeDelete == nil {
return errors.New("Couldn't get deleted message data.")
}
msgContent := m.BeforeDelete.Content
msgAuthor := m.BeforeDelete.Author
msg := "`Username` - " + msgAuthor.Username + "#" + msgAuthor.Discriminator + "\n"
msg += "`User ID` - " + msgAuthor.ID + "\n"
msg += "`Channel` - <#" + msgChannelID + ">\n"
msg += "`Message` - " + msgContent + "\n"
embed := f.CreateDefinedEmbed("Deleted Message", msg, "", msgAuthor)
_, err := s.ChannelMessageSendEmbed(f.Config.LoggingChannel, embed)
if err != nil {
return err
}
return nil
}
func (f *Features) ProcessUserJoin(s *discordgo.Session, m *discordgo.GuildMemberAdd) error {
userChannel, err := s.UserChannelCreate(m.User.ID)
if err != nil {
log.Print("Error (User Join): " + err.Error())
return
log.Print("[!] Error (User Join): " + err.Error())
return err
}
_, err = s.ChannelMessageSend(userChannel.ID, f.Config.WelcomeText)
if err != nil {
log.Print("Error (User Join): " + err.Error())
return
log.Print("[!] Error (User Join): " + err.Error())
return err
}
for _, roleID := range f.Config.JoinRoleIDs {
err = s.GuildMemberRoleAdd(f.Auth.Guild.ID, m.User.ID, roleID)
if err != nil {
log.Print("Error (User Join)" + err.Error())
log.Print("[!] Error (User Join)" + err.Error())
return err
}
}
return nil
}
func (f *Features) ProcessMessage(s *discordgo.Session, m interface{}) {
switch m.(type) {
case *discordgo.MessageCreate:
// Pass Messages to the command processor
err := f.ProcessCommand(s, m.(*discordgo.MessageCreate))
if err != nil {
log.Fatal("[!] Error: " + err.Error())
}
break
case *discordgo.MessageDelete:
// Log deleted messages to the logging channel.
err := f.ProcessMessageDelete(s, m.(*discordgo.MessageDelete))
if err != nil {
log.Printf("[!] Error " + err.Error())
eMsg := f.CreateDefinedEmbed("Error (Message Deleted)", err.Error(), "error", nil)
_, err = s.ChannelMessageSendEmbed(f.Config.LoggingChannel, eMsg)
if err != nil {
log.Fatal("[!] Error " + err.Error())
}
}
break
case *discordgo.GuildMemberAdd:
// Handle new member (Welcome message, etc)
_ = f.ProcessUserJoin(s, m.(*discordgo.GuildMemberAdd))
/* if err != nil {
log.Fatal("[!] Error: " + err.Error())
}*/
break
case *discordgo.MessageDeleteBulk:
//TODO: Should handle these too
return
}
}
}

View File

@ -30,4 +30,6 @@ type Configuration struct {
ColorRoles []ColorRole `json:"color_roles"`
Guild *discordgo.Guild `json:"reserved_guild"`
LoggingChannel string `json:"logging_channel"`
}