diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/Bot/Untis.go b/Bot/Untis.go new file mode 100644 index 0000000..6e13c6f --- /dev/null +++ b/Bot/Untis.go @@ -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 + } + +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..27283e1 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Discord bot for logging Untis timetable \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6050381 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..04715b6 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6966df9 --- /dev/null +++ b/main.go @@ -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 +} diff --git a/untislogger.exe b/untislogger.exe new file mode 100644 index 0000000..6f2ebd3 Binary files /dev/null and b/untislogger.exe differ