check for restricted private messaging to bot
This commit is contained in:
parent
0df0b21198
commit
c0e7a3918a
|
|
@ -21,14 +21,14 @@ func initBotWith(token string) *discordgo.Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiscordOptions struct {
|
type DiscordOptions struct {
|
||||||
AppConfig *domain.Config
|
Config *domain.Config
|
||||||
Controller *controller.WorkflowController
|
Controller *controller.WorkflowController
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(conf domain.Config, opts DiscordOptions) error {
|
func Run(conf domain.Config, opts DiscordOptions) error {
|
||||||
token := conf.Discord.Token
|
token := conf.Discord.Token
|
||||||
|
|
||||||
session := initBotWith(token)
|
s := initBotWith(token)
|
||||||
|
|
||||||
router := handler.InitRouter(*opts.Controller, &conf.Discord)
|
router := handler.InitRouter(*opts.Controller, &conf.Discord)
|
||||||
|
|
||||||
|
|
@ -39,47 +39,78 @@ func Run(conf domain.Config, opts DiscordOptions) error {
|
||||||
|
|
||||||
componentsHandlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
componentsHandlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
||||||
"task_start": router.Components[0].Handler,
|
"task_start": router.Components[0].Handler,
|
||||||
"task_close": router.Components[1].Handler,
|
"task_close": router.Components[0].Handler,
|
||||||
}
|
}
|
||||||
|
|
||||||
session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
s.AddHandler(router.ListenPostsHandler)
|
||||||
|
|
||||||
|
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
switch i.Type {
|
switch i.Type {
|
||||||
case discordgo.InteractionApplicationCommand:
|
case discordgo.InteractionApplicationCommand:
|
||||||
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
|
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
|
||||||
|
dchan, err := s.Channel(i.ChannelID)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if dchan.Type == discordgo.ChannelTypeDM {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Yo, fella! I'm not working in private!",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
h(s, i)
|
h(s, i)
|
||||||
}
|
}
|
||||||
case discordgo.InteractionMessageComponent:
|
case discordgo.InteractionMessageComponent:
|
||||||
if h, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
|
if h, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
|
||||||
|
dchan, err := s.Channel(i.ChannelID)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if dchan.Type == discordgo.ChannelTypeDM {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Yo, fella! I'm not working in private!",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
h(s, i)
|
h(s, i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := session.Open(); err != nil {
|
if err := s.Open(); err != nil {
|
||||||
return fmt.Errorf("cannot open the session: %v", err)
|
return fmt.Errorf("cannot open the session: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UPDATE FORUM IF NEEDED:
|
// UPDATE FORUM IF NEEDED:
|
||||||
|
|
||||||
forum, err := session.Channel(os.Getenv("TASKS_CHANNEL"))
|
// forum, err := session.Channel(os.Getenv("TASKS_CHANNEL"))
|
||||||
|
forum, err := s.Channel(conf.Discord.IsProjectChannel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
forum, err = session.ChannelEditComplex(forum.ID, &discordgo.ChannelEdit{
|
_, err = s.ChannelEditComplex(forum.ID, &discordgo.ChannelEdit{
|
||||||
AvailableTags: &router.Tags,
|
AvailableTags: &router.Tags,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Adding commands...")
|
log.Println("Adding commands...")
|
||||||
var cmds []*discordgo.ApplicationCommand
|
var cmds []*discordgo.ApplicationCommand
|
||||||
var logString []string
|
var logString []string
|
||||||
for _, h := range router.Commands {
|
for _, h := range router.Commands {
|
||||||
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, "", &h.Command)
|
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "1103928338898235462", &h.Command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Cannot create '%v' command: %v", h.Command.Name, err)
|
log.Panicf("Cannot create '%v' command: %v", h.Command.Name, err)
|
||||||
}
|
}
|
||||||
|
|
@ -90,7 +121,7 @@ func Run(conf domain.Config, opts DiscordOptions) error {
|
||||||
log.Println("Following commands added:")
|
log.Println("Following commands added:")
|
||||||
log.Println(logString)
|
log.Println(logString)
|
||||||
|
|
||||||
defer session.Close()
|
defer s.Close()
|
||||||
stop := make(chan os.Signal, 1)
|
stop := make(chan os.Signal, 1)
|
||||||
signal.Notify(stop, os.Interrupt)
|
signal.Notify(stop, os.Interrupt)
|
||||||
<-stop
|
<-stop
|
||||||
|
|
@ -98,7 +129,7 @@ func Run(conf domain.Config, opts DiscordOptions) error {
|
||||||
|
|
||||||
log.Println("Removing commands...")
|
log.Println("Removing commands...")
|
||||||
for _, h := range cmds {
|
for _, h := range cmds {
|
||||||
err := session.ApplicationCommandDelete(session.State.User.ID, "", h.ID)
|
err := s.ApplicationCommandDelete(s.State.User.ID, "", h.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Cannot delete '%v' command: %v", h.Name, err)
|
log.Panicf("Cannot delete '%v' command: %v", h.Name, err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue