使用wego.com打包使Discord weatherBot

时间:2018-12-13 16:16:51

标签: go discord

我正在尝试熟悉Goland,我想用它制作我的第一个机器人Discord。 我正在制造一个不和谐的机器人,我正在尝试使用使用天气预报API的wego软件包发出天气命令,这是该软件包以及自述文件的屏幕截图:here

这是我的代码:

package main

import (
    "flag"
    "fmt"
    "github.com/bwmarrin/discordgo"
    "github.com/schachmat/ingo"
    _ "github.com/schachmat/wego/backends"
    _ "github.com/schachmat/wego/frontends"
    "github.com/schachmat/wego/iface"
    "log"
    "os"
    "sort"
    "strconv"
    "strings"
)

var (
    commandPrefix string
    botID         string
)

func pluginLists() {
    bEnds := make([]string, 0, len(iface.AllBackends))
    for name := range iface.AllBackends {
        bEnds = append(bEnds, name)
    }
    sort.Strings(bEnds)

fEnds := make([]string, 0, len(iface.AllFrontends))
for name := range iface.AllFrontends {
    fEnds = append(fEnds, name)
}
sort.Strings(fEnds)

fmt.Fprintln(os.Stderr, "Available backends:", strings.Join(bEnds, ", "))
fmt.Fprintln(os.Stderr, "Available frontends:", strings.Join(fEnds, ", "))
}

func main() {
    discord, err := discordgo.New("Bot My_Api_Key")
    errCheck("error creating discord session", err)
    user, err := discord.User("@me")
    errCheck("error retrieving account", err)

    botID = user.ID
    discord.AddHandler(commandHandler)
    discord.AddHandler(func(discord *discordgo.Session, ready *discordgo.Ready) {
        err = discord.UpdateStatus(0, "A friendly helpful bot!")
        if err != nil {
            fmt.Println("Error attempting to set my status")
        }
        servers := discord.State.Guilds
        fmt.Printf("GoBot has started on %d servers", len(servers))
    })

    err = discord.Open()
    errCheck("Error opening connection to Discord", err)
    defer discord.Close()

    commandPrefix = "!"

    <-make(chan struct{})

    for _, be := range iface.AllBackends {
        be.Setup()
    }
    for _, fe := range iface.AllFrontends {
        fe.Setup()
    }

    // initialize global flags and default config
    location := flag.String("location", "48.839661,2.375300", "`LOCATION` to be queried")
    flag.StringVar(location, "l", "48.839661,2.375300", "`LOCATION` to be queried (shorthand)")
    numdays := flag.Int("days", 1, "`NUMBER` of days of weather forecast to be displayed")
    flag.IntVar(numdays, "d", 1, "`NUMBER` of days of weather forecast to be displayed (shorthand)")
    unitSystem := flag.String("units", "metric", "`UNITSYSTEM` to use for output.\n    \tChoices are: metric, imperial, si, metric-ms")
    flag.StringVar(unitSystem, "u", "metric", "`UNITSYSTEM` to use for output. (shorthand)\n    \tChoices are: metric, imperial, si, metric-ms")
    selectedBackend := flag.String("backend", "forecast.io", "`BACKEND` to be used")
    flag.StringVar(selectedBackend, "b", "forecast.io", "`BACKEND` to be used (shorthand)")
    selectedFrontend := flag.String("frontend", "ascii-art-table", "`FRONTEND` to be used")
    flag.StringVar(selectedFrontend, "f", "ascii-art-table", "`FRONTEND` to be used (shorthand)")

    // print out a list of all backends and frontends in the usage
    tmpUsage := flag.Usage
    flag.Usage = func() {
        tmpUsage()
        pluginLists()
    }

    // read/write config and parse flags
    if err := ingo.Parse("wego"); err != nil {
        log.Fatalf("Error parsing config: %v", err)
    }

    // non-flag shortcut arguments overwrite possible flag arguments
    for _, arg := range flag.Args() {
        if v, err := strconv.Atoi(arg); err == nil && len(arg) == 1 {
            *numdays = v
        } else {
            *location = arg
        }
    }

    // get selected backend and fetch the weather data from it
    be, ok := iface.AllBackends[*selectedBackend]
    if !ok {
        log.Fatalf("Could not find selected backend \"%s\"", *selectedBackend)
    }
    r := be.Fetch(*location, *numdays)

    // set unit system
    unit := iface.UnitsMetric
    if *unitSystem == "imperial" {
        unit = iface.UnitsImperial
    } else if *unitSystem == "si" {
        unit = iface.UnitsSi
    } else if *unitSystem == "metric-ms" {
        unit = iface.UnitsMetricMs
    }

    // get selected frontend and render the weather data with it
    fe, ok := iface.AllFrontends[*selectedFrontend]
    if !ok {
        log.Fatalf("Could not find selected frontend \"%s\"", *selectedFrontend)
    }
    fe.Render(r, unit)

}

func errCheck(msg string, err error) {
    if err != nil {
        fmt.Printf("%s: %+v", msg, err)
        panic(err)
    }
}

func commandHandler(discord *discordgo.Session, message *discordgo.MessageCreate) {
    user := message.Author
    if user.ID == botID || user.Bot {
        //Do nothing because the bot is talking
        return
    }

    if message.Content == "test" {
        discord.ChannelMessageSend(message.ChannelID, "test")
    }

    fmt.Printf("Message: %+v || From: %s\n", message.Message, message.Author)
}

因此,此操作不执行任何操作,仅显示用户在频道中编写的消息。我看到航站楼wego上的wego打印位于src / github.com / schachmat / frontends / emoji.go,但我不知道如何在我的机器人上使用它,我想在用户键入“!天气”。 我迷路了,不知道该怎么办。 对不起英语,我已经尽力了,这是我在StackOverflow上的第一篇文章:)

0 个答案:

没有答案