Files
InnkeeperBot/main.go
Matte23 bd2fe73102
All checks were successful
Build artifact / build (push) Successful in 22s
Build artifact / release (push) Has been skipped
chore: Objectify managed guilds
2024-07-11 12:44:30 +02:00

153 lines
4.7 KiB
Go

// InnkeeperBot, a bot to create and manage custom channels
// Copyright (C) 2020-2021 Matteo Schiff
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"flag"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
)
// Variables used for command line parameters
var (
Token string
)
func init() {
flag.StringVar(&Token, "t", "", "Bot Token")
flag.Parse()
}
func main() {
setupLogging()
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + Token)
if err != nil {
log.Fatal("Error creating Discord session,", err)
return
}
// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(handleMessage)
dg.AddHandler(handleVoiceActivity)
dg.AddHandler(handlePresenceUpdate)
dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildMessages | discordgo.IntentsGuildVoiceStates | discordgo.IntentsGuildPresences | discordgo.IntentsGuildMembers)
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
log.Fatal("Error opening connection,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
log.Info("InnkeeperBot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Cleanly close down the Discord session.
dg.Close()
}
func handleVoiceActivity(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) {
if autoGuild, ok := managedGuilds[vs.GuildID]; ok {
if vs.ChannelID == "" {
// The user left a channel
autoGuild.removeUserFromChannels(s, vs.UserID)
return
}
if vs.ChannelID == autoGuild.newChannelEndpoint.ID {
// User joined the new channel endpoint
autoGuild.removeUserFromChannels(s, vs.UserID)
autoGuild.createNewChannel(s, vs.ChannelID, vs.UserID)
} else if _, ok := autoGuild.createdChannels[vs.ChannelID]; ok {
// User joined a managed channel
autoGuild.trackUserJoinManagedChannel(s, vs.ChannelID, vs.UserID)
} else {
// User joined a non managed channel
autoGuild.removeUserFromChannels(s, vs.UserID)
}
} else {
initChannels(s, vs.GuildID)
}
}
func handlePresenceUpdate(s *discordgo.Session, pu *discordgo.PresenceUpdate) {
if autoGuild, ok := managedGuilds[pu.GuildID]; ok {
for channelID, channel := range autoGuild.createdChannels {
for _, user := range channel {
if user == pu.User.ID {
autoGuild.updateChannelName(s, channelID)
}
}
}
} else {
initChannels(s, pu.GuildID)
}
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the autenticated bot has access to.
func handleMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// If the message starts with "!new", create a new custom channel. Parameters: name, privacy [public or private](default public, optional)
if strings.HasPrefix(m.Content, "!new") {
commandNew(s, m)
}
// If the message starts with "!del", delete a custom channel. Parameters: name
if strings.HasPrefix(m.Content, "!del") {
commandDelete(s, m)
}
// If the message starts with "!add", enable an user to join the channel. Parameters: channel name, user name
if strings.HasPrefix(m.Content, "!add") {
commandAdd(s, m)
}
// If the message starts with "!rem", remove from an user the permission to join the channel. Parameters: channel name, user name
if strings.HasPrefix(m.Content, "!rem") {
commandRem(s, m)
}
// If the message starts with "!op", enable an user to manage the channel. Parameters: channel name, user name
if strings.HasPrefix(m.Content, "!op") {
commandOp(s, m)
}
// If the message starts with "!deop", remove from an user the permission to manage the channel. Parameters: channel name, user name
if strings.HasPrefix(m.Content, "!deop") {
commandDeop(s, m)
}
// If the message starts with "!rpc", remove from an user the permission to join the channel. Parameters: channel name, user name
if strings.HasPrefix(m.Content, "!help") {
commandHelp(s, m)
}
}