64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package Untis
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
var BotToken string
|
|
|
|
func checkNilErr(e error) {
|
|
if e != nil {
|
|
log.Fatal("Error message")
|
|
}
|
|
}
|
|
|
|
func Run() {
|
|
|
|
// create a session
|
|
discord, err := discordgo.New("Bot " + BotToken)
|
|
checkNilErr(err)
|
|
|
|
// set intents to receive message events in guilds and DMs
|
|
discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
|
|
|
// add a event handler
|
|
discord.AddHandler(newMessage)
|
|
|
|
// open session
|
|
discord.Open()
|
|
defer discord.Close() // close session, after function termination
|
|
|
|
// keep bot running untill there is NO os interruption (ctrl + C)
|
|
fmt.Println("Bot running....")
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt)
|
|
<-c
|
|
|
|
}
|
|
|
|
func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
|
|
fmt.Printf(message.Content)
|
|
/* prevent bot responding to its own message
|
|
this is achived by looking into the message author id
|
|
if message.author.id is same as bot.author.id then just return
|
|
*/
|
|
if message.Author.ID == discord.State.User.ID {
|
|
return
|
|
}
|
|
|
|
// respond to user message if it contains `!help` or `!bye`
|
|
switch {
|
|
case strings.Contains(message.Content, "!untis"):
|
|
discord.ChannelMessageSend(message.ChannelID, "Hello World😃")
|
|
case strings.Contains(message.Content, "!bye"):
|
|
discord.ChannelMessageSend(message.ChannelID, "Good Bye👋")
|
|
// add more cases if required
|
|
}
|
|
|
|
}
|