mirror-nourybot/cmd/bot/command.go

383 lines
9.9 KiB
Go
Raw Normal View History

2022-08-07 01:34:31 +02:00
package main
2022-06-21 00:31:17 +02:00
import (
"strings"
"github.com/gempir/go-twitch-irc/v3"
2022-08-07 01:34:31 +02:00
"github.com/lyx0/nourybot/pkg/commands"
2022-08-07 02:27:40 +02:00
"github.com/lyx0/nourybot/pkg/common"
2022-06-21 00:31:17 +02:00
)
2022-08-11 22:15:30 +02:00
// handleCommand takes in a twitch.PrivateMessage and then routes the message to
// the function that is responsible for each command and knows how to deal with it accordingly.
func (app *Application) handleCommand(message twitch.PrivateMessage) {
2022-08-07 03:09:09 +02:00
2022-08-11 22:15:30 +02:00
// Increments the counter how many commands have been used, called in the ping command.
2022-08-07 17:24:47 +02:00
common.CommandUsed()
2022-06-21 00:31:17 +02:00
// commandName is the actual name of the command without the prefix.
// e.g. `()ping` would be `ping`.
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
// cmdParams are additional command parameters.
// e.g. `()weather san antonio`
// cmdParam[0] is `san` and cmdParam[1] = `antonio`.
//
// Since Twitch messages are at most 500 characters I use a
// maximum count of 500+10 just to be safe.
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
2022-08-07 02:27:40 +02:00
cmdParams := strings.SplitN(message.Message, " ", 500)
2022-06-21 00:31:17 +02:00
// msgLen is the amount of words in a message without the prefix.
// Useful to check if enough cmdParams are provided.
msgLen := len(strings.SplitN(message.Message, " ", -2))
// target is the channelname the message originated from and
2022-08-12 16:59:58 +02:00
// where the TwitchClient should send the response
2022-06-21 00:31:17 +02:00
target := message.Channel
// Userlevel is the level set for a user in the database.
// It is NOT same as twitch user/mod.
// 1000 = admin
// 500 = mod
2022-08-11 22:15:30 +02:00
// 250 = vip
// 100 = normal
// If the level returned is 0 then the user was not found in the database.
userLevel := app.GetUserLevel(message.User.Name)
2022-08-11 22:15:30 +02:00
app.Logger.Infow("Command received",
2022-08-12 00:08:39 +02:00
// "message", message, // Pretty taxing
2022-08-11 23:34:00 +02:00
"message.Message", message.Message,
2022-08-07 04:35:03 +02:00
"message.Channel", target,
2022-08-12 00:08:39 +02:00
"userLevel", userLevel,
2022-08-07 03:17:29 +02:00
"commandName", commandName,
"cmdParams", cmdParams,
"msgLen", msgLen,
)
2022-08-11 22:15:30 +02:00
// A `commandName` is every message starting with `()`.
// Hardcoded commands have a priority over database commands.
// Switch over the commandName and see if there is a hardcoded case for it.
// If there was no switch case satisfied, query the database if there is
2022-08-12 16:59:58 +02:00
// a data.CommandModel.Name equal to the `commandName`
// If there is return the data.CommandModel.Text entry.
2022-08-11 22:15:30 +02:00
// Otherwise we ignore the message.
2022-06-21 00:31:17 +02:00
switch commandName {
case "":
if msgLen == 1 {
2022-08-08 23:12:50 +02:00
common.Send(target, "xd", app.TwitchClient)
2022-06-21 00:31:17 +02:00
return
}
2022-08-11 21:49:19 +02:00
// ()bttv <emote name>
2022-08-07 21:43:40 +02:00
case "bttv":
if msgLen < 2 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Not enough arguments provided. Usage: ()bttv <emote name>", app.TwitchClient)
2022-08-07 21:43:40 +02:00
return
} else {
2022-08-08 23:12:50 +02:00
commands.Bttv(target, cmdParams[1], app.TwitchClient)
2022-08-07 21:43:40 +02:00
return
}
2022-08-11 21:49:19 +02:00
// Coinflip
2022-08-08 13:17:42 +02:00
case "coin":
2022-08-08 23:12:50 +02:00
commands.Coinflip(target, app.TwitchClient)
2022-08-08 13:17:42 +02:00
return
case "coinflip":
2022-08-08 23:12:50 +02:00
commands.Coinflip(target, app.TwitchClient)
2022-08-08 13:17:42 +02:00
return
case "cf":
2022-08-08 23:12:50 +02:00
commands.Coinflip(target, app.TwitchClient)
2022-08-08 13:17:42 +02:00
return
2022-08-11 21:49:19 +02:00
// ()currency <amount> <input currency> to <output currency>
2022-08-07 17:24:47 +02:00
case "currency":
if msgLen < 4 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Not enough arguments provided. Usage: ()currency 10 USD to EUR", app.TwitchClient)
2022-08-07 17:24:47 +02:00
return
}
2022-08-08 23:12:50 +02:00
commands.Currency(target, cmdParams[1], cmdParams[2], cmdParams[4], app.TwitchClient)
2022-08-07 21:43:40 +02:00
return
2022-08-08 16:43:21 +02:00
2022-08-11 21:49:19 +02:00
// ()ffz <emote name>
2022-08-07 21:43:40 +02:00
case "ffz":
if msgLen < 2 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Not enough arguments provided. Usage: ()ffz <emote name>", app.TwitchClient)
2022-08-07 21:43:40 +02:00
return
} else {
2022-08-08 23:12:50 +02:00
commands.Ffz(target, cmdParams[1], app.TwitchClient)
2022-08-07 21:43:40 +02:00
return
}
2022-08-08 16:43:21 +02:00
2022-08-11 21:49:19 +02:00
// ()followage <channel> <username>
2022-08-08 16:43:21 +02:00
case "followage":
if msgLen == 1 { // ()followage
2022-08-08 23:12:50 +02:00
commands.Followage(target, target, message.User.Name, app.TwitchClient)
2022-08-08 16:43:21 +02:00
return
} else if msgLen == 2 { // ()followage forsen
2022-08-08 23:12:50 +02:00
commands.Followage(target, target, cmdParams[1], app.TwitchClient)
2022-08-08 16:43:21 +02:00
return
} else { // ()followage forsen pajlada
2022-08-08 23:12:50 +02:00
commands.Followage(target, cmdParams[1], cmdParams[2], app.TwitchClient)
2022-08-08 16:43:21 +02:00
return
}
2022-08-11 21:49:19 +02:00
2022-08-08 16:43:21 +02:00
// First Line
2022-08-11 21:49:19 +02:00
// ()firstline <channel> <username>
2022-08-08 16:43:21 +02:00
case "firstline":
if msgLen == 1 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Usage: ()firstline <channel> <user>", app.TwitchClient)
2022-08-08 16:43:21 +02:00
return
} else if msgLen == 2 {
2022-08-08 23:12:50 +02:00
commands.FirstLine(target, target, cmdParams[1], app.TwitchClient)
2022-08-08 16:43:21 +02:00
return
} else {
2022-08-08 23:12:50 +02:00
commands.FirstLine(target, cmdParams[1], cmdParams[2], app.TwitchClient)
2022-08-08 16:43:21 +02:00
return
}
2022-08-11 21:49:19 +02:00
// ()fl <channel> <username>
2022-08-08 16:43:21 +02:00
case "fl":
if msgLen == 1 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Usage: ()firstline <channel> <user>", app.TwitchClient)
2022-08-08 16:43:21 +02:00
return
} else if msgLen == 2 {
2022-08-08 23:12:50 +02:00
commands.FirstLine(target, target, cmdParams[1], app.TwitchClient)
2022-08-08 16:43:21 +02:00
return
} else {
2022-08-08 23:12:50 +02:00
commands.FirstLine(target, cmdParams[1], cmdParams[2], app.TwitchClient)
2022-08-08 16:43:21 +02:00
return
}
2022-08-12 16:59:58 +02:00
// ()pinG
2022-08-07 21:43:40 +02:00
case "ping":
2022-08-08 23:12:50 +02:00
commands.Ping(target, app.TwitchClient)
2022-08-07 21:43:40 +02:00
return
2022-08-11 21:49:19 +02:00
// Thumbnail
// ()preview <live channel>
2022-08-07 21:43:40 +02:00
case "preview":
if msgLen < 2 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Not enough arguments provided. Usage: ()preview <username>", app.TwitchClient)
2022-08-07 18:10:40 +02:00
return
} else {
2022-08-08 23:12:50 +02:00
commands.Preview(target, cmdParams[1], app.TwitchClient)
2022-08-07 21:43:40 +02:00
return
}
2022-08-11 21:49:19 +02:00
// ()thumbnail <live channel>
2022-08-08 16:43:21 +02:00
case "thumbnail":
2022-08-08 13:45:37 +02:00
if msgLen < 2 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Not enough arguments provided. Usage: ()thumbnail <username>", app.TwitchClient)
2022-08-08 13:45:37 +02:00
return
} else {
2022-08-08 23:12:50 +02:00
commands.Preview(target, cmdParams[1], app.TwitchClient)
2022-08-08 13:45:37 +02:00
return
}
2022-08-08 16:43:21 +02:00
// SevenTV
2022-08-11 21:49:19 +02:00
// ()seventv <emote name>
2022-08-08 16:43:21 +02:00
case "seventv":
2022-08-08 13:45:37 +02:00
if msgLen < 2 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Not enough arguments provided. Usage: ()seventv <emote name>", app.TwitchClient)
2022-08-08 13:45:37 +02:00
return
} else {
2022-08-08 23:12:50 +02:00
commands.Seventv(target, cmdParams[1], app.TwitchClient)
2022-08-08 13:45:37 +02:00
return
}
2022-08-11 21:49:19 +02:00
// ()7tv <emote name>
2022-08-08 16:43:21 +02:00
case "7tv":
2022-08-07 21:43:40 +02:00
if msgLen < 2 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Not enough arguments provided. Usage: ()seventv <emote name>", app.TwitchClient)
2022-08-07 21:43:40 +02:00
return
} else {
2022-08-08 23:12:50 +02:00
commands.Seventv(target, cmdParams[1], app.TwitchClient)
2022-08-07 18:10:40 +02:00
return
}
2022-08-11 21:49:19 +02:00
// ()tweet <username>
2022-08-07 16:08:30 +02:00
case "tweet":
if msgLen < 2 {
2022-08-08 23:12:50 +02:00
common.Send(target, "Not enough arguments provided. Usage: ()tweet <username>", app.TwitchClient)
2022-08-07 16:08:30 +02:00
return
} else {
2022-08-08 23:12:50 +02:00
commands.Tweet(target, cmdParams[1], app.TwitchClient)
return
2022-08-07 16:08:30 +02:00
}
2022-08-11 21:49:19 +02:00
// Xkcd
2022-08-08 13:45:37 +02:00
case "rxkcd":
2022-08-08 23:12:50 +02:00
commands.RandomXkcd(target, app.TwitchClient)
2022-08-08 13:45:37 +02:00
return
case "randomxkcd":
2022-08-08 23:12:50 +02:00
commands.RandomXkcd(target, app.TwitchClient)
2022-08-08 13:45:37 +02:00
return
case "xkcd":
2022-08-08 23:12:50 +02:00
commands.Xkcd(target, app.TwitchClient)
2022-08-08 13:45:37 +02:00
return
2022-08-11 21:49:19 +02:00
// Commands with permission level or database from here on
//#################
// 250 - VIP only
//#################
case "debug":
if userLevel < 250 {
return
} else if msgLen < 3 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else if cmdParams[1] == "user" {
app.DebugUser(cmdParams[2], message)
return
} else {
return
}
2022-08-12 16:59:58 +02:00
// ()echo <message>
2022-08-11 21:49:19 +02:00
case "echo":
if userLevel < 250 { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
commands.Echo(target, message.Message[7:len(message.Message)], app.TwitchClient)
return
}
//###################
2022-08-12 16:59:58 +02:00
// 1000 - Admin only
2022-08-11 21:49:19 +02:00
//###################
// #####
// Add
// #####
case "addchannel":
2022-08-11 22:15:30 +02:00
if userLevel < 1000 {
2022-08-11 21:49:19 +02:00
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
// ()addchannel noemience
app.AddChannel(cmdParams[1], message)
return
}
case "addcommand":
2022-08-11 22:15:30 +02:00
if userLevel < 1000 {
2022-08-11 21:49:19 +02:00
return
} else if msgLen < 3 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
// ()addcommand dank FeelsDankMan xD
app.AddCommand(cmdParams[1], message)
return
}
case "adduser":
2022-08-11 22:15:30 +02:00
if userLevel < 1000 {
2022-08-11 21:49:19 +02:00
return
} else if msgLen < 3 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
// ()adduser nourylul 1000
app.AddUser(cmdParams[1], cmdParams[2], message)
return
}
// ######
// Edit
// ######
2022-08-11 22:15:30 +02:00
case "edituser":
2022-08-11 21:49:19 +02:00
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 4 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else if cmdParams[1] == "level" {
2022-08-11 22:15:30 +02:00
// ()edituser level nourylul 1000
2022-08-11 21:49:19 +02:00
app.EditUserLevel(cmdParams[2], cmdParams[3], message)
return
} else {
return
}
case "editcommand": // ()editcommand level nourylul 1000
if userLevel < 1000 {
return
} else if msgLen < 4 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else if cmdParams[1] == "level" {
app.EditCommandLevel(cmdParams[2], cmdParams[3], message)
return
} else {
return
}
2022-08-11 22:15:30 +02:00
2022-08-11 21:49:19 +02:00
// ########
// Delete
// ########
case "deletechannel":
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
// ()deletechannel noemience
app.DeleteChannel(cmdParams[1], message)
return
}
case "deletecommand":
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
2022-08-11 22:15:30 +02:00
// ()deletecommand dank
2022-08-11 21:49:19 +02:00
app.DeleteCommand(cmdParams[1], message)
return
}
case "deleteuser":
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
2022-08-11 22:15:30 +02:00
// ()deleteuser noemience
2022-08-11 21:49:19 +02:00
app.DeleteUser(cmdParams[1], message)
return
}
2022-08-11 22:15:30 +02:00
case "bttvemotes":
2022-08-11 21:49:19 +02:00
if userLevel < 1000 {
commands.Bttvemotes(target, app.TwitchClient)
return
} else {
return
}
2022-08-11 22:15:30 +02:00
case "ffzemotes":
2022-08-11 21:49:19 +02:00
if userLevel < 1000 {
commands.Ffzemotes(target, app.TwitchClient)
return
} else {
return
}
// ##################
// Check if the commandName exists as the "name" of a command in the database.
// if it doesnt then ignore it.
2022-08-11 21:49:19 +02:00
// ##################
default:
reply, err := app.GetCommand(commandName, message.User.Name)
if err != nil {
return
}
common.SendNoLimit(message.Channel, reply, app.TwitchClient)
return
2022-06-21 00:31:17 +02:00
}
}