added !vc to create temporary voice channels

This commit is contained in:
kageru 2019-03-12 22:37:06 +01:00
parent a4e7ecb92e
commit c22f81d6cb
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
4 changed files with 98 additions and 9 deletions

View File

@ -1,6 +1,9 @@
# Selphybot Changelog # Selphybot Changelog
Updates are listed in reverse chronological order. Updates are listed in reverse chronological order.
### 1.5 (dev)
- added !vc to create temporary voice channels
### 1.4 ### 1.4
- a copy of each deleted message is now send via DM to the author (suggested by CommanderLook) - a copy of each deleted message is now send via DM to the author (suggested by CommanderLook)
- seasonal fluff - seasonal fluff

View File

@ -43,8 +43,8 @@ func isDM(s *discordgo.Session, m *discordgo.MessageCreate) bool {
} }
func getDMChannelFromMessage(s *discordgo.Session, m *discordgo.MessageCreate) *discordgo.Channel { func getDMChannelFromMessage(s *discordgo.Session, m *discordgo.MessageCreate) *discordgo.Channel {
dm, _ := s.UserChannelCreate(m.Author.ID) dm, _ := s.UserChannelCreate(m.Author.ID)
return dm return dm
} }
func isAdmin(u *discordgo.User) bool { func isAdmin(u *discordgo.User) bool {
@ -55,3 +55,13 @@ func isAdmin(u *discordgo.User) bool {
} }
return false return false
} }
func getServer() *discordgo.Guild {
server, _ := state.Guild(config.ServerID)
return server
}
func remove(channels []*discordgo.Channel, position int) []*discordgo.Channel {
channels[len(channels)-1], channels[position] = channels[position], channels[len(channels)-1]
return channels[:len(channels)-1]
}

29
main.go
View File

@ -11,18 +11,19 @@ import (
var config = readConfig() var config = readConfig()
var commands []*Command var commands []*Command
var state *discordgo.State
func main() { func main() {
dg, err := discordgo.New("Bot " + config.Token) session, err := discordgo.New("Bot " + config.Token)
if err != nil { if err != nil {
fmt.Println("error: ", err) fmt.Println("error: ", err)
return return
} }
defer dg.Close() defer session.Close()
dg.AddHandler(evaluateMessage) session.AddHandler(evaluateMessage)
dg.AddHandler(onJoin) session.AddHandler(onJoin)
err = dg.Open() err = session.Open()
if err != nil { if err != nil {
fmt.Println("No connection:\n", err) fmt.Println("No connection:\n", err)
return return
@ -34,7 +35,15 @@ func main() {
} }
defer f.Close() defer f.Close()
log.SetOutput(f) log.SetOutput(f)
dg.UpdateStatus(0, "!help") session.UpdateStatus(0, "!help")
state = discordgo.NewState()
server, err := session.Guild(config.ServerID)
if err != nil {
fmt.Println("Guild incorrectly configured. Exiting...")
return
}
state.GuildAdd(server)
go checkAndDeleteUnusedChannels(session)
addCommands() addCommands()
fmt.Println("Bot running. selphyWoo") fmt.Println("Bot running. selphyWoo")
@ -43,6 +52,9 @@ func main() {
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc <-sc
for _, channel := range tempChannels {
session.ChannelDelete(channel.ID)
}
fmt.Println("Exiting...") fmt.Println("Exiting...")
log.Println("Exiting...") log.Println("Exiting...")
} }
@ -61,7 +73,7 @@ func addCommands() {
registerCommand(Command{Trigger: comm, Type: CommandTypeFullMatch, DMOnly: true, Function: giveAgeRole}) registerCommand(Command{Trigger: comm, Type: CommandTypeFullMatch, DMOnly: true, Function: giveAgeRole})
} }
// Misc commands // Fluff
registerCommand(Command{Trigger: "o/", Output: "\\o", Type: CommandTypeFullMatch, Cooldown: 10}) registerCommand(Command{Trigger: "o/", Output: "\\o", Type: CommandTypeFullMatch, Cooldown: 10})
registerCommand(Command{Trigger: "\\o", Output: "o/", Type: CommandTypeFullMatch, Cooldown: 10}) registerCommand(Command{Trigger: "\\o", Output: "o/", Type: CommandTypeFullMatch, Cooldown: 10})
registerCommand(Command{Trigger: "\\o/", Output: "/o\\", Type: CommandTypeFullMatch, Cooldown: 10}) registerCommand(Command{Trigger: "\\o/", Output: "/o\\", Type: CommandTypeFullMatch, Cooldown: 10})
@ -75,6 +87,9 @@ func addCommands() {
registerCommand(Command{Trigger: "!welcome", OutputEmbed: getWelcomeEmbed(), Type: CommandTypeFullMatch, DMOnly: true}) registerCommand(Command{Trigger: "!welcome", OutputEmbed: getWelcomeEmbed(), Type: CommandTypeFullMatch, DMOnly: true})
registerCommand(Command{Trigger: "!mods", Output: "Bei Fragen, Problemen und Beschwerden wende dich bitte an die Moderatoren oder schick mir eine Nachricht beginnend mit !complain, um dich anonym zu beschweren.\nAktuell anwesende Mods werden dir rechts mit dem Rang „Maid“ angezeigt.", Type: CommandTypeFullMatch}) registerCommand(Command{Trigger: "!mods", Output: "Bei Fragen, Problemen und Beschwerden wende dich bitte an die Moderatoren oder schick mir eine Nachricht beginnend mit !complain, um dich anonym zu beschweren.\nAktuell anwesende Mods werden dir rechts mit dem Rang „Maid“ angezeigt.", Type: CommandTypeFullMatch})
// Features :Pog:
registerCommand(Command{Trigger: "!vc ", Type: CommandTypePrefix, Function: parseVoiceChannelCommand})
// Admin and/or debug // Admin and/or debug
registerCommand(Command{Trigger: "<@%s> <3", Output: "<@%s> <3", Type: CommandTypeFullMatch, AdminOnly: true, OutputIsReply: true, RequiresMention: true}) registerCommand(Command{Trigger: "<@%s> <3", Output: "<@%s> <3", Type: CommandTypeFullMatch, AdminOnly: true, OutputIsReply: true, RequiresMention: true})
registerCommand(Command{Trigger: "echo", Type: CommandTypePrefix, Function: echoMessage, AdminOnly: true}) registerCommand(Command{Trigger: "echo", Type: CommandTypePrefix, Function: echoMessage, AdminOnly: true})

61
voicechannel.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"strconv"
"strings"
"time"
)
var tempChannels []*discordgo.Channel
func parseVoiceChannelCommand(session *discordgo.Session, message *discordgo.MessageCreate) {
userLimit, err := strconv.Atoi(strings.Split(message.Content, " ")[1])
if err != nil {
session.ChannelMessageSend(message.ChannelID, "Error: Expected a number after !vc")
log.Printf("Incorrect syntax for !vc, “%s” triggered by %s", message.Content, userToString(message.Author))
return
}
if userLimit > 99 {
session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Als ob %d Leute *mit dir* in einen Channel wollen", userLimit-1))
log.Printf("%s tried to create a channel with %d slots", userToString(message.Author), userLimit)
return
}
createData := discordgo.GuildChannelCreateData{
Name: fmt.Sprintf("%s’s Volatile Corner", message.Author.Username),
Type: discordgo.ChannelTypeGuildVoice,
UserLimit: userLimit,
}
channel, err := session.GuildChannelCreateComplex(message.GuildID, createData)
if err != nil {
session.ChannelMessageSend(message.ChannelID, "Couldn’t create the voice channel. Please bug kageru about this.")
log.Printf("Failed to create voice channel, %s", err)
return
}
tempChannels = append(tempChannels, channel)
session.GuildMemberMove(config.ServerID, message.Author.ID, channel.ID)
}
func checkAndDeleteUnusedChannels(session *discordgo.Session) {
for true {
time.Sleep(30 * time.Second)
for i, channel := range tempChannels {
if channelIsEmpty(channel.ID) {
session.ChannelDelete(channel.ID)
tempChannels = remove(tempChannels, i)
break
}
}
}
}
func channelIsEmpty(channelID string) bool {
for _, voiceState := range getServer().VoiceStates {
if channelID == voiceState.ChannelID {
return false
}
}
return true
}