<commit_before>package bot_reactions

import (
	"fmt"
	"strings"

	"github.com/adayoung/ada-bot/settings"
	"github.com/bwmarrin/discordgo"
)

type BotReaction interface {
	Help() string
	HelpDetail(*discordgo.Message) string
	Reaction(message *discordgo.Message, author *discordgo.Member) string
}

var _botReactions map[string][]BotReaction

func init() {
	_botReactions = make(map[string][]BotReaction)
}

func addReaction(trigger string, reaction BotReaction) {
	// FIXME: calls to addReaction should be idempotent, let's
	// not add multiple instances of the same reaction to a trigger
	_botReactions[trigger] = append(_botReactions[trigger], reaction)
}

func GetReactions(message *discordgo.Message, author *discordgo.Member) []string {
	if _, ok := _botReactions["*"]; ok { // Run wildcard triggers first
		for _, reaction := range _botReactions["*"] {
			_ = reaction.Reaction(message, author) // Wildcard triggers should not respond
		}
	}

	var reactions []string
	if !strings.HasPrefix(message.Content, settings.Settings.Discord.BotPrefix) {
		return reactions // The message is irrelevant, bail out with no reactions
	}

	if strings.TrimSpace(message.Content) == fmt.Sprintf("%shelp", settings.Settings.Discord.BotPrefix) {
		reactions = append(reactions, GenHelp())
		return reactions
	}

	for trigger, _reactions := range _botReactions {
		if strings.HasPrefix(message.Content[len(settings.Settings.Discord.BotPrefix):], trigger) {
			for _, reaction := range _reactions {
				reactions = append(reactions, reaction.Reaction(message, author))
			}
		}
	}

	return reactions
}

func GenHelp() string { // FIXME: Use padding to correctly align the help text
	help := "I have the following commands available:"
	for k, v := range _botReactions {
		for _, item := range v {
			help = fmt.Sprintf("%s\n%s%s - %s", help, settings.Settings.Discord.BotPrefix, k, item.Help())
		}
	}
	return fmt.Sprintf("```%s```", help)
}

func GetHelpDetail(trigger string, message *discordgo.Message) string {
	return "" // TODO: Not implemented yet
	// var help []string
	// if _, ok := _botReactions[trigger]; ok {
	// 	for _, reaction := range _botReactions[trigger] {
	// 		help = append(help, reaction.HelpDetail(message))
	// 	}
	// }
	// return help
}
<commit_msg>Bot reaction triggers are case insensitive again! :D<commit_after>package bot_reactions

import (
	"fmt"
	"strings"

	"github.com/adayoung/ada-bot/settings"
	"github.com/bwmarrin/discordgo"
)

type BotReaction interface {
	Help() string
	HelpDetail(*discordgo.Message) string
	Reaction(message *discordgo.Message, author *discordgo.Member) string
}

var _botReactions map[string][]BotReaction

func init() {
	_botReactions = make(map[string][]BotReaction)
}

func addReaction(trigger string, reaction BotReaction) {
	// FIXME: calls to addReaction should be idempotent, let's
	// not add multiple instances of the same reaction to a trigger
	_botReactions[trigger] = append(_botReactions[trigger], reaction)
}

func GetReactions(message *discordgo.Message, author *discordgo.Member) []string {
	if _, ok := _botReactions["*"]; ok { // Run wildcard triggers first
		for _, reaction := range _botReactions["*"] {
			_ = reaction.Reaction(message, author) // Wildcard triggers should not respond
		}
	}

	var reactions []string
	if !strings.HasPrefix(message.Content, settings.Settings.Discord.BotPrefix) {
		return reactions // The message is irrelevant, bail out with no reactions
	}

	if strings.TrimSpace(strings.ToLower(message.Content)) == fmt.Sprintf("%shelp", settings.Settings.Discord.BotPrefix) {
		reactions = append(reactions, GenHelp())
		return reactions
	}

	for trigger, _reactions := range _botReactions {
		if strings.HasPrefix(strings.ToLower(message.Content[len(settings.Settings.Discord.BotPrefix):]), strings.ToLower(trigger)) {
			for _, reaction := range _reactions {
				reactions = append(reactions, reaction.Reaction(message, author))
			}
		}
	}

	return reactions
}

func GenHelp() string { // FIXME: Use padding to correctly align the help text
	help := "I have the following commands available:"
	for k, v := range _botReactions {
		for _, item := range v {
			help = fmt.Sprintf("%s\n%s%s - %s", help, settings.Settings.Discord.BotPrefix, k, item.Help())
		}
	}
	return fmt.Sprintf("```%s```", help)
}

func GetHelpDetail(trigger string, message *discordgo.Message) string {
	return "" // TODO: Not implemented yet
	// var help []string
	// if _, ok := _botReactions[trigger]; ok {
	// 	for _, reaction := range _botReactions[trigger] {
	// 		help = append(help, reaction.HelpDetail(message))
	// 	}
	// }
	// return help
}

{'fraction_non_alphanumeric': 0.12889948006932408, 'fraction_numerical': 0.0, 'mean_word_length': 4.476868327402135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5201559', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1458, 'n_words': 381}