bridge

package module
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2023 License: Apache-2.0 Imports: 7 Imported by: 1

README

Discord Go Bridge library

Build Status Godoc

A small library using discordgo to implement bridges between discord and other chats (e.g. IRC). On the discord side, the bot uses webhook to impersonate nicknames, and if the nickname matches one on discord, also uses their avatar.

Example

import (
  bridge "github.com/StalkR/discordgo-bridge"
)

general := bridge.NewChannel("#general", "https://discord.com/api/webhooks/<webhook id>/<webhook token>", func(nick, text string) {
  log.Printf("relaying from discord -> external: <%v> %v", nick, text)
  // implement sending to external
})

go func() {
  // implement receiving from external
  for ; ; time.Sleep(10 * time.Second) {
    nick, text := "timebot", fmt.Sprintf("Current time: %v", time.Now())
    log.Printf("relaying from external -> discord: <%v> %v", nick, text)
    general.Send(nick, text)
  }
}()

b := bridge.NewBot("<bot token>", general)
if err := b.Start(); err != nil {
  log.Fatal(err)
}
defer b.Close()

log.Printf("Discord bridge bot running - press CTRL-C to exit")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc

Acknowledgements

Discord Go bindings: bwmarrin/discordgo (doc).

Bugs, comments, questions

Create a new issue.

Documentation

Overview

Package bridge is a library to bridge communications with discord.

import bridge "github.com/StalkR/discordgo-bridge"

Setup:

Multiple channels can be bridged. The bot can only be added to one server. To use it on more than one server, create another application and run another bot.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotConnected = errors.New("not connected")

ErrNotConnected is returned when sending but the bot is not connected.

Functions

This section is empty.

Types

type Bot

type Bot struct {
	// contains filtered or unexported fields
}

A Bot represents a discord bridge bot.

Example
general := NewChannel("#general", "https://discord.com/api/webhooks/<webhook id>/<webhook token>", func(nick, text string) {
	log.Printf("relaying from discord -> external: <%v> %v", nick, text)
	// implement sending to external
})

go func() {
	// implement receiving from external
	for ; ; time.Sleep(10 * time.Second) {
		nick, text := "timebot", fmt.Sprintf("Current time: %v", time.Now())
		log.Printf("relaying from external -> discord: <%v> %v", nick, text)
		general.Send(nick, text)
	}
}()

b := NewBot("<bot token>", general)
if err := b.Start(); err != nil {
	log.Fatal(err)
}
defer b.Close()

log.Printf("Discord bridge bot running - press CTRL-C to exit")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
Output:

func NewBot

func NewBot(token string, channels ...*Channel) *Bot

NewBot creates a new discord bridge bot.

func (*Bot) Close

func (b *Bot) Close() error

Close closes the discord bridge bot. Send and receive hooks are uninstalled, and the discord session closed. Bot can be started again.

func (*Bot) Start

func (b *Bot) Start() error

Start starts the bot, connecting it with discord.

type Channel

type Channel struct {
	// contains filtered or unexported fields
}

A Channel represents a discord channel bridge.

func NewChannel

func NewChannel(name, webhookURL string, receive func(nick, text string)) *Channel

NewChannel creates a new channel bridge. The receive function is called when messages are received on discord.

func (*Channel) Send

func (c *Channel) Send(nick, text string) error

Send sends a message to discord. It returns ErrNotConnected if called when bot is not connected to discord.

Directories

Path Synopsis
Package irc implements an irc-discord bridge.
Package irc implements an irc-discord bridge.

Jump to

Keyboard shortcuts

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