Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Since the beginning, DiscordGo did not have ability for adding per-call customization:
ChannelMessageDelete
, instead handling the error by yourself?GuildBanCreate
, likeX-Audit-Log-Reason
, to comment your action in audit log?We’ve been thinking on how to combat this problem, and came up with the following solutions:
Builders pattern
Functional options
This PR implements the latter. It provides a few pre-built options. However, you can always supply your custom option.
Options have access to a
RequestConfig
struct, which is internally used byRequestX
functions. This config allows you to modify a few global settings, likeClient
,ShouldRetryOnRateLimit
andMaxRestRetries
, but it also allows you to modify the request directly.A typical functional option would look like this:
These are built-in options discordgo will provide:
WithClient
- Controlhttp.Client
used for the request (defaults tos.Client
)WithHeader
- Change a certain header in the requestWithAuditLogReason
- SetX-Audit-Log-Reason
headerWithLocale
- SetX-Discord-Locale
headerWithRetryOnRateLimit
- ControlSession.ShouldRetryOnRateLimit
field for the requestWithMaxRestRetries
- ControlSession.MaxRestRetries
field for the requestWithContext
- Provide acontext.Context
to thehttp.Request
This change will be affecting all of the REST endpoints. However it should be non-breaking.
If you have used any function signatures though, you would need to change them accordingly:
Special thanks to @jalavosus for bringing up the issue with context in REST functions. His PR (#1119) was one of the factors that inspired me to implement this functionality.