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 {
|
||||
AppConfig *domain.Config
|
||||
Config *domain.Config
|
||||
Controller *controller.WorkflowController
|
||||
}
|
||||
|
||||
func Run(conf domain.Config, opts DiscordOptions) error {
|
||||
token := conf.Discord.Token
|
||||
|
||||
session := initBotWith(token)
|
||||
s := initBotWith(token)
|
||||
|
||||
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){
|
||||
"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 {
|
||||
case discordgo.InteractionApplicationCommand:
|
||||
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)
|
||||
}
|
||||
case discordgo.InteractionMessageComponent:
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if err := session.Open(); err != nil {
|
||||
if err := s.Open(); err != nil {
|
||||
return fmt.Errorf("cannot open the session: %v", err)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
forum, err = session.ChannelEditComplex(forum.ID, &discordgo.ChannelEdit{
|
||||
_, err = s.ChannelEditComplex(forum.ID, &discordgo.ChannelEdit{
|
||||
AvailableTags: &router.Tags,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
log.Println("Adding commands...")
|
||||
var cmds []*discordgo.ApplicationCommand
|
||||
var logString []string
|
||||
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 {
|
||||
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(logString)
|
||||
|
||||
defer session.Close()
|
||||
defer s.Close()
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt)
|
||||
<-stop
|
||||
|
|
@ -98,7 +129,7 @@ func Run(conf domain.Config, opts DiscordOptions) error {
|
|||
|
||||
log.Println("Removing commands...")
|
||||
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 {
|
||||
log.Panicf("Cannot delete '%v' command: %v", h.Name, err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue