Verify discord channel type on join, fixes issue #8

master
Matthieu Lalonde 5 years ago
parent e52ae1e4a3
commit 1b5987275a

@ -12,6 +12,7 @@ import (
"os" "os"
"git.lalonde.me/matth/AltVRBot/pkg/discord/mux" "git.lalonde.me/matth/AltVRBot/pkg/discord/mux"
"git.lalonde.me/matth/AltVRBot/pkg/utils"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -27,6 +28,10 @@ type Discord struct {
User *discordgo.User User *discordgo.User
} }
var (
allowedChannelTypes = []discordgo.ChannelType{discordgo.ChannelTypeGuildText, discordgo.ChannelTypeDM, discordgo.ChannelTypeGroupDM}
)
// New instanciates a new discord bot // New instanciates a new discord bot
func New(token, sID, cID string) (*Discord, error) { func New(token, sID, cID string) (*Discord, error) {
dg := &Discord{ dg := &Discord{
@ -49,6 +54,15 @@ func New(token, sID, cID string) (*Discord, error) {
dg.Session = session dg.Session = session
c, err := dg.Session.Channel(cID)
if err != nil {
log.Fatalf("Invalid channel ID: %+v\n", err)
}
if !utils.SliceContainsChannelType(allowedChannelTypes, c.Type) {
log.Fatalf("Cannot join channel, invalid channel type")
}
dg.addHandlers() dg.addHandlers()
// Open a websocket connection to Discord // Open a websocket connection to Discord

@ -1,5 +1,7 @@
package utils package utils
import "github.com/bwmarrin/discordgo"
// TruncateString UTF8 truncation of string to `i` length // TruncateString UTF8 truncation of string to `i` length
func TruncateString(s string, i int) string { func TruncateString(s string, i int) string {
runes := []rune(s) runes := []rune(s)
@ -8,3 +10,13 @@ func TruncateString(s string, i int) string {
} }
return s return s
} }
// SliceContainsChannelType looks for int value in a slice
func SliceContainsChannelType(s []discordgo.ChannelType, e discordgo.ChannelType) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

Loading…
Cancel
Save