Bot setup

This commit is contained in:
Jannover5000r 2025-06-12 15:14:20 +02:00
parent cadd31aaa6
commit a312f1d7ed
7 changed files with 108 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.env

64
Bot/Untis.go Normal file
View file

@ -0,0 +1,64 @@
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
}
}

1
README.md Normal file
View file

@ -0,0 +1 @@
# Discord bot for logging Untis timetable

14
go.mod Normal file
View file

@ -0,0 +1,14 @@
module untislogger
go 1.24.4
require (
github.com/bwmarrin/discordgo v0.29.0
github.com/joho/godotenv v1.5.1
)
require (
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
)

14
go.sum Normal file
View file

@ -0,0 +1,14 @@
github.com/bwmarrin/discordgo v0.29.0 h1:FmWeXFaKUwrcL3Cx65c20bTRW+vOb6k8AnaP+EgjDno=
github.com/bwmarrin/discordgo v0.29.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

14
main.go Normal file
View file

@ -0,0 +1,14 @@
package main
import (
"os"
Untis "untislogger/Bot"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
Untis.BotToken = os.Getenv("DISCORD_TOKEN")
Untis.Run() // call the run function of Bot/Untis.go
}

BIN
untislogger.exe Normal file

Binary file not shown.