Skip to content

Commit

Permalink
DiscordLogger & DiscordCache refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Feb 7, 2023
1 parent 723e00a commit 13bc353
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 127 deletions.
30 changes: 22 additions & 8 deletions README.md
Expand Up @@ -231,22 +231,21 @@ DiscordGlobalConfiguration.logManager = DiscordLogManager(
),
aliveNotice: .init(
address: try .url(WEBHOOK_URL),
interval: .hours(1),
/// If nil, DiscordLogger will only send 1 "I'm alive" notice, on boot.
/// If not nil, it will send a "I'm alive" notice every this-amount too.
interval: nil,
message: "I'm Alive! :)",
color: .blue,
initialNoticeMention: .user("970723029262942248")
),
mentions: [
.critical: .role("970723029262942248"),
.error: .role("970723101044244510"),
.warning: .role("970723134149918800"),
.trace: .role("970723180706668584"),
.debug: .role("970723199761383484"),
.notice: .role("970723218551865384"),
.info: .role("970723238097330237"),
.error: .role("970723101044244510"),
.critical: .role("970723029262942248"),
],
extraMetadata: [.warning, .error, .critical],
disabledLogLevels: [.debug, .trace]
disabledLogLevels: [.debug, .trace],
disabledInDebug: true
)
)
```
Expand All @@ -256,6 +255,21 @@ If you want to only use Discord logger and don't use the rest of `DiscordBM`, yo
.product(name: "DiscordLogger", package: "DiscordBM"),
```

#### Example

```swift
/// After bootstrapping the `LoggingSystem`, and with the configuration above, but `extraMetadata` set to `[.critical]`
let logger = Logger(label: "LoggerLabel")
logger.warning("Warning you about something!")
logger.error("We're having an error!", metadata: [
"number": .stringConvertible(1),
"statusCode": "401 Unauthorized"
])
logger.critical("CRITICAL PROBLEM. ABOUT TO EXPLODE 💥")
```

<img width="489" alt="DiscordLogger Showcase Output" src="https://user-images.githubusercontent.com/54685446/217139045-e4abf8c7-d5f9-4e89-9170-53980e90af57.png">

</details>

### Discord Cache
Expand Down
8 changes: 5 additions & 3 deletions Sources/DiscordClient/Endpoint.swift
Expand Up @@ -240,9 +240,9 @@ public enum Endpoint: Sendable {
case .getGateway, .getGatewayBot, .createInteractionResponse, .getInteractionResponse, .editInteractionResponse, .deleteInteractionResponse, .postFollowupInteractionResponse, .getFollowupInteractionResponse, .editFollowupInteractionResponse, .deleteFollowupInteractionResponse, .createMessage, .editMessage, .deleteMessage, .createApplicationGlobalCommand, .getApplicationGlobalCommands, .deleteApplicationGlobalCommand, .getGuild, .searchGuildMembers, .getGuildMember, .getChannel, .getChannelMessages, .getChannelMessage, .leaveGuild, .createGuildRole, .deleteGuildRole, .addGuildMemberRole, .removeGuildMemberRole, .getGuildAuditLogs, .addReaction, .createDM, .createWebhook, .getChannelWebhooks, .getGuildWebhooks, .getWebhook1, .modifyWebhook1, .deleteWebhook1:
return self.urlSuffix
case let .getWebhook2(id, token),
let .modifyWebhook2(id, token),
let .deleteWebhook2(id, token),
let .executeWebhook(id, token):
let .modifyWebhook2(id, token),
let .deleteWebhook2(id, token),
let .executeWebhook(id, token):
return "webhooks/\(id)/\(token.hash)"
case let .getWebhookMessage(id, token, messageId):
return "webhooks/\(id)/\(token.hash)/messages/\(messageId)"
Expand Down Expand Up @@ -321,6 +321,8 @@ public enum Endpoint: Sendable {
}
}

/// Some endpoints like don't require an authorization header because the endpoint itself
/// contains some kind of authorization token. Like half of the webhook endpoints.
var requiresAuthorizationHeader: Bool {
switch self {
case .getGateway, .getGatewayBot, .createInteractionResponse, .getInteractionResponse, .editInteractionResponse, .deleteInteractionResponse, .postFollowupInteractionResponse, .getFollowupInteractionResponse, .editFollowupInteractionResponse, .deleteFollowupInteractionResponse, .createMessage, .editMessage, .deleteMessage, .createApplicationGlobalCommand, .getApplicationGlobalCommands, .deleteApplicationGlobalCommand, .getGuild, .searchGuildMembers, .getGuildMember, .getChannel, .getChannelMessages, .getChannelMessage, .leaveGuild, .createGuildRole, .deleteGuildRole, .addGuildMemberRole, .removeGuildMemberRole, .getGuildAuditLogs, .addReaction, .createDM, .createWebhook, .getChannelWebhooks, .getGuildWebhooks, .getWebhook1, .modifyWebhook1, .deleteWebhook1:
Expand Down
12 changes: 0 additions & 12 deletions Sources/DiscordCore/DiscordGlobalConfiguration.swift
Expand Up @@ -14,18 +14,6 @@ public enum DiscordGlobalConfiguration {
/// Function to make loggers with. You can override it with your own logger.
/// The `String` argument represents the label of the logger.
public static var makeLogger: (String) -> Logger = { Logger(label: $0) }
/// Log about sub-optimal situations during decode.
/// For example if a type can't find a representation to decode a value to,
/// and has to get rid of that value.
/// Does not include decode errors.
public static var enableLoggingDuringDecode: Bool = false
/// Global rate-limit for requests per second.
/// 50 by default, but you can ask Discord for a raise.
public static var globalRateLimit = 50
/// Whether or not to perform validations for `DefaultDiscordClient` payloads, before sending.
/// The library will throw a ``ValidationError`` if it finds anything invalid in the payload.
/// This all works based on Discord docs' validation notes.
public static var performClientSideValidations = true
}

//MARK: - DiscordDecoder
Expand Down

0 comments on commit 13bc353

Please sign in to comment.