alexberryman / DiscordWordle

Discord bot to track Wordle scores
MIT License
12 stars 15 forks source link

Use this code to track scores for other games similar to Wordle #30

Open tearjeong opened 2 years ago

tearjeong commented 2 years ago

Hi, I am trying to tweak this bot to work for a different wordle like game, it uses different colo emojis and has a different name

Is there a way to make that happen somehow? I'm a huge newbie when it comes to coding but I use python

Thank you!

alexberryman commented 2 years ago

To help you get started with your discovery on how you would accomplish this, I'd like to point out a few interesting parts of the code.

First, we start in themain.go file. The bot decides to respond to a message if the message contains a mention of the bot or it detects some Wordle content:

botMentionToken := fmt.Sprintf("@%s", botName)
wordleScoreDetected, err := mentionlessWordleScoreDetection(tokenizedContent)

if strings.HasPrefix(tokenizedContent, botMentionToken) || wordleScoreDetected {

The "wordle content detection" where I look for the format of Wordle 292 3/6<new line> is done in the mentionlessWordleScoreDetection() function

func mentionlessWordleScoreDetection(input string) (bool, error) {
    var dataExp = regexp.MustCompile(fmt.Sprintf(`Wordle (?P<game_id>\d+)\s(?P<guesses>\d+|%s)/6[\%s]?\n`, noSolutionResult, hardModeIndicator))
    result, err := matchGroupsToStringMap(input, dataExp)
    if err != nil {
        return false, err
    }

    return len(result) > 0, nil
}

Here I use a regular expression to attempt to search for a message in a specific format:

Wordle (?P<game_id>\d+)\s(?P<guesses>\d+|%s)/6[\%s]?\n

I inject a few special characters like X for no solution or * to indicate hard-mode into the parts that say %s. Afer doing this I end up with a regex that matches the first line of a Wordle score block followed by a new line, and ignore the rest of the emojis. You can see this RegEx in action here: https://regex101.com/r/eCVzSa/1

Screen Shot 2022-04-07 at 3 27 41 AM

This would be the first part you would need to swap out to detect scores from other games like Quardle, Worldle, and so on to get the bot to start doing other stuff like saving scores to the database.

All of that fun stuff happens in a big long chain of if message contains ______ do ______ statements in routeMessageToAction() function in the main.go file: if message contains "wordle stuff" do "persist score to database"

func routeMessageToAction(ctx context.Context, s *discordgo.Session, m *discordgo.MessageCreate, input string, account wordle.Account, q *wordle.Queries, botMentionToken string) {
    var r response

    if strings.Contains(input, cmdWordle) {
        gameId, guesses, err := extractGameGuesses(input)
        if err != nil {
            log.Error().Str("server_id", m.GuildID).Str("input", input).Str("author", m.Author.ID).Str("command", cmdWordle).Err(err).Msg("Error parsing guess count")
        }
        log.Info().Str("server_id", m.GuildID).Str("input", input).Str("author", m.Author.ID).Str("command", cmdWordle).Int("guesses", guesses).Int("game_id", gameId).Msg("Found a Wordle")
        persistScore(ctx, m, s, account, gameId, guesses)

If you want to tell me what game your trying to reuse this bot for, and show me a RegEx you would use to detect the content then I can start to help you bit by bit.

tearjeong commented 2 years ago

Wow thank you for taking the time to type out all of that!

I'm actually trying to use that bot for two games one named SUTOM and the other one TUSMO (I guess I'd need to use something like

if any(x in message.content for x in ["SUTOM", "TUSMO"]):

the RegEx would look like:

SUTOM (?P<game_id>\d+)\s(?P<guesses>\d+|%s)/6[\%s]?\n

IMG_6630

Unfortunately for TUSMO it contains a french flag emoji at the beginning, idk if that changes anything IMG_6631