Replace ioutil.ReadFile with os.ReadFile, fix chat set [[:space:]] in regexp

This commit is contained in:
eleksir
2023-06-14 01:08:24 +03:00
parent 747ff1454e
commit ac243bf4fd
4 changed files with 10 additions and 13 deletions

View File

@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
@@ -131,7 +130,7 @@ func ReadConfig() MyConfig {
continue
}
buf, err := ioutil.ReadFile(location)
buf, err := os.ReadFile(location)
// Не удалось прочитать, попробуем следующего кандидата
if err != nil {

View File

@@ -388,32 +388,32 @@ func msgParser(ctx context.Context, msg string) {
j.Message = fortune()
} else if (cmd == "karma") || (cmd == "карма") {
j.Message = getKarma(j.ChatID, "")
} else if match, _ := regexp.MatchString("^(rum|ром)[::space::]?([::space::].+)*$", cmd); match {
} else if match, _ := regexp.MatchString("^(rum|ром)[[:space:]]?([[:space:]].+)*$", cmd); match {
format := "/me притаскивает на подносе стопку рома для %s, края стопки искрятся кристаллами соли."
j.Message = fmt.Sprintf(format, j.Misc.Username)
} else if match, _ := regexp.MatchString("^(vodka|водка)[::space::]?([::space::].+)*$", cmd); match {
} else if match, _ := regexp.MatchString("^(vodka|водка)[[:space:]]?([[:space:]].+)*$", cmd); match {
format := "/me подаёт шот водки с небольшим маринованным огурчиком на блюдце для %s. Из огурчика торчит "
format += "небольшая вилочка."
j.Message = fmt.Sprintf(format, j.Misc.Username)
} else if match, _ := regexp.MatchString("^(beer|пиво)[::space::]?([::space::].+)*$", cmd); match {
} else if match, _ := regexp.MatchString("^(beer|пиво)[[:space:]]?([[:space:]].+)*$", cmd); match {
format := "/me бахает об стол перед %s кружкой холодного пива, часть пенной шапки сползает по запотевшей "
format += "стенке кружки."
j.Message = fmt.Sprintf(format, j.Misc.Username)
} else if match, _ := regexp.MatchString("^(tequila|текила)[::space::]?([::space::].+)*$", cmd); match {
} else if match, _ := regexp.MatchString("^(tequila|текила)[[:space:]]?([[:space:]].+)*$", cmd); match {
format := "/me ставит рядом с %s шот текилы, аккуратно на ребро стопки насаживает дольку лайма и ставит "
format += "кофейное блюдце с горочкой соли."
j.Message = fmt.Sprintf(format, j.Misc.Username)
} else if match, _ := regexp.MatchString("^(whisky|виски)[::space::]?([::space::].+)*$", cmd); match {
} else if match, _ := regexp.MatchString("^(whisky|виски)[[:space:]]?([[:space:]].+)*$", cmd); match {
format := "/me демонстративно достаёт из морозилки пару кубических камушков, бросает их в толстодонный "
format += "стакан и аккуратно наливает Jack Daniels. Запускает стакан вдоль барной стойки, он "
format += "останавливается около %s."
j.Message = fmt.Sprintf(format, j.Misc.Username)
} else if match, _ := regexp.MatchString("^(absinth|абсент)[::space::]?([::space::].+)*$", cmd); match {
} else if match, _ := regexp.MatchString("^(absinth|абсент)[[:space:]]?([[:space:]].+)*$", cmd); match {
format := "/me наливает абсент в стопку. Смочив кубик сахара в абсенте кладёт его на дырявую ложечку и "
format += "поджигает. Как только пламя потухнет, %s размешивает оплавившийся кубик в абсенте и подносит "
format += "стопку %s."
j.Message = fmt.Sprintf(format, j.Misc.BotNick, j.Misc.Username)
} else if match, _ := regexp.MatchString("^fuck[::space::]*$", cmd); match {
} else if match, _ := regexp.MatchString("^fuck[[:space:]]*$", cmd); match {
j.Message = "Не ругайся."
} else {
cmdLen := len(cmd)

View File

@@ -2,7 +2,6 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
@@ -50,7 +49,7 @@ func main() {
// "Заглатывать" файл целиком - плохая практика, но мы предополагаем, что файлы будут небольшие.
fortuneFile := fmt.Sprintf("%s/%s", srcDir, file.Name())
bytesRead, err := ioutil.ReadFile(fortuneFile)
bytesRead, err := os.ReadFile(fortuneFile)
if err != nil {
log.Errorf("Unable to read %s: %s\n", file.Name(), err)

View File

@@ -2,7 +2,6 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
@@ -80,7 +79,7 @@ func main() {
}
file := config.DataDir + "/friday_src/friday.txt"
bytesRead, err := ioutil.ReadFile(file)
bytesRead, err := os.ReadFile(file)
if err != nil {
log.Errorf("Unable to read %s: %s\n", file, err)