~xenrox/twitch-bot

b6898b63efea86d670844a28f29a7a8860787e54 — Thorben Günther 1 year, 6 months ago 2304c62
static_commands: Add commands list

Preconfigured commands are still missing from this list.
1 files changed, 40 insertions(+), 5 deletions(-)

M static_commands.go
M static_commands.go => static_commands.go +40 -5
@@ 3,6 3,7 @@ package main
import (
	"database/sql"
	"errors"
	"fmt"
	"log"
	"strings"



@@ 12,15 13,49 @@ import (
var errTooShort = errors.New("input too short")

func (b *bot) handleCommands(parsed []string, msg *twitch.PrivateMessage) error {
	if !b.isAuthorized(&msg.User, msg.Channel) {
		return errNotAuthorized
	}

	if len(parsed) == 1 || parsed[1] == "list" {
		// TODO: List all
		var commands []string
		// TODO: Add preconfigured commands like elo
		query := `
			SELECT name
			FROM static_commands
			WHERE channel = $1`

		rows, err := b.db.DB.Query(query, msg.Channel)
		if err != nil {
			return err
		}
		defer rows.Close()

		for rows.Next() {
			var command string
			if err := rows.Scan(&command); err != nil {
				return err
			}

			commands = append(commands, command)
		}

		if err = rows.Err(); err != nil {
			return err
		}

		var text string
		for i, command := range commands {
			text += fmt.Sprintf("!%s", command)
			if i < len(commands)-1 {
				text += ", "
			}
		}

		b.say(msg, text)
		return nil
	}

	if !b.isAuthorized(&msg.User, msg.Channel) {
		return errNotAuthorized
	}

	editCommands := [4]string{"edit", "update", "create", "add"}
	for _, cmd := range editCommands {
		if parsed[1] == cmd {