Skip to content

Hello Handler

Will Navidson edited this page Jun 30, 2017 · 5 revisions

First, lets create our HelloHandler, which will listen for the string hello in every channel that our bot has access to.

package main

import (
	"fmt"
	
	"github.com/bwmarrin/discordgo"
)

type HelloHandler struct {
    conf *Config
}

func (h *HelloHandler) Read(s *discordgo.Session, m *discordgo.MessageCreate) {

	// Ignore all messages created by the bot itself
	if m.Author.ID == s.State.User.ID {
		return
	}

	// Ignore bots
	if m.Author.Bot {
		return
	}

    // Set our command prefix to the default one within our config file
	cp := h.conf.DUBotConfig.CP
	
	// If the message read is "hello" reply in the same channel with "Hi!"
	if m.Content == cp + "hello" {
		s.ChannelMessageSend( m.ChannelID, "Hi!")
	}	
}

That's it!

As you can see, the process of adding more strings to listen for is fairly easy, just remember to take the command prefix into account.

You can save this file as hello_handler.go, I like the underscore in the filename because it's easy for me to distinguish handler files in a directory listing.

See Also

Clone this wiki locally

⌂ 换网址