<commit_before>package bruxism

import (
	"fmt"
	"io"
	"log"
	"regexp"

	"github.com/iopred/discordgo"
)

// DiscordServiceName is the service name for the Discord service.
const DiscordServiceName string = "Discord"

// DiscordMessage is a Message wrapper around discordgo.Message.
type DiscordMessage struct {
	DiscordgoMessage *discordgo.Message
	MessageType      MessageType
}

// Channel returns the channel id for this message.
func (m *DiscordMessage) Channel() string {
	return m.DiscordgoMessage.ChannelID
}

// UserName returns the user name for this message.
func (m *DiscordMessage) UserName() string {
	if m.DiscordgoMessage.Author == nil {
		return ""
	}
	return m.DiscordgoMessage.Author.Username
}

// UserID returns the user id for this message.
func (m *DiscordMessage) UserID() string {
	if m.DiscordgoMessage.Author == nil {
		return ""
	}
	return m.DiscordgoMessage.Author.ID
}

// UserAvatar returns the avatar url for this message.
func (m *DiscordMessage) UserAvatar() string {
	if m.DiscordgoMessage.Author == nil {
		return ""
	}
	return discordgo.USER_AVATAR(m.DiscordgoMessage.Author.ID, m.DiscordgoMessage.Author.Avatar)
}

// Message returns the message content for this message.
func (m *DiscordMessage) Message() string {
	return m.DiscordgoMessage.ContentWithMentionsReplaced()
}

// RawMessage returns the raw message content for this message.
func (m *DiscordMessage) RawMessage() string {
	return m.DiscordgoMessage.Content
}

// MessageID returns the message ID for this message.
func (m *DiscordMessage) MessageID() string {
	return m.DiscordgoMessage.ID
}

// IsModerator returns whether or not the sender of this message is a moderator.
func (m *DiscordMessage) IsModerator() bool {
	return false
}

// MessageType returns the type of message.
func (m *DiscordMessage) Type() MessageType {
	return m.MessageType
}

// Discord is a Service provider for Discord.
type Discord struct {
	args        []interface{}
	Session     *discordgo.Session
	messageChan chan Message
}

// NewDiscord creates a new discord service.
func NewDiscord(args ...interface{}) *Discord {
	return &Discord{
		args:        args,
		messageChan: make(chan Message, 200),
	}
}

var channelIDRegex = regexp.MustCompile("<#[0-9]*>")

func (d *Discord) replaceChannelNames(message *discordgo.Message) {
	message.Content = channelIDRegex.ReplaceAllStringFunc(message.Content, func(str string) string {
		c, err := d.Session.State.Channel(str[2 : len(str)-1])
		if err != nil {
			return str
		}

		return "#" + c.Name
	})
}

func (d *Discord) onMessageCreate(s *discordgo.Session, message *discordgo.Message) {
	if message.Content == "" {
		return
	}

	d.replaceChannelNames(message)

	d.messageChan <- &DiscordMessage{message, MessageTypeCreate}
}

func (d *Discord) onMessageUpdate(s *discordgo.Session, message *discordgo.Message) {
	if message.Content == "" {
		return
	}

	d.replaceChannelNames(message)

	d.messageChan <- &DiscordMessage{message, MessageTypeUpdate}
}

func (d *Discord) onMessageDelete(s *discordgo.Session, message *discordgo.Message) {
	d.messageChan <- &DiscordMessage{message, MessageTypeDelete}
}

// Name returns the name of the service.
func (d *Discord) Name() string {
	return DiscordServiceName
}

// Open opens the service and returns a channel which all messages will be sent on.
func (d *Discord) Open() (<-chan Message, error) {
	var err error

	d.Session, err = discordgo.New(d.args...)
	if err != nil {
		return nil, err
	}
	d.Session.State.MaxMessageCount = 50
	d.Session.OnMessageCreate = d.onMessageCreate
	d.Session.OnMessageUpdate = d.onMessageUpdate
	d.Session.OnMessageDelete = d.onMessageDelete

	d.Session.Open()

	return d.messageChan, nil
}

// IsMe returns whether or not a message was sent by the bot.
func (d *Discord) IsMe(message Message) bool {
	if d.Session.State.User == nil {
		return false
	}<commit_msg>Support the new discordgo API.<commit_after>package bruxism

import (
	"fmt"
	"io"
	"log"
	"regexp"

	"github.com/iopred/discordgo"
)

// DiscordServiceName is the service name for the Discord service.
const DiscordServiceName string = "Discord"

// DiscordMessage is a Message wrapper around discordgo.Message.
type DiscordMessage struct {
	DiscordgoMessage *discordgo.Message
	MessageType      MessageType
}

// Channel returns the channel id for this message.
func (m *DiscordMessage) Channel() string {
	return m.DiscordgoMessage.ChannelID
}

// UserName returns the user name for this message.
func (m *DiscordMessage) UserName() string {
	if m.DiscordgoMessage.Author == nil {
		return ""
	}
	return m.DiscordgoMessage.Author.Username
}

// UserID returns the user id for this message.
func (m *DiscordMessage) UserID() string {
	if m.DiscordgoMessage.Author == nil {
		return ""
	}
	return m.DiscordgoMessage.Author.ID
}

// UserAvatar returns the avatar url for this message.
func (m *DiscordMessage) UserAvatar() string {
	if m.DiscordgoMessage.Author == nil {
		return ""
	}
	return discordgo.USER_AVATAR(m.DiscordgoMessage.Author.ID, m.DiscordgoMessage.Author.Avatar)
}

// Message returns the message content for this message.
func (m *DiscordMessage) Message() string {
	return m.DiscordgoMessage.ContentWithMentionsReplaced()
}

// RawMessage returns the raw message content for this message.
func (m *DiscordMessage) RawMessage() string {
	return m.DiscordgoMessage.Content
}

// MessageID returns the message ID for this message.
func (m *DiscordMessage) MessageID() string {
	return m.DiscordgoMessage.ID
}

// IsModerator returns whether or not the sender of this message is a moderator.
func (m *DiscordMessage) IsModerator() bool {
	return false
}

// MessageType returns the type of message.
func (m *DiscordMessage) Type() MessageType {
	return m.MessageType
}

// Discord is a Service provider for Discord.
type Discord struct {
	args        []interface{}
	Session     *discordgo.Session
	messageChan chan Message
}

// NewDiscord creates a new discord service.
func NewDiscord(args ...interface{}) *Discord {
	return &Discord{
		args:        args,
		messageChan: make(chan Message, 200),
	}
}

var channelIDRegex = regexp.MustCompile("<#[0-9]*>")

func (d *Discord) replaceChannelNames(message *discordgo.Message) {
	message.Content = channelIDRegex.ReplaceAllStringFunc(message.Content, func(str string) string {
		c, err := d.Session.State.Channel(str[2 : len(str)-1])
		if err != nil {
			return str
		}

		return "#" + c.Name
	})
}

func (d *Discord) onMessageCreate(s *discordgo.Session, message *discordgo.MessageCreate) {
	if message.Content == "" {
		return
	}

	d.replaceChannelNames(message.Message)

	d.messageChan <- &DiscordMessage{message.Message, MessageTypeCreate}
}

func (d *Discord) onMessageUpdate(s *discordgo.Session, message *discordgo.MessageUpdate) {
	if message.Content == "" {
		return
	}

	d.replaceChannelNames(message.Message)

	d.messageChan <- &DiscordMessage{message.Message, MessageTypeUpdate}
}

func (d *Discord) onMessageDelete(s *discordgo.Session, message *discordgo.MessageDelete) {
	d.messageChan <- &DiscordMessage{message.Message, MessageTypeDelete}
}

// Name returns the name of the service.
func (d *Discord) Name() string {
	return DiscordServiceName
}

// Open opens the service and returns a channel which all messages will be sent on.
func (d *Discord) Open() (<-chan Message, error) {
	var err error

	d.Session, err = discordgo.New(d.args...)
	if err != nil {
		return nil, err
	}
	d.Session.State.MaxMessageCount = 50
	d.Session.AddHandler(d.onMessageCreate)
	d.Session.AddHandler(d.onMessageUpdate)
	d.Session.AddHandler(d.onMessageDelete)

	d.Session.Open()

	return d.messageChan, nil
}

// IsMe returns whether or not a message was sent by the bot.
func (d *Discord) IsMe(message Message) bool {
	if d.Session.State.User == nil {
		return false
	}
{'fraction_non_alphanumeric': 0.10834722400307732, 'fraction_numerical': 0.002307988203615848, 'mean_word_length': 4.259608900876602, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2786349', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2495, 'n_words': 735}