本文整理汇总了Golang中github.com/bwmarrin/discordgo.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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()
}
开发者ID:Xe,项目名称:betterbot,代码行数:27,代码来源:main.go
示例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
}
开发者ID:42wim,项目名称:matterbridge,代码行数:25,代码来源:main.go
示例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")
}
开发者ID:Xwth,项目名称:h-bot,代码行数:27,代码来源:main.go
示例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
}
开发者ID:bwmarrin,项目名称:discordgo,代码行数:33,代码来源:main.go
示例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
}
开发者ID:42wim,项目名称:matterbridge,代码行数:25,代码来源:main.go
示例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")
}
开发者ID:softashell,项目名称:lewdbot-discord,代码行数:34,代码来源:main.go
示例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
}
开发者ID:Skullever,项目名称:digo,代码行数:26,代码来源:new_basic.go
示例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
}
开发者ID:iopred,项目名称:discord-cli,代码行数:58,代码来源:main.go
示例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
}
开发者ID:bwmarrin,项目名称:dca,代码行数:56,代码来源:main.go
示例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{})
}
开发者ID:dhedegaard,项目名称:notibot,代码行数:19,代码来源:notibot.go
示例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
}
开发者ID:RyanMorrison04,项目名称:pokemon-discord,代码行数:53,代码来源:pokemon.go
示例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
}
开发者ID:Silvea12,项目名称:dgvoice,代码行数:48,代码来源:playfile.go
示例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
}
开发者ID:bwmarrin,项目名称:discordgo,代码行数:48,代码来源:oauth2_test.go
示例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
}
开发者ID:Luukdegram,项目名称:patbot,代码行数:43,代码来源:patbot.go
示例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
}
开发者ID:Rassilon216,项目名称:dgvoice,代码行数:43,代码来源:main.go
示例16: main
func main() {
dg, err := discordgo.New(os.Getenv("DIS_TOKEN"))
if err != nil {
fmt.Println(err)
return
}
//set a call back func OnMessageCreate
dg.AddHandler(messageCreate)
err = dg.Open()
if err != nil {
fmt.Println(err)
return
}
st, err := dg.UserGuilds()
fmt.Printf("%s", st[0].Name)
// Simple way to keep program running until any key press.
var input string
fmt.Scanln(&input)
return
}
开发者ID:dayln,项目名称:gopherbot,代码行数:20,代码来源:main.go
示例17: Connect
func (b *bdiscord) Connect() error {
var err error
flog.Info("Connecting")
if !strings.HasPrefix(b.Config.Token, "Bot ") {
b.Config.Token = "Bot " + b.Config.Token
}
b.c, err = discordgo.New(b.Config.Token)
if err != nil {
flog.Debugf("%#v", err)
return err
}
flog.Info("Connection succeeded")
b.c.AddHandler(b.messageCreate)
err = b.c.Open()
if err != nil {
flog.Debugf("%#v", err)
return err
}
guilds, err := b.c.UserGuilds()
if err != nil {
flog.Debugf("%#v", err)
return err
}
userinfo, err := b.c.User("@me")
if err != nil {
flog.Debugf("%#v", err)
return err
}
b.Nick = userinfo.Username
for _, guild := range guilds {
if guild.Name == b.Config.Server {
b.Channels, err = b.c.GuildChannels(guild.ID)
if err != nil {
flog.Debugf("%#v", err)
return err
}
}
}
return nil
}
开发者ID:42wim,项目名称:matterbridge,代码行数:40,代码来源:discord.go
示例18: discordStart
//Starts discord connection and sets handlers and behavior
func discordStart(c *discordConfig) {
//Updates discordCfg variable with c parameter data
//For future use outside this function
discordCfg = c
//Connect to discord with token
var err error
discord, err = discordgo.New(c.Token)
if err != nil {
log.Fatal("Error initializing discord in discordStart() function!\n", err)
}
//Set behavior & assign handlers
discord.Debug = c.Debug
discord.AddHandler(discordMsgHandler)
discord.AddHandler(discordReadyHandler)
discord.AddHandler(discordNewGuildHandler)
discord.AddHandler(discordLeaveGuildHandler)
discord.AddHandler(discordNewChannelHandler)
discord.AddHandler(discordLeaveChannelHandler)
discord.AddHandler(discordChannelUpdateHandler)
discord.Open() //Opens discord connection
}
开发者ID:gagizagi,项目名称:Axolotl-GO,代码行数:24,代码来源:discord.go
示例19: init
func init() {
token := config.Constants.DiscordToken
guildId := config.Constants.DiscordGuildId
if token == "" || guildId == "" {
return
}
var err error
Discord, err = dg.New(token)
if err != nil {
logrus.Fatal(err)
return
}
guild, err := Discord.Guild(guildId)
if err != nil {
Discord = nil
logrus.Fatal(err)
return
}
rawChannels, err := Discord.GuildChannels(guildId)
if err != nil {
Discord = nil
logrus.Fatal(err)
return
}
for _, emoji := range guild.Emojis {
emojis[emoji.Name] = fmt.Sprintf("<:%s:%s>", emoji.Name, emoji.ID)
}
for _, channel := range rawChannels {
channels[channel.Name] = channel
}
logrus.Infof("Discord: Loaded %d channels, %d emojis", len(rawChannels), len(guild.Emojis))
}
开发者ID:TF2Stadium,项目名称:Helen,代码行数:36,代码来源:discord.go
示例20: init
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
)
// Version is a constant that stores the Disgord version information.
const Version = "v0.0.0-alpha"
// Session is declared in the global space so it can be easily used
// throughout this program.
// In this use case, there is no error that would be returned.
var Session, _ = discordgo.New()
// Read in all configuration options from both environment variables and
// command line arguments.
func init() {
// Discord Authentication Token
Session.Token = os.Getenv("DG_TOKEN")
if Session.Token == "" {
flag.StringVar(&Session.Token, "t", "", "Discord Authentication Token")
}
}
func main() {
// Declare any variables needed later.
开发者ID:bwmarrin,项目名称:disgord,代码行数:31,代码来源:main.go
注:本文中的github.com/bwmarrin/discordgo.New函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论