New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Handle InteractionResponseDeferredChannelMessageWithSource in slash commands example. Also refactoring. #1045
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, everything looks good to me. But want to mention that we don't usually make go modules inside examples. Also I'm not sure if the slash commands example needs a readme, since it could be built with simple go build
.
examples/slash_commands/handlers.go
Outdated
"github.com/bwmarrin/discordgo" | ||
) | ||
|
||
func SelectHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a critical problem, but I think this should be renamed to something else, it is quite confusing, since we have select components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will look into this on weekend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's done. Take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, missed one thing
examples/slash_commands/main.go
Outdated
const GuildID string = "Bot token goes here" | ||
const BotToken string = "Guild where the bot will be used" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is a good idea, since most of the time sensitive stuff is put into flags, plus it is easy to replace these values without recompiling the application again.
Sorry, no updates. Busy doing other things. |
Closed it accidentally. I'm working on it right now and will reopen PR once I have something to commit. |
… occurs inside one of the handlers.
func Login(ds *discordgo.Session, r *discordgo.Ready) { | ||
log.Printf("Logged in as: %v#%v", ds.State.User.Username, ds.State.User.Discriminator) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having anonymous handler would be better in this case, since it's just oneliner.
examples/slash_commands/handlers.go
Outdated
r := recover() | ||
if r != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r := recover() | |
if r != nil { | |
if err := recover(); err != nil { |
examples/slash_commands/handlers.go
Outdated
r := recover() | ||
if r != nil { | ||
deleteCommands(ds, guildID) | ||
log.Fatal("Error occured inside command handler") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log.Fatal("Error occured inside command handler") | |
log.Fatalf("Error occured inside command handler: %s", err) |
examples/slash_commands/handlers.go
Outdated
} | ||
|
||
func Basic(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
panic("test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
panic("test") |
examples/slash_commands/main.go
Outdated
"Also, as bonus... look at the original interaction response :D", | ||
}) | ||
}, | ||
ds, err := discordgo.New("Bot " + *BotToken) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why you named the session ds
, the convention is usually s
examples/slash_commands/main.go
Outdated
func createCommands(ds *discordgo.Session, guildId string) { | ||
var err error | ||
for i, cmd := range commands { | ||
commands[i], err = ds.ApplicationCommandCreate( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're doing a refactoring of the example, I think it would be worthwhile to also replace ApplicationCommandCreate
with ApplicationCommandBulkOverwrite
here. While using Create
& Delete
is a lot cleaner (you don't affect other commands) and straightforward, they get ratelimited pretty quickly when using them on multiple commands.
examples/slash_commands/main.go
Outdated
var ( | ||
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally") | ||
BotToken = flag.String("token", "", "Bot access token") | ||
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not") | ||
RemoveCommands = flag.Bool("rmcmd", true, "Weather to remove all commands after shutdowning or not") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also speaking of performance and ratelimit issues, I think it would be better to set the default value to false
here. That way each time you run the application you won't need to wait until it deletes the commands.
Otherwise, in addition to usage of ApplicationCommandBulkOverwrite
, we can replace this parameter with -clean
, which when enabled would use ApplicationCommandCreate
& ApplicationCommandDelete
instead of ApplicationCommandBulkOverwrite
. That way it also might make sense to separate functionality into createCommands
function instead of doing it directly in main.
examples/slash_commands/handlers.go
Outdated
} | ||
|
||
func BasicWithFile(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples/slash_commands/handlers.go
Outdated
} | ||
|
||
func Subcommands(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples/slash_commands/handlers.go
Outdated
} | ||
|
||
func Responses(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…ed panic handling.
In /responses command
InteractionResponseDeferredChannelMessageWithSource
was listed in options but was not handled inside handler function. In result when typing command /responses and selecting option "Deffered response..." bot would either crash or go into infinite loop not knowing how to handle request from user.Moved handler functions and commands definitions to separate files.