本文整理汇总了Golang中github.com/bwmarrin/discordgo.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
conn, err := common.Connect()
if err != nil {
panic(err)
}
uplink = conn
discord, err := discordgo.New(*discordUser, *discordPass)
if err != nil {
panic(err)
}
uplink.Println("Connected to discord")
discord.Open()
discord.AddHandler(messageCreate)
discord.UpdateStatus(0, "Golang message input queue")
conn.Subscribe(uplink.ID+":input", func(subj, reply string, msg *common.Message) {
switch msg.Kind {
case "TextMessage":
discord.ChannelMessageSend(msg.Destination, msg.Body)
}
})
runtime.Goexit()
}
示例2: main
func main() {
// Create a new Discord session using the provided login information.
// Use discordgo.New(Token) to just use a token for login.
dg, err := discordgo.New(Email, Password, Token)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
// Register messageCreate as a callback for the messageCreate events.
dg.AddHandler(messageCreate)
// Open the websocket and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
// Simple way to keep program running until CTRL-C is pressed.
<-make(chan struct{})
return
}
示例3: connectToDiscord
func connectToDiscord() {
log.Println("Connecting to discord")
//var err error
c := config.Get()
dg, err := discordgo.New(c.Email, c.Password, c.Token)
if err != nil {
log.Println(err.Error())
return
}
// Register messageCreate as a callback for the OnMessageCreate event.
dg.AddHandler(messageCreate)
// Retry after broken websocket
dg.ShouldReconnectOnError = true
// Open websocket connection
err = dg.Open()
if err != nil {
log.Println(err)
return
}
log.Println("Connected")
}
示例4: main
func main() {
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
// Get the account information.
u, err := dg.User("@me")
if err != nil {
fmt.Println("error obtaining account details,", err)
}
// Store the account ID for later use.
BotID = u.ID
// Register messageCreate as a callback for the messageCreate events.
dg.AddHandler(messageCreate)
// Open the websocket and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
// Simple way to keep program running until CTRL-C is pressed.
<-make(chan struct{})
return
}
示例5: main
func main() {
// Create a new Discord session using the provided login information.
// Use discordgo.New(Token) to just use a token for login.
dg, err := discordgo.New(Email, Password, Token)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
bot, err := dg.User("@me")
if err != nil {
fmt.Println("error fetching the bot details,", err)
return
}
BotID = bot.ID
BotUsername = bot.Username
changeAvatar(dg)
fmt.Println("Bot is now running. Press CTRL-C to exit.")
// Simple way to keep program running until CTRL-C is pressed.
<-make(chan struct{})
return
}
示例6: connectToDiscord
func connectToDiscord() {
log.Println("Connecting to discord")
var err error
c := config.Get()
dg, err := discordgo.New(c.Email, c.Password, "Bot "+c.Token)
if err != nil {
log.Println(err.Error())
return
}
// Register messageCreate as a callback for the OnMessageCreate event.
dg.AddHandler(messageCreate)
// Retry after broken websocket
dg.ShouldReconnectOnError = true
// Verify the Token is valid and grab user information
dg.State.User, err = dg.User("@me")
if err != nil {
log.Printf("error fetching user information, %s\n", err)
}
// Open websocket connection
err = dg.Open()
if err != nil {
log.Printf("error opening connection to Discord, %s\n", err)
return
}
log.Println("Connected")
}
示例7: main
func main() {
// Check for Username and Password CLI arguments.
if len(os.Args) != 3 {
fmt.Println("You must provide username and password as arguments. See below example.")
fmt.Println(os.Args[0], " [username] [password]")
return
}
// Call the helper function New() passing username and password command
// line arguments. This returns a new Discord session, authenticates,
// connects to the Discord data websocket, and listens for events.
dg, err := discordgo.New(os.Args[1], os.Args[2])
if err != nil {
fmt.Println(err)
return
}
// Register messageCreate as a callback for the OnMessageCreate event.
dg.OnMessageCreate = messageCreate
// Simple way to keep program running until any key press.
var input string
fmt.Scanln(&input)
return
}
示例8: main
func main() {
//Initialize Config
GetConfig()
CheckState()
State.InsertMode = false
Clear()
Header("V0.1")
// Connect to Discord
dg, err := discordgo.New(State.Username, State.Password)
if err != nil {
fmt.Println(err)
return
}
// Register messageCreate as a callback for the OnMessageCreate event.
dg.OnMessageCreate = messageCreate
// Open the websocket and begin listening.
dg.Open()
//Print Welcome as a sign that the user has logged in.
Welcome(dg)
//SetChannelState
SetGuildState(dg)
SetChannelState(dg)
//Setup stdout logging
rl, err := readline.NewEx(&readline.Config{
Prompt: "> ",
UniqueEditLine: true,
})
if err != nil {
panic(err)
}
defer rl.Close()
log.SetOutput(rl.Stderr()) // let "log" write to l.Stderr instead of os.Stderr
//Start Listening
for {
line, _ := rl.Readline()
//QUIT
if line == ":q" {
break
}
line = ParseForCommands(line, dg)
if line != "" {
dg.ChannelMessageSend(State.Channel.ID, line)
}
}
return
}
示例9: main
func main() {
// NOTE: All of the below fields are required for this example to work correctly.
var (
Email = flag.String("e", "", "Discord account email.")
Password = flag.String("p", "", "Discord account password.")
GuildID = flag.String("g", "", "Guild ID")
ChannelID = flag.String("c", "", "Channel ID")
Folder = flag.String("f", "", "Folder of files to play.")
err error
)
flag.Parse()
// Connect to Discord
discord, err := discordgo.New(*Email, *Password)
if err != nil {
fmt.Println(err)
return
}
// Open Websocket
err = discord.Open()
if err != nil {
fmt.Println(err)
return
}
// Connect to voice channel.
// NOTE: Setting mute to false, deaf to true.
err = discord.ChannelVoiceJoin(*GuildID, *ChannelID, false, true)
if err != nil {
fmt.Println(err)
return
}
// Hacky loop to prevent sending on a nil channel.
// TODO: Find a better way.
for discord.Voice.Ready == false {
runtime.Gosched()
}
// Start loop and attempt to play all files in the given folder
fmt.Println("Reading Folder: ", *Folder)
files, _ := ioutil.ReadDir(*Folder)
for _, f := range files {
fmt.Println("PlayAudioFile:", f.Name())
discord.UpdateStatus(0, f.Name())
PlayAudioFile(discord.Voice, fmt.Sprintf("%s/%s", *Folder, f.Name()))
}
// Close connections
discord.Voice.Close()
discord.Close()
return
}
示例10: main
func main() {
logInfo("Logging in...")
var err error
var session *discordgo.Session
if accountToken == "" {
logInfo("Logging in with username and password...")
session, err = discordgo.New(username, password)
} else {
logInfo("Logging in with bot account token...")
session, err = discordgo.New(accountToken)
}
setupHandlers(session)
panicOnErr(err)
logInfo("Opening session...")
err = session.Open()
panicOnErr(err)
logInfo("Sleeping...")
<-make(chan struct{})
}
示例11: main
func main() {
logrus.Info("pokemon-discord is starting up.")
// parse the command-line arguments for the Discord bot token
var (
dToken = flag.String("t", "", "Discord API token for bot usage")
enableDebug = flag.Bool("d", false, "Enables or disables debug logging (default disabled)")
err error
)
flag.Parse()
// validate command-line arguments
if *dToken == "" {
logrus.Fatal("Discord API authorization token was expected but never provided!")
return
}
if *enableDebug {
logrus.Info("Enabling debug logging as set by the command-line parameter.")
logrus.SetLevel(logrus.DebugLevel)
}
// build Discord session
discord, err = discordgo.New(*dToken)
if err != nil {
logrus.WithFields(logrus.Fields{
"capturedError": err,
}).Fatal("An error occurred while creating a Discord session.")
return
}
// add event handler functions
logrus.Info("Adding Discord event handlers to bot.")
discord.AddHandler(onReady)
discord.AddHandler(onMessageCreate)
discord.AddHandler(onGuildCreate)
discord.AddHandler(onGuildMemberAdd)
logrus.Info("Establishing Websocket connection with the Discord API.")
err = discord.Open()
if err != nil {
logrus.WithFields(logrus.Fields{
"capturedError": err,
}).Fatal("An error occurred while connecting to the Discord Websocket API.")
return
}
logrus.Info("pokemon-discord is ready.")
// wait for OS signal to shutdown
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
}
示例12: main
func main() {
// NOTE: All of the below fields are required for this example to work correctly.
var (
Email = flag.String("e", "", "Discord account email.")
Password = flag.String("p", "", "Discord account password.")
GuildID = flag.String("g", "", "Guild ID")
ChannelID = flag.String("c", "", "Channel ID")
Filename = flag.String("f", "", "Filename of file to play.")
err error
)
flag.Parse()
// Connect to Discord
discord, err := discordgo.New(*Email, *Password)
if err != nil {
fmt.Println(err)
return
}
// Connect to voice channel.
// NOTE: Setting mute to false, deaf to true.
err = discord.ChannelVoiceJoin(*GuildID, *ChannelID, false, true)
if err != nil {
fmt.Println(err)
return
}
// This will block until Voice is ready. This is not the most ideal
// way to check and shouldn't be used outside of this example.
for {
if discord.Voice.Ready {
break
}
fmt.Print(".")
time.Sleep(1 * time.Second)
}
// streams file from ffmpeg then encodes with opus and sends via UDP
// to Discord.
dgvoice.PlayAudioFile(discord, *Filename)
// Close connections
discord.Voice.Close()
discord.Close()
return
}
示例13: ExampleApplication
func ExampleApplication() {
// Authentication Token pulled from environment variable DG_TOKEN
Token := os.Getenv("DG_TOKEN")
if Token == "" {
return
}
// Create a new Discordgo session
dg, err := discordgo.New(Token)
if err != nil {
log.Println(err)
return
}
// Create an new Application
ap := &discordgo.Application{}
ap.Name = "TestApp"
ap.Description = "TestDesc"
ap, err = dg.ApplicationCreate(ap)
log.Printf("ApplicationCreate: err: %+v, app: %+v\n", err, ap)
// Get a specific Application by it's ID
ap, err = dg.Application(ap.ID)
log.Printf("Application: err: %+v, app: %+v\n", err, ap)
// Update an existing Application with new values
ap.Description = "Whooooa"
ap, err = dg.ApplicationUpdate(ap.ID, ap)
log.Printf("ApplicationUpdate: err: %+v, app: %+v\n", err, ap)
// create a new bot account for this application
bot, err := dg.ApplicationBotCreate(ap.ID)
log.Printf("BotCreate: err: %+v, bot: %+v\n", err, bot)
// Get a list of all applications for the authenticated user
apps, err := dg.Applications()
log.Printf("Applications: err: %+v, apps : %+v\n", err, apps)
for k, v := range apps {
log.Printf("Applications: %d : %+v\n", k, v)
}
// Delete the application we created.
err = dg.ApplicationDelete(ap.ID)
log.Printf("Delete: err: %+v\n", err)
return
}
示例14: main
func main() {
// Check for Username and Password CLI arguments.
if len(os.Args) != 3 {
fmt.Println("You must provide username and password as arguments. See below example.")
fmt.Println(os.Args[0], " [username] [password]")
return
}
// Call the helper function New() passing username and password command
// line arguments. This returns a new Discord session, authenticates,
// connects to the Discord data websocket, and listens for events.
dg, err := discordgo.New(os.Args[1], os.Args[2])
if err != nil {
fmt.Println(err)
return
}
raffles = map[string]*Raffle{}
commandHandler = NewCommandHandler(dg)
RegisterCommands()
commandHandler.Start()
// Register messageCreate as a callback for the OnMessageCreate event.
dg.AddHandler(messageCreate)
// Open the websocket and begin listening.
dg.Open()
fmt.Println("Successfully started the bot, type \"quit\" to stop the bot.")
running := true
for running {
var input string
fmt.Scanln(&input)
if input == "quit" {
running = false
}
}
return
}
示例15: main
func main() {
// NOTE: All of the below fields are required for this example to work correctly.
var (
Email = flag.String("e", "", "Discord account email.")
Password = flag.String("p", "", "Discord account password.")
GuildID = flag.String("g", "", "Guild ID")
ChannelID = flag.String("c", "", "Channel ID")
err error
)
flag.Parse()
// Connect to Discord
discord, err := discordgo.New(*Email, *Password)
if err != nil {
fmt.Println(err)
return
}
// Open Websocket
err = discord.Open()
if err != nil {
fmt.Println(err)
return
}
// Connect to voice channel.
// NOTE: Setting mute to false, deaf to true.
dgv, err := discord.ChannelVoiceJoin(*GuildID, *ChannelID, false, true)
if err != nil {
fmt.Println(err)
return
}
// Starts echo
Echo(dgv)
// Close connections
dgv.Close()
discord.Close()
return
}