Archiviert
1
0

Add EventManager

Dieser Commit ist enthalten in:
yoyosource 2021-07-28 16:45:25 +02:00
Ursprung da13f8d546
Commit 8bab33942e
3 geänderte Dateien mit 115 neuen und 114 gelöschten Zeilen

Datei anzeigen

@ -21,27 +21,23 @@ package de.steamwar.bungeecore.bot;
import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.BungeeCore;
import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig;
import de.steamwar.bungeecore.bot.events.EventManager;
import de.steamwar.bungeecore.bot.listeners.RolesInteractionButtonListener; import de.steamwar.bungeecore.bot.listeners.RolesInteractionButtonListener;
import de.steamwar.bungeecore.bot.util.DiscordRolesMessage; import de.steamwar.bungeecore.bot.util.DiscordRolesMessage;
import de.steamwar.bungeecore.bot.util.DiscordRulesMessage; import de.steamwar.bungeecore.bot.util.DiscordRulesMessage;
import de.steamwar.bungeecore.sql.Event; import de.steamwar.bungeecore.sql.Event;
import de.steamwar.bungeecore.sql.EventFight; import de.steamwar.bungeecore.sql.Team;
import de.steamwar.bungeecore.sql.TeamTeilnahme;
import lombok.Getter; import lombok.Getter;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.ActivityFlag;
import net.dv8tion.jda.api.entities.RichPresence;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.internal.entities.EntityBuilder; import net.dv8tion.jda.internal.entities.EntityBuilder;
import net.dv8tion.jda.internal.entities.SelfUserImpl;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class SteamwarDiscordBot { public class SteamwarDiscordBot {
@ -58,112 +54,6 @@ public class SteamwarDiscordBot {
public SteamwarDiscordBot() { public SteamwarDiscordBot() {
INSTANCE = this; INSTANCE = this;
JDABuilder builder = JDABuilder.createDefault(SteamwarDiscordBotConfig.TOKEN); JDABuilder builder = JDABuilder.createDefault(SteamwarDiscordBotConfig.TOKEN);
/*builder.setActivity(new RichPresence() {
@Override
public long getApplicationIdLong() {
return 869611389818400779L;
}
@NotNull
@Override
public String getApplicationId() {
return "869611389818400779";
}
@Nullable
@Override
public String getSessionId() {
return null;
}
@Nullable
@Override
public String getSyncId() {
return null;
}
@Override
public int getFlags() {
return ActivityFlag.PLAY.getRaw();
}
@Override
public EnumSet<ActivityFlag> getFlagSet() {
return EnumSet.of(ActivityFlag.PLAY);
}
@Nullable
@Override
public String getState() {
return "Steamwar.de";
}
@Nullable
@Override
public String getDetails() {
return "Spielt auf Steamwar.de";
}
@Nullable
@Override
public Party getParty() {
return new Party(null, 1, 100);
}
@Nullable
@Override
public Image getLargeImage() {
return null;
}
@Nullable
@Override
public Image getSmallImage() {
return null;
}
@Override
public boolean isRich() {
return true;
}
@Nullable
@Override
public RichPresence asRichPresence() {
return this;
}
@NotNull
@Override
public String getName() {
return "SteamWar";
}
@Nullable
@Override
public String getUrl() {
return null;
}
@NotNull
@Override
public ActivityType getType() {
return ActivityType.DEFAULT;
}
@Nullable
@Override
public Timestamps getTimestamps() {
return null;
}
@Nullable
@Override
public Emoji getEmoji() {
return null;
}
});*/
builder.setStatus(OnlineStatus.ONLINE); builder.setStatus(OnlineStatus.ONLINE);
try { try {
jda = builder.build(); jda = builder.build();
@ -176,7 +66,11 @@ public class SteamwarDiscordBot {
e.printStackTrace(); e.printStackTrace();
} }
activity(); activity();
ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), this::activity, 30, 30, TimeUnit.SECONDS); EventManager.update();
ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> {
activity();
EventManager.update();
}, 30, 30, TimeUnit.SECONDS);
DiscordRolesMessage.sendMessage(); DiscordRolesMessage.sendMessage();
DiscordRulesMessage.sendMessage(); DiscordRulesMessage.sendMessage();

Datei anzeigen

@ -28,6 +28,7 @@ public class SteamwarDiscordBotConfig {
public static String TOKEN; public static String TOKEN;
public static String GUILD; public static String GUILD;
public static String EVENTS_CHANNEL;
public static String ROLES_CHANNEL; public static String ROLES_CHANNEL;
public static String ROLES_BASE_MESSAGE; public static String ROLES_BASE_MESSAGE;
public static String ROLES_ADDED; public static String ROLES_ADDED;
@ -41,6 +42,7 @@ public class SteamwarDiscordBotConfig {
public static void loadConfig(Configuration config) { public static void loadConfig(Configuration config) {
TOKEN = config.getString("token"); TOKEN = config.getString("token");
GUILD = config.getString("guild"); GUILD = config.getString("guild");
EVENTS_CHANNEL = config.getString("events-channel");
Configuration rolesSection = config.getSection("roles-claim"); Configuration rolesSection = config.getSection("roles-claim");
ROLES_CHANNEL = rolesSection.getString("channel"); ROLES_CHANNEL = rolesSection.getString("channel");
ROLES_BASE_MESSAGE = rolesSection.getString("base"); ROLES_BASE_MESSAGE = rolesSection.getString("base");

Datei anzeigen

@ -0,0 +1,105 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bungeecore.bot.events;
import de.steamwar.bungeecore.bot.SteamwarDiscordBot;
import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig;
import de.steamwar.bungeecore.sql.Event;
import de.steamwar.bungeecore.sql.EventFight;
import de.steamwar.bungeecore.sql.Team;
import lombok.experimental.UtilityClass;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import java.awt.*;
import java.sql.Timestamp;
import java.time.Instant;
@UtilityClass
public class EventManager {
private Message message;
private TextChannel textChannel;
static {
textChannel = SteamwarDiscordBot.instance().getJda().getGuildById(SteamwarDiscordBotConfig.GUILD).getTextChannelById(SteamwarDiscordBotConfig.EVENTS_CHANNEL);
assert textChannel != null;
if(textChannel.hasLatestMessage()) {
textChannel.getIterableHistory().complete().forEach(message -> message.delete().complete());
}
}
public void update() {
if (Event.get() == null) {
updateComing();
} else {
updateCurrent();
}
}
private void updateComing() {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Color.GRAY);
embedBuilder.setTitle("Zukünftige Events");
embedBuilder.setAuthor("SteamWar", "https://www.steamwar.de");
Timestamp now = Timestamp.from(Instant.now());
Event.getComing().forEach(event -> {
StringBuilder st = new StringBuilder();
if (event.getDeadline().after(now)) {
st.append("Deadline: <t:").append(event.getDeadline().getTime()).append(":R>\n");
}
st.append("Start: <t:").append(event.getStart().getTime()).append(":R>");
embedBuilder.addField(event.getEventName(), st.toString(), false);
});
MessageBuilder messageBuilder = new MessageBuilder();
messageBuilder.setEmbeds(embedBuilder.build());
if (message == null) {
message = textChannel.sendMessage(messageBuilder.build()).complete();
} else {
message.editMessage(messageBuilder.build()).complete();
}
}
private void updateCurrent() {
Event event = Event.get();
if (event == null) return;
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Color.GRAY);
embedBuilder.setTitle("Event: " + event.getEventName());
embedBuilder.setAuthor("SteamWar", "https://www.steamwar.de");
EventFight.getEvent(event.getEventID()).forEach(eventFight -> {
embedBuilder.addField(Team.get(eventFight.getTeamBlue()) + " vs. " + Team.get(eventFight.getTeamRed()), "<t:" + eventFight.getStartTime().getTime() + ":R>", true);
});
MessageBuilder messageBuilder = new MessageBuilder();
messageBuilder.setEmbeds(embedBuilder.build());
if (message == null) {
message = textChannel.sendMessage(messageBuilder.build()).complete();
} else {
message.editMessage(messageBuilder.build()).complete();
}
}
}