discogo

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2022 License: MIT Imports: 4 Imported by: 0

README

discogo

discogo is a package to create a connection as a discord bot to handle messages received in a channel and send messages to a specific channel.

You can use it to implement handlers for updating specific configurations in your app, so you can update them without restarting the process and also log some information to discord.

Getting Started

Installing
go get github.com/tripolious/discogo

You first need to create a discord bot and a token

Note

The bot will run as a goroutine. To handle the shutdown gracefully you need a context and a waitgroup.

You can find a working example (main + CreateLaunchContext func) under examples how to start it and handle the interrupt (ctr+c)

Usage

Import the package into your project.

import "github.com/tripolious/discogo

Initialize the bot

log.Println("starting discord bot")
err := discogo.Boot(ctx, &wg, *token)
if err != nil {
    log.Fatalf("booting failed %s", err)
}

You can add as many handlers as you wish in your project to consume message events

// define a function you want to add
func handler_x(s *discordgo.Session, m *discordgo.MessageCreate){
	
}
// define an array of handlers
var handlers = []interface{}{
    func(s *discordgo.Session, r *discordgo.Ready) { log.Println("discord bot is up and running") }, 
    handler_x,
}

// add them
err = discogo.AddHandlers(handlers)
if err != nil {
log.Printf("unable to add handlers: %s", err)
}

Send a message to a discord channel

err := discogo.SendMessage(*channel, "hello world!")
if err != nil {
    log.Printf("unable to send message: %s", err)
}
Example

Checkout examples for a working solution

go run examples/listen-and-respond/listen-and-respond.go --token xxx --channel yyy 
go run examples/send-message/send-message.go --token xxx --channel yyy 

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHandlers added in v0.0.2

func AddHandlers(handlers []interface{}) error

AddHandlers to consume messages sent or received in discord

func Boot added in v0.0.2

func Boot(ctx context.Context, wg *sync.WaitGroup, token string) error

Boot a new discord bot if there is already a session, there will be no other

func SendMessage added in v0.0.2

func SendMessage(channel string, message string) error

SendMessage to a specific discord channel

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL