Skip to content

Commit

Permalink
wip: discord bot
Browse files Browse the repository at this point in the history
  • Loading branch information
sb-child committed Aug 17, 2023
1 parent 625b731 commit 9c6a8aa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
with:
tag_name: ${{ github.ref }}
name: Oh My Duo - Release ${{ github.ref }}
generate_release_notes: true
draft: false
prerelease: false

Expand Down
35 changes: 32 additions & 3 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"time"

"github.com/bwmarrin/discordgo"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd"
Expand All @@ -17,6 +18,7 @@ import (
)

var tgBotLock = make(chan struct{})
var dcBotLock = make(chan struct{})

func StartHttpServer(ctx context.Context) {
s := g.Server("ohmyduo-http")
Expand All @@ -30,8 +32,8 @@ func StartHttpServer(ctx context.Context) {
}

func StartTelegramServer(ctx context.Context, token string) {
tgBotTimeout := g.Config().MustGet(ctx, "ohmyduo.telegramBotHttpTimeout", 5000).Int()
tgBotProxy := g.Config().MustGet(ctx, "ohmyduo.telegramBotHttpProxy", "").String()
tgBotTimeout := g.Config().MustGet(ctx, "ohmyduo.telegramBotTimeout", 5000).Int()
tgBotProxy := g.Config().MustGet(ctx, "ohmyduo.telegramBotProxy", "").String()
c := g.Client()
c.SetTimeout(time.Duration(tgBotTimeout) * time.Millisecond)
c.SetProxy(tgBotProxy)
Expand All @@ -48,22 +50,49 @@ func StartTelegramServer(ctx context.Context, token string) {
}(ctx, bot)
}

func StartDiscordServer(ctx context.Context, token string) {
dcBotTimeout := g.Config().MustGet(ctx, "ohmyduo.discordBotTimeout", 20000).Int()
dcBotProxy := g.Config().MustGet(ctx, "ohmyduo.discordBotProxy", "").String()
bot, err := discordgo.New("Bot " + token)
c := g.Client()
c.SetTimeout(time.Duration(dcBotTimeout) * time.Millisecond)
c.SetProxy(dcBotProxy)
bot.Client = &c.Client
if err != nil {
g.Log().Fatal(ctx, "Discord bot start failed: "+err.Error())
// todo: auto restart
dcBotLock <- struct{}{}
}
go func(ctx context.Context, x *discordgo.Session) {
DiscordProcess(ctx, x)
}(ctx, bot)
}

func MainProcess(ctx context.Context, parser *gcmd.Parser) (err error) {
httpServer := g.Config().MustGet(ctx, "ohmyduo.httpServer", false).Bool()
tgBotToken := g.Config().MustGet(ctx, "ohmyduo.telegramBotToken", "").String()
dcBotToken := g.Config().MustGet(ctx, "ohmyduo.discordBotToken", "").String()
tgCtx, tgCancel := context.WithCancel(ctx)
dcCtx, dcCancel := context.WithCancel(ctx)
service.MyDuo().Init(ctx)
if httpServer {
StartHttpServer(ctx)
}
if len(tgBotToken) > 0 {
StartTelegramServer(tgCtx, tgBotToken)
}
if len(dcBotToken) > 0 {
StartDiscordServer(dcCtx, dcBotToken)
}
gproc.AddSigHandlerShutdown(func(sig os.Signal) {
g.Log().Infof(ctx, "%s Signal received, stopping service...", sig.String())
if len(tgBotToken) > 0 {
tgCancel()
<- tgBotLock
<-tgBotLock
}
if len(dcBotToken) > 0 {
dcCancel()
<-dcBotLock
}
if httpServer {
s := g.Server("ohmyduo-http")
Expand Down
18 changes: 17 additions & 1 deletion internal/cmd/discord_handler.go
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
package cmd
package cmd

import (
"context"

"github.com/bwmarrin/discordgo"
"github.com/gogf/gf/v2/frame/g"
)

func DiscordProcess(ctx context.Context, bot *discordgo.Session) {
g.Log().Warning(ctx, "Discord bot started.")
bot.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {

})()
g.Log().Warning(ctx, "Discord bot stopped.")
dcBotLock <- struct{}{}
}
1 change: 1 addition & 0 deletions internal/cmd/tg_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TelegramProcess(ctx context.Context, bot *tgbot.Bot) {
u, err := bot.GetMe(ctx)
if err != nil {
g.Log().Warning(ctx, "Failed to get telegram bot info: ", err.Error())
// todo: imporve it
}
g.Log().Infof(ctx, "Telegram bot: @%s [%d]", u.Username, u.ID)
bot.Start(ctx)
Expand Down
12 changes: 8 additions & 4 deletions manifest/config/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ server:
# ----------------------------------------------------------------
# oh-my-duo 设置
ohmyduo:
httpServer: true
rootDir: "/ohmyduo" # OhMyDuo 服务器 URL 地址前缀

telegramBotToken: "YOUR_BOT_TOKEN_FROM_BOTFATHER"
telegramBotHttpTimeout: 10000 # 1000ms = 1s
telegramBotHttpProxy: "" # support http:// and socks5:// currently
telegramBotTimeout: 10000 # 1000ms = 1s
telegramBotProxy: "" # support http:// and socks5:// currently
telegramBotImageServer: "https://xxxxxx.xxxx/ohmyduo" # your OhMyDuo server or others...

discordBotToken: "YOUR_DISCORD_BOT_ACCESS_TOKEN"
discordBotGuild: "" # Test guild ID. If not passed - bot registers commands globally
discordBotTimeout: 20000 # 1000ms = 1s
discordBotProxy: "" # support http:// and socks5:// currently
discordBotCleanup: false # Remove all commands after shutdowning or not
httpServer: true
rootDir: "/ohmyduo" # OhMyDuo 服务器 URL 地址前缀

0 comments on commit 9c6a8aa

Please sign in to comment.