Skip to content

Commit

Permalink
Merge pull request #92 from Azuki-bar/delete-msg-cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Azuki-bar authored Aug 11, 2022
2 parents db41406 + 81da8e7 commit 87a0c0d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 863 deletions.
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

0 comments on commit 87a0c0d

Please sign in to comment.