Skip to content

Commit

Permalink
feat: add DiscordPingProvider to check current ping
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmefox committed Oct 17, 2022
1 parent 1f59b91 commit 304652d
Showing 1 changed file with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2022 Viascom Ltd liab. Co
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.viascom.discord.bot.aluna.bot.command.systemcommand

import io.viascom.discord.bot.aluna.bot.Interaction
import io.viascom.discord.bot.aluna.bot.command.SystemCommand
import io.viascom.discord.bot.aluna.configuration.condition.ConditionalOnJdaEnabled
import io.viascom.discord.bot.aluna.configuration.condition.ConditionalOnSystemCommandEnabled
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.InteractionHook
import java.awt.Color
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit

@Interaction
@ConditionalOnJdaEnabled
@ConditionalOnSystemCommandEnabled
class DiscordPingProvider : SystemCommandDataProvider(
"discord_ping",
"Get Discord Ping",
true,
true,
false,
false
) {
override fun execute(event: SlashCommandInteractionEvent, hook: InteractionHook?, command: SystemCommand) {
val builder = EmbedBuilder()

builder.setTitle("Discord Connection")
.setColor(Color.ORANGE)
.setDescription("\uD83D\uDCE1 Pinging...")

val hook = event.deferReply().complete()

val gatewayPing: Long = event.jda.gatewayPing
val now = LocalDateTime.now()
val apiPing = event.jda.restPing.complete()

hook.editOriginalEmbeds(builder.build()).queue { msg: Message ->
builder.setTitle("Discord Connection")
.setDescription(
"❯ **Gateway Ping:** `${gatewayPing}ms`\n" +
"❯ **Api Ping:** `${apiPing}ms`\n" +
"❯ **Roundtrip Latency:** `${ChronoUnit.MILLIS.between(now, LocalDateTime.now())}ms`\n"
)
.setColor(Color.GREEN)
.build()
hook.editOriginalEmbeds(builder.build()).queue()
}
}
}

0 comments on commit 304652d

Please sign in to comment.