Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete user msg command start from ! #92

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package main
import (
"database/sql"
"fmt"
"github.com/azuki-bar/ceviord/pkg/joinVc"
"github.com/azuki-bar/ceviord/pkg/slashCmd"
"github.com/azuki-bar/ceviord/pkg/speech/grpc"
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/azuki-bar/ceviord/pkg/joinVc"
"github.com/azuki-bar/ceviord/pkg/slashCmd"
"github.com/azuki-bar/ceviord/pkg/speech/grpc"

"github.com/azuki-bar/ceviord/pkg/ceviord"
"github.com/azuki-bar/ceviord/pkg/replace"
"github.com/bwmarrin/discordgo"
"github.com/go-gorp/gorp"
_ "github.com/go-sql-driver/mysql"
"github.com/k0kubun/pp"
"github.com/vrischmann/envconfig"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -119,7 +119,6 @@ func main() {
if err != nil {
log.Println("slash command generate failed")
}
pp.Print(sg.Generate())
slashCmds, err := slashCmd.NewCmds(dgSess, "", sg.Generate())
defer func() {
if slashCmds != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/ceviord/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ceviord

const prefix = "!"
const strLenMax = 300
const DiscordPostLenLimit = 2000
53 changes: 9 additions & 44 deletions pkg/ceviord/handleMsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c Channel) IsActorJoined(sess *discordgo.Session) (bool, error) {
}

type Channels map[string]*Channel

func (cs Channels) AddChannel(c Channel, guildId string) {
if _, ok := cs[guildId]; !ok {
c.CurrentParam = &Cache.Param.Parameters[0]
Expand Down Expand Up @@ -115,9 +116,6 @@ type CevioWav interface {
ApplyEmotions(param *Parameter) (err error)
}

const prefix = "!"
const strLenMax = 300

var tmpDir = filepath.Join(os.TempDir(), "ceviord")

var Cache = Ceviord{
Expand Down Expand Up @@ -151,34 +149,6 @@ func FindJoinedVC(s *discordgo.Session, guildId, authorId string) *discordgo.Cha
return nil
}

func parseUserCmd(msg string) (userMainCmd, error) {
rawCmd := regexp.MustCompile(`[\s ]+`).Split(msg, -1)
if len(rawCmd) < 1 {
return nil, fmt.Errorf("parsing user cmd failed. user msg is `%s`\n", msg)
}
var mainCmd userMainCmd
switch rawCmd[0] {
case "sasara":
mainCmd = new(sasaraOld)
case "bye":
mainCmd = new(byeOld)
case "dict":
mainCmd = new(dictOld)
case "change":
mainCmd = new(changeOld)
case "help", "man":
mainCmd = new(helpOld)
case "ping":
mainCmd = new(pingOld)
default:
return nil, fmt.Errorf("unknown user cmd `%s` \n", rawCmd[0])
}
if err := mainCmd.parse(rawCmd); err != nil {
return nil, err
}
return mainCmd, nil
}

// MessageCreate will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to.
func MessageCreate(sess *discordgo.Session, msg *discordgo.MessageCreate) {
Expand Down Expand Up @@ -218,17 +188,6 @@ func MessageCreate(sess *discordgo.Session, msg *discordgo.MessageCreate) {
return
}
}
if cev != nil { // already establish connection
cev.DictController.SetGuildId(msg.GuildID)
}
cmd, err := parseUserCmd(strings.TrimPrefix(msg.Content, prefix))
if err != nil {
Logger.Log(logging.DEBUG, fmt.Errorf("error occured in user cmd parser `%w`", err))
return
}
if err = cmd.handle(sess, msg); err != nil {
Logger.Log(logging.WARN, fmt.Errorf("error occured in cmd handler %T; `%w`", cmd, err))
}
}

func RawSpeak(text string, guildId string, sess *discordgo.Session) error {
Expand Down Expand Up @@ -282,7 +241,7 @@ func SendMsg(msg string, session *discordgo.Session, guildId string) error {
return err
}
// https://discord.com/developers/docs/resources/channel#create-message-jsonform-params
if len([]rune(msg)) > 2000 {
if len([]rune(msg)) > DiscordPostLenLimit {
return fmt.Errorf("discord message send limitation error")
} else if len([]rune(msg)) == 0 {
return fmt.Errorf("message len is 0")
Expand Down Expand Up @@ -338,4 +297,10 @@ func GetMsg(m *discordgo.MessageCreate, s *discordgo.Session) string {
return stringMax(rawMsg, strLenMax)
}


func stringMax(msg string, max int) string {
lenMsg := len([]rune(msg))
if lenMsg > max {
return string([]rune(msg)[0:max])
}
return msg
}
Loading