discordgoext

package module
v0.0.0-...-08838e0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: MIT Imports: 8 Imported by: 0

README

discordgoext

standard-readme compliant Patreon

Deprecation notice

It's been two years since I have worked on this project. I'm not even sure if it still works. Given the new additions to the Discord API and future bots' reliance on them for their functionality, I have created a new and improved project called Harmonia. Please use that module instead.

Current features

  • Easier way of creating commands for a bot
  • Automatic help command

Table of Contents

Install

go get -u github.com/Moonlington/discordgoext

Usage

Here's an example of a bot that pings and pongs;

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"

    "github.com/Moonlington/discordgoext"
)

func main() {

    // Create a new Discord session using the provided bot token.
    sess, err := discordgoext.New("Bot " + "INSERT YOUR BOT TOKEN", "pingbot.", false)
    if err != nil {
        fmt.Println("error creating Discord session,", err)
        return
    }

    sess.AddCommand("Pings", discordgoext.NewCommand("ping", "Pings", "", "", func(ctx *discordgoext.Context) {
        ctx.SendMessage("pong!")
    }))
    sess.AddCommand("Pongs", discordgoext.NewCommand("pong", "Pongs", "", "", func(ctx *discordgoext.Context) {
        ctx.SendMessage("ping!")
    }))

    sess.AddCommand("Saying Hi", discordgoext.NewCommand(
        "hi",
        "Says hi",
        "[to who]",
        "If `to who` is not provided, it will say hi to you.",
        func(ctx *discordgoext.Context) {
            if len(ctx.Argstr) != 0 {
                ctx.SendMessage("Hi " + ctx.Argstr + "!")
            } else {
                ctx.SendMessage("Hi " + ctx.Mess.Author.Username + "!")
            }
        },
    ))

    // Open a websocket connection to Discord and begin listening.
    err = sess.Open()
    if err != nil {
        fmt.Println("error opening connection,", err)
        return
    }

    // Wait here until CTRL-C or other term signal is received.
    fmt.Println("Bot is now running.  Press CTRL-C to exit.")
    sc := make(chan os.Signal, 1)
    signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
    <-sc

    // Cleanly close down the Discord session.
    sess.Close()
}

Contribute

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © Moonlington

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUserFinding = errors.New("Found more than 1 or no users")

ErrUserFinding is the error it gives when more than 1 or no users are found, used to return commands.

Functions

func GetCreationTime

func GetCreationTime(ID string) (t time.Time, err error)

GetCreationTime is a helper function to get the time of creation of any ID. ID: ID to get the time from

Types

type Command

type Command struct {
	Name        string
	OnMessage   func(ctx *Context)
	Description string
	Usage       string
	Detailed    string
	Subcommands []*Command
	Category    string
	Check       func(ctx *Context) bool
}

The Command structy stores the command.

func NewCommand

func NewCommand(name, description, usage, detaileddescription string, onmessage func(ctx *Context)) *Command

NewCommand handles the creation of Commands.

func (*Command) AddSubCommand

func (c *Command) AddSubCommand(sc *Command) *Command

AddSubCommand handles the addition of extra subcommands to an existing command

type Context

type Context struct {
	Invoked string
	Argstr  string
	Args    []string
	Channel *discordgo.Channel
	Guild   *discordgo.Guild
	Mess    *discordgo.MessageCreate
	Sess    *ExtSession
}

A Context struct holds variables for Messages

func (*Context) GetAllUsers

func (ctx *Context) GetAllUsers() (ms []*discordgo.User, err error)

GetAllUsers is a helper function to return all members

func (*Context) GetUser

func (ctx *Context) GetUser(args ...string) (user *discordgo.User, err error)

GetUser is a helper function to get a user by name

func (*Context) GetUserByName

func (ctx *Context) GetUserByName(query string) (members []*discordgo.User, err error)

GetUserByName is a helper function to find a User by string query: String to use when finding User

func (*Context) GuildGetUserByName

func (ctx *Context) GuildGetUserByName(query, GuildID string) (members []*discordgo.User, err error)

GuildGetUserByName is a helper function to find a User by string in a guild query: String to use when finding User GuildID: ID for guild

func (*Context) ParseTooManyUsers

func (ctx *Context) ParseTooManyUsers(query string, users []*discordgo.User) (*discordgo.Message, error)

ParseTooManyUsers is a helper function to create a message for finding too many users query: String used when finding User users: List of users found

func (*Context) QuickSendEmbed

func (ctx *Context) QuickSendEmbed(s string) (*discordgo.Message, error)

QuickSendEmbed is a helper function to easily send strings as an embed. s: string to send

func (*Context) SendEmbed

func (ctx *Context) SendEmbed(em *discordgo.MessageEmbed) (*discordgo.Message, error)

SendEmbed is a helper function to easily send embeds. em: embed to send

func (*Context) SendMessage

func (ctx *Context) SendMessage(s string) (*discordgo.Message, error)

SendMessage sends a message to the channel the command came from. em: embed to send

type ExtSession

type ExtSession struct {
	*discordgo.Session
	Token    string
	Prefix   string
	Bot      bool
	Commands []*Command
	// contains filtered or unexported fields
}

ExtSession handles the bot and it's commands.

func New

func New(token, prefix string, userbot bool) (*ExtSession, error)

New creates a ExtSession from a token.

func (*ExtSession) AddCommand

func (s *ExtSession) AddCommand(category string, c *Command)

AddCommand handles the adding of Commands to the handler.

func (*ExtSession) AddPrivateCommand

func (s *ExtSession) AddPrivateCommand(category string, check func(ctx *Context) bool, c *Command)

AddPrivateCommand handles the adding of Private Commands to the handler.

func (*ExtSession) ChangeMessageHandler

func (s *ExtSession) ChangeMessageHandler(handler func(s *discordgo.Session, m *discordgo.MessageCreate))

ChangeMessageHandler handles the changing of the message handler (Lots of handlers.)

func (*ExtSession) CreateEmbed

func (s *ExtSession) CreateEmbed(ctx *Context) *discordgo.MessageEmbed

CreateEmbed handles the easy creation of Embeds.

func (*ExtSession) HandleCommands

func (s *ExtSession) HandleCommands(ctx *Context)

HandleCommands handles the Context and calls Command ctx: Context used

func (*ExtSession) HandleSubcommands

func (s *ExtSession) HandleSubcommands(ctx *Context, called *Command) (*Context, *Command)

HandleSubcommands returns the Context and Command that is being called ctx: Context used called: Command called

func (*ExtSession) HelpFunction

func (s *ExtSession) HelpFunction(ctx *Context)

HelpFunction handles the Help command for the CommandHandler ctx: Context used

Jump to

Keyboard shortcuts

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