1. golang 接入Discord做消息推送

discord

discord 是非常优秀的聊天工具,游戏开黑还有非常多的开发组也在使用discord作为沟通聊天工具

第三方库:github.com/andersfylling/disgord

package main

import (
    "github.com/go-redis/redis/v7"
    "github.com/zhaojunlike/logger"
    "os"
    "zhaojunlike/cacher"
    "zhaojunlike/snkrs/discord"
)

var rds = cacher.NewGtCache(&redis.Options{
    Addr:     "",
    Password: xx",
    DB:       0,
})

func init() {
    _ = logger.SetLogger(`
    {   
        "File": {                 
            "filename": "./logs/discord/app.log", 
            "level": "TRAC",        
            "daily": true,        
            "maxlines": -1,   
            "maxsize": 1,         
            "maxdays": -1,      
            "append": true,    
            "permit": "0660"  
        }
    }
`)
}

func main() {
    config, err := discord.GetConfigFromConfigFile("./conf/config.discord.json")
    if err != nil {
        panic(err)
    }
    var opt = &discord.DisOption{
        Brand:    config.Brand,
        Version:  config.Version,
        BotToken: config.BotToken, //dev
        Proxy:    config.Proxy,
        Rds:      rds,
    }
    var bot = discord.NewDisBot(opt)
    defer bot.Destroy()
    err = bot.Start()
    if err != nil {
        logger.Error("Start 失败:", err)
        os.Exit(-1)
    }
    logger.Crit("Discord Notice Bot 启动成功,版本:%v", config.Version)
    _ = bot.GetClient().DisconnectOnInterrupt() //接受退出指令
}

Discrod 包装结构体

type DisBot struct {
    opt    *DisOption
    client *disgord.Client
    rds    *cacher.GtCache
}

func NewDisBot(opt *DisOption) *DisBot {
    var bot = &DisBot{opt: opt}
    var conf = disgord.Config{
        BotToken: opt.BotToken,
        Logger:   disgord.DefaultLogger(false), // debug=false
    }
    if opt.Proxy != "" {
        conf.Proxy, _ = proxy.SOCKS5("tcp", opt.Proxy, nil, nil)
    }
    bot.client = disgord.New(conf)
    bot.rds = opt.Rds
    return bot
}
func (bot *DisBot) Start() error {
    var err = bot.client.Connect(context.Background())
    if err != nil {
        return err
    }
    go bot.listenPublish()
    bot.client.On(disgord.EvtMessageCreate, bot.printMessage)
    bot.client.On(disgord.EvtReady, func() {

    })
    return nil
}

你可能感兴趣的:(笔记)