Skip to content

Commit

Permalink
Merge pull request #24 from JonasFranke/devNoUpdateEnv
Browse files Browse the repository at this point in the history
Dev no update env
  • Loading branch information
JonasFranke authored Aug 14, 2023
2 parents 2a597c7 + 5666206 commit 22b40a9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ View the DockerHub page [here](https://hub.docker.com/r/jnsfrnk/ddsb).
- Use the ```/ds``` command to see all running containers
- Use the ```/stopupdating <TRUE/FALSE>``` command to stop the bot from updating the status

---

#### Environment variables

| Name | Default | Values |
|---------------------|---------|------------------------|
| ```DISCORD_TOKEN``` | none | Your discord bot token |
| ```NO_UPDATE``` | false | true/false |
18 changes: 16 additions & 2 deletions src/main/java/tech/jonasfranke/ddsb/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Main {

private static Logger logger = LoggerFactory.getLogger(Main.class);
private static boolean cancelThreads = false;
private static boolean noUpdate = false;
private static DiscordClient client;
private static GatewayDiscordClient gateway;

Expand All @@ -35,6 +36,15 @@ public static void main(String[] args) {
/// Map contains the message id as key and the channel id as value
final HashMap<Snowflake, Snowflake> messageIds = new HashMap<>();

if (System.getenv("NO_UPDATE") != null) {
if (System.getenv("NO_UPDATE").equalsIgnoreCase("true") || System.getenv("NO_UPDATE").equalsIgnoreCase("false")) {
noUpdate = Boolean.parseBoolean(System.getenv("NO_UPDATE"));
logger.info("NO_UPDATE is set to " + noUpdate);
} else {
throw new RuntimeException("Could not parse NO_UPDATE env variable");
}
}

assert gateway != null;
gateway.on(MessageCreateEvent.class).subscribe(event -> {
final Message message = event.getMessage();
Expand All @@ -46,8 +56,8 @@ public static void main(String[] args) {
}
case "just a second..." -> {
if (message.getAuthor().isPresent() && message.getAuthor().get().isBot()) {
final Message embed = event.getMessage().getChannel().block().createMessage(MessageCreateSpec.builder().addEmbed(new DockerManager().createDockerEmbed(message.getGuildId().get(), true)).build()).block();
if (!cancelThreads) {
final Message embed = event.getMessage().getChannel().block().createMessage(MessageCreateSpec.builder().addEmbed(new DockerManager().createDockerEmbed(message.getGuildId().get(), true, isNoUpdate())).build()).block();
if (!cancelThreads && !isNoUpdate()) {
assert embed != null;
UpdateEmbedThread thread = new UpdateEmbedThread(client, embed.getId(), messageIds, message.getGuildId().get());
if (LatestMessageUpdater.hasRunningThread(embed.getChannelId())) {
Expand Down Expand Up @@ -134,6 +144,10 @@ public static DiscordClient getClient() {
return client;
}

public static boolean isNoUpdate() {
return noUpdate;
}

public static GatewayDiscordClient getGateway() {
return gateway;
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/tech/jonasfranke/ddsb/util/DockerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ public EmbedCreateSpec createDockerEmbed(Snowflake guildID, boolean running) {
builder.footer("Last updated: " + formatter.format(now.getTime()) + " (Stopped thread for updating this message)", null);


return builder.build();
}
public EmbedCreateSpec createDockerEmbed(Snowflake guildID, boolean running, boolean isNoUpdate) {
handleDocker();
EmbedCreateSpec.Builder builder = EmbedCreateSpec.builder()
.color(Color.BLUE)
.title("Docker Status")
.description("This is the status of the docker containers")
.addField("\u200B", "\u200B", false)
.addField("\u200B", "\u200B", false);

for (Container containerName : containerNames) {
builder
.addField(CustomEmote.GreenUpArrow.init(guildID, Main.getGateway()).getFullStringOrAlias() + " " + containerName.getNames()[0].replaceFirst("/", ""), /*containerUptime.get(containerName.getId()) +*/ "<t:" + containerUptimeRelative.get(containerName.getId()) + ":R>", false);
}

Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
if (running) {
if (!isNoUpdate) {
builder.footer("Last updated: " + formatter.format(now.getTime()), null);
} else {
builder.footer("Last updated: " + formatter.format(now.getTime()) + " ❌ Update", null);
}

} else
builder.footer("Last updated: " + formatter.format(now.getTime()) + " (Stopped thread for updating this message)", null);


return builder.build();
}
}

0 comments on commit 22b40a9

Please sign in to comment.