Skip to content

Commit

Permalink
all-bot: add support for dm channels not being in state
Browse files Browse the repository at this point in the history
  • Loading branch information
jogramming committed Nov 1, 2018
1 parent 3b76244 commit f691fce
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion automod_legacy/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func CheckMessage(m *discordgo.Message) bool {
return false // Pls no panicerinos or banerinos self
}

if m.Author.Bot {
if m.Author.Bot || m.GuildID == 0 {
return false
}

Expand Down
11 changes: 4 additions & 7 deletions commands/yagcommmand.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) {
}

// Send typing to indicate the bot's working
common.BotSession.ChannelTyping(data.CS.ID)
common.BotSession.ChannelTyping(data.Msg.ChannelID)

logger := yc.Logger(data)

Expand All @@ -130,22 +130,19 @@ func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) {
yc.logExecutionTime(time.Since(started), data.Msg.Content, data.Msg.Author.Username)
}()

cState := bot.State.Channel(true, data.Msg.ChannelID)
if cState == nil {
return nil, errors.New("Channel not found")
}
cState := data.CS

// Set up log entry for later use
logEntry := &common.LoggedExecutedCommand{
UserID: discordgo.StrID(data.Msg.Author.ID),
ChannelID: discordgo.StrID(cState.ID),
ChannelID: discordgo.StrID(data.Msg.ChannelID),

Command: yc.Name,
RawCommand: data.Msg.Content,
TimeStamp: time.Now(),
}

if cState.Guild != nil {
if cState != nil && cState.Guild != nil {
logEntry.GuildID = discordgo.StrID(cState.Guild.ID)
}

Expand Down
4 changes: 4 additions & 0 deletions customcommands/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func StringCommands(ccs []*CustomCommand) string {
}

func shouldIgnoreChannel(evt *discordgo.MessageCreate, cState *dstate.ChannelState) bool {
if evt.GuildID == 0 {
return true
}

if cState == nil {
log.Warn("Channel not found in state")
return true
Expand Down
9 changes: 4 additions & 5 deletions serverstats/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,16 @@ func HandleMemberRemove(evt *eventsystem.EventData) {
func HandleMessageCreate(evt *eventsystem.EventData) {

m := evt.MessageCreate()
channel := bot.State.Channel(true, m.ChannelID)
if m.GuildID == 0 {
return // private channel
}

channel := bot.State.Channel(true, m.ChannelID)
if channel == nil {
log.WithField("channel", m.ChannelID).Warn("Channel not in state")
return
}

if channel.IsPrivate() {
return
}

config, err := BotCachedFetchGuildConfig(evt.Context(), channel.Guild)
if err != nil {
log.WithError(err).WithField("guild", channel.Guild.ID).Error("Failed retrieving config")
Expand Down
5 changes: 2 additions & 3 deletions stdcommands/reverse/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package reverse

import (
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/automod_legacy"
"github.com/jonas747/yagpdb/commands"
)
Expand All @@ -28,9 +27,9 @@ var Command = &commands.YAGCommand{
cop := *data.Msg
cop.Content = out

if data.CS.Type == discordgo.ChannelTypeGuildText {
if data.GS != nil {

if automod_legacy.CheckMessageForBadInvites(out, data.CS.Guild.ID) {
if automod_legacy.CheckMessageForBadInvites(out, data.GS.ID) {
return data.Msg.Author.Mention() + " tried to use the reverse command to send a invite to another server :(", nil
}

Expand Down

0 comments on commit f691fce

Please sign in to comment.