123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- package main
- import (
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "strings"
- "time"
- "github.com/bwmarrin/discordgo"
- "github.com/fatih/color"
- )
- type fileItem struct {
- Link string
- Filename string
- Time time.Time
- }
- var (
- skipCommands = []string{
- "skip",
- "ignore",
- "don't save",
- "no save",
- }
- )
- //#region Events
- func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
- handleMessage(m.Message, false, false)
- }
- func messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) {
- if m.EditedTimestamp != nil {
- handleMessage(m.Message, true, false)
- }
- }
- func handleMessage(m *discordgo.Message, edited bool, history bool) int64 {
- // Ignore own messages unless told not to
- if m.Author.ID == user.ID && !config.ScanOwnMessages {
- return -1
- }
- // Admin Channel
- if isAdminChannelRegistered(m.ChannelID) {
- m = fixMessage(m)
- // Log
- sendLabel := fmt.Sprintf("%s in \"%s\"#%s",
- getUserIdentifier(*m.Author),
- getGuildName(m.GuildID), getChannelName(m.ChannelID),
- )
- content := m.Content
- if len(m.Attachments) > 0 {
- content = content + fmt.Sprintf(" (%d attachments)", len(m.Attachments))
- }
- if edited {
- log.Println(color.HiGreenString("[ADMIN CHANNEL] "), color.CyanString("Edited [%s]: %s", sendLabel, content))
- } else {
- log.Println(color.HiGreenString("[ADMIN CHANNEL] "), color.CyanString("Message [%s]: %s", sendLabel, content))
- }
- }
- // Registered Channel
- if isChannelRegistered(m.ChannelID) {
- channelConfig := getChannelConfig(m.ChannelID)
- // Ignore bots if told to do so
- if m.Author.Bot && *channelConfig.IgnoreBots {
- return -1
- }
- // Ignore if told so by config
- if (!history && !*channelConfig.Enabled) || (edited && !*channelConfig.ScanEdits) {
- return -1
- }
- m = fixMessage(m)
- // Log
- if config.MessageOutput {
- sendLabel := fmt.Sprintf("%s in \"%s\"#%s",
- getUserIdentifier(*m.Author),
- getGuildName(m.GuildID), getChannelName(m.ChannelID),
- )
- content := m.Content
- if len(m.Attachments) > 0 {
- content = content + fmt.Sprintf(" (%d attachments)", len(m.Attachments))
- }
- if edited {
- log.Println(color.CyanString("Edited [%s]: %s", sendLabel, content))
- } else {
- log.Println(color.CyanString("Message [%s]: %s", sendLabel, content))
- }
- }
- // Log Messages to File
- if channelConfig.LogMessages != nil {
- if channelConfig.LogMessages.Destination != "" {
- logPath := channelConfig.LogMessages.Destination
- if *channelConfig.LogMessages.DestinationIsFolder == true {
- if !strings.HasSuffix(logPath, string(os.PathSeparator)) {
- logPath += string(os.PathSeparator)
- }
- err := os.MkdirAll(logPath, 0755)
- if err == nil {
- logPath += "Log_Messages"
- if *channelConfig.LogMessages.DivideLogsByServer == true {
- if m.GuildID == "" {
- ch, err := bot.State.Channel(m.ChannelID)
- if err == nil {
- if ch.Type == discordgo.ChannelTypeDM {
- logPath += " DM"
- } else if ch.Type == discordgo.ChannelTypeGroupDM {
- logPath += " GroupDM"
- } else {
- logPath += " Unknown"
- }
- } else {
- logPath += " Unknown"
- }
- } else {
- logPath += " SID_" + m.GuildID
- }
- }
- if *channelConfig.LogMessages.DivideLogsByChannel == true {
- logPath += " CID_" + m.ChannelID
- }
- if *channelConfig.LogMessages.DivideLogsByUser == true {
- logPath += " UID_" + m.Author.ID
- }
- }
- logPath += ".txt"
- }
- // Read
- currentLog, err := ioutil.ReadFile(logPath)
- currentLogS := ""
- if err == nil {
- currentLogS = string(currentLog)
- }
- canLog := true
- // Filter Duplicates
- if channelConfig.LogMessages.FilterDuplicates != nil {
- if *channelConfig.LogMessages.FilterDuplicates {
- if strings.Contains(currentLogS, fmt.Sprintf("[%s/%s/%s]", m.GuildID, m.ChannelID, m.ID)) {
- canLog = false
- }
- }
- }
- if canLog {
- // Writer
- f, err := os.OpenFile(logPath, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0600)
- if err != nil {
- log.Println(color.RedString("[channelConfig.LogMessages] Failed to open log file:\t%s", err))
- f.Close()
- }
- defer f.Close()
- var newLine string
- // Prepend
- prefix := ""
- if channelConfig.LogMessages.Prefix != nil {
- prefix = *channelConfig.LogMessages.Prefix
- }
- // More Data
- additionalInfo := ""
- if channelConfig.LogMessages.UserData != nil {
- if *channelConfig.LogMessages.UserData == true {
- additionalInfo = fmt.Sprintf("[%s/%s/%s] \"%s\"#%s (%s) @ %s: ", m.GuildID, m.ChannelID, m.ID, m.Author.Username, m.Author.Discriminator, m.Author.ID, m.Timestamp)
- }
- }
- if len(m.Attachments) > 0 {
- additionalInfo += fmt.Sprintf("<%d ATTACHMENTS> ", len(m.Attachments))
- }
- // Append
- suffix := ""
- if channelConfig.LogMessages.Suffix != nil {
- suffix = *channelConfig.LogMessages.Suffix
- }
- // New Line
- contentFmt, err := m.ContentWithMoreMentionsReplaced(bot)
- if err == nil {
- newLine += "\n" + prefix + additionalInfo + contentFmt + suffix
- } else {
- newLine += "\n" + prefix + additionalInfo + m.Content + suffix
- }
- if _, err = f.WriteString(newLine); err != nil {
- log.Println(color.RedString("[channelConfig.LogMessages] Failed to append file:\t%s", err))
- }
- }
- }
- }
- // Filters
- if channelConfig.Filters != nil {
- shouldAbort := false
- if channelConfig.Filters.AllowedPhrases != nil ||
- channelConfig.Filters.AllowedUsers != nil ||
- channelConfig.Filters.AllowedRoles != nil {
- shouldAbort = true
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.HiMagentaString("(FILTER)"), color.YellowString("Filter will be ignoring by default..."))
- }
- }
- if channelConfig.Filters.BlockedPhrases != nil {
- for _, phrase := range *channelConfig.Filters.BlockedPhrases {
- if strings.Contains(m.Content, phrase) {
- shouldAbort = true
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.HiMagentaString("(FILTER)"), color.YellowString("blockedPhrases found \"%s\" in message, planning to abort...", phrase))
- }
- break
- }
- }
- }
- if channelConfig.Filters.AllowedPhrases != nil {
- for _, phrase := range *channelConfig.Filters.AllowedPhrases {
- if strings.Contains(m.Content, phrase) {
- shouldAbort = false
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.HiMagentaString("(FILTER)"), color.YellowString("allowedPhrases found \"%s\" in message, planning to process...", phrase))
- }
- break
- }
- }
- }
- if channelConfig.Filters.BlockedUsers != nil {
- if stringInSlice(m.Author.ID, *channelConfig.Filters.BlockedUsers) {
- shouldAbort = true
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.HiMagentaString("(FILTER)"), color.YellowString("blockedUsers caught %s, planning to abort...", m.Author.ID))
- }
- }
- }
- if channelConfig.Filters.AllowedUsers != nil {
- if stringInSlice(m.Author.ID, *channelConfig.Filters.AllowedUsers) {
- shouldAbort = false
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.HiMagentaString("(FILTER)"), color.YellowString("allowedUsers caught %s, planning to process...", m.Author.ID))
- }
- }
- }
- if channelConfig.Filters.BlockedRoles != nil {
- member := m.Member
- if member == nil {
- member, _ = bot.GuildMember(m.GuildID, m.Author.ID)
- }
- if member != nil {
- for _, role := range member.Roles {
- if stringInSlice(role, *channelConfig.Filters.BlockedRoles) {
- shouldAbort = true
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.HiMagentaString("(FILTER)"), color.YellowString("blockedRoles caught %s, planning to abort...", role))
- }
- break
- }
- }
- }
- }
- if channelConfig.Filters.AllowedRoles != nil {
- member := m.Member
- if member == nil {
- member, _ = bot.GuildMember(m.GuildID, m.Author.ID)
- }
- if member != nil {
- for _, role := range member.Roles {
- if stringInSlice(role, *channelConfig.Filters.AllowedRoles) {
- shouldAbort = false
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.HiMagentaString("(FILTER)"), color.YellowString("allowedRoles caught %s, planning to allow...", role))
- }
- break
- }
- }
- }
- }
- // Abort
- if shouldAbort {
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.HiMagentaString("(FILTER)"), color.HiYellowString("Filter decided to ignore message..."))
- }
- return -1
- } /*else {
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.HiMagentaString("(FILTER)"), color.HiYellowString("Filter approved message..."))
- }
- }*/
- }
- // Skipping
- canSkip := config.AllowSkipping
- if channelConfig.OverwriteAllowSkipping != nil {
- canSkip = *channelConfig.OverwriteAllowSkipping
- }
- if canSkip {
- for _, cmd := range skipCommands {
- if m.Content == cmd {
- log.Println(color.HiYellowString("Message handling skipped due to use of skip command."))
- return -1
- }
- }
- }
- // Process Files
- var downloadCount int64
- files := getFileLinks(m)
- for _, file := range files {
- if file.Link == "" {
- continue
- }
- if config.DebugOutput {
- log.Println(logPrefixDebug, color.CyanString("FOUND FILE: "+file.Link))
- }
- status := startDownload(
- downloadRequestStruct{
- InputURL: file.Link,
- Filename: file.Filename,
- Path: channelConfig.Destination,
- Message: m,
- FileTime: file.Time,
- HistoryCmd: history,
- EmojiCmd: false,
- })
- if status.Status == downloadSuccess {
- downloadCount++
- }
- }
- return downloadCount
- }
- return -1
- }
- //#endregion
|