SteamWar/BungeeCore
Archiviert
13
2

Add Steamwar Discord Auth

Dieser Commit ist enthalten in:
Chaoscaot 2021-07-28 20:13:31 +02:00
Ursprung 31a07c30e2
Commit c9a5f468da
10 geänderte Dateien mit 237 neuen und 8 gelöschten Zeilen

Datei anzeigen

@ -126,6 +126,7 @@ public class BungeeCore extends Plugin {
new ResourcereloadCommand(); new ResourcereloadCommand();
new ListCommand(); new ListCommand();
new StatCommand(); new StatCommand();
new VerifyCommand();
if(!EVENT_MODE){ if(!EVENT_MODE){
new WebregisterCommand(); new WebregisterCommand();

Datei anzeigen

@ -0,0 +1,75 @@
/*
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;
import de.steamwar.bungeecore.BungeeCore;
import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig;
import de.steamwar.bungeecore.sql.SteamwarUser;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Emoji;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.Button;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class AuthManager {
private static final Map<String, Long> TOKENS = new HashMap<>();
private static final Random rand = new Random();
public static String createDiscordAuthToken(Member member) {
if(TOKENS.containsValue(member.getIdLong())) return null;
byte[] randBytes = new byte[16];
rand.nextBytes(randBytes);
randBytes[0] = 'D';
randBytes[1] = 'C';
String code = Base64.getEncoder().encodeToString(randBytes);
TOKENS.put(code, member.getIdLong());
BungeeCore.log("Created Discord Auth-Token: " + code + " for: " + member.getUser().getAsTag());
ScheduledTask[] task = new ScheduledTask[1];
task[0] = BungeeCore.get().getProxy().getScheduler().schedule(BungeeCore.get(), () -> {
TOKENS.remove(code);
task[0].cancel();
}, 10, 10, TimeUnit.MINUTES);
return code;
}
public static Member connectAuth(SteamwarUser user, String code) {
if (TOKENS.containsKey(code)) {
Member member = SteamwarDiscordBot.instance().getJda().getGuildById(SteamwarDiscordBotConfig.GUILD).retrieveMemberById(TOKENS.get(code).longValue()).complete();
if(member == null) return null;
user.setDiscordId(member.getId());
MessageBuilder builder = new MessageBuilder();
builder.setContent(":white_check_mark: Dein Discord Konto wurde mit **" + user.getUserName() + "** verknüpft");
builder.setActionRows(ActionRow.of(Button.success("tada", Emoji.fromUnicode("U+1F389")), Button.danger("invalid", "Ich war das nicht")));
member.getUser().openPrivateChannel().complete().sendMessage(builder.build()).complete();
TOKENS.remove(code);
return member;
} else {
return null;
}
}
}

Datei anzeigen

@ -23,6 +23,7 @@ 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.events.EventManager;
import de.steamwar.bungeecore.bot.listeners.AnnouncementListener; import de.steamwar.bungeecore.bot.listeners.AnnouncementListener;
import de.steamwar.bungeecore.bot.listeners.DiscordAuthListener;
import de.steamwar.bungeecore.bot.listeners.DiscordTicketListener; import de.steamwar.bungeecore.bot.listeners.DiscordTicketListener;
import de.steamwar.bungeecore.bot.listeners.RolesInteractionButtonListener; import de.steamwar.bungeecore.bot.listeners.RolesInteractionButtonListener;
import de.steamwar.bungeecore.bot.util.DiscordTicketMessage; import de.steamwar.bungeecore.bot.util.DiscordTicketMessage;
@ -35,6 +36,7 @@ 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.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
@ -58,6 +60,7 @@ public class SteamwarDiscordBot {
INSTANCE = this; INSTANCE = this;
JDABuilder builder = JDABuilder.createDefault(SteamwarDiscordBotConfig.TOKEN); JDABuilder builder = JDABuilder.createDefault(SteamwarDiscordBotConfig.TOKEN);
builder.setStatus(OnlineStatus.ONLINE); builder.setStatus(OnlineStatus.ONLINE);
builder.setMemberCachePolicy(MemberCachePolicy.ONLINE);
try { try {
jda = builder.build(); jda = builder.build();
} catch (LoginException e) { } catch (LoginException e) {
@ -80,6 +83,7 @@ public class SteamwarDiscordBot {
new RolesInteractionButtonListener(); new RolesInteractionButtonListener();
new DiscordTicketListener(); new DiscordTicketListener();
new DiscordAuthListener();
announcementListener = new AnnouncementListener(); announcementListener = new AnnouncementListener();
} }

Datei anzeigen

@ -0,0 +1,59 @@
/*
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.listeners;
import de.steamwar.bungeecore.bot.AuthManager;
import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig;
import de.steamwar.bungeecore.sql.SteamwarUser;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.events.interaction.GenericComponentInteractionCreateEvent;
import net.dv8tion.jda.api.interactions.InteractionType;
import org.jetbrains.annotations.NotNull;
public class DiscordAuthListener extends BasicDiscordListener {
@Override
public void onGenericComponentInteractionCreate(@NotNull GenericComponentInteractionCreateEvent event) {
if(event.getType() == InteractionType.COMPONENT) {
if(event.getChannel().getId().equals(SteamwarDiscordBotConfig.RULES_CHANNEL) && event.getComponentId().equals("auth")) {
String authMessage = AuthManager.createDiscordAuthToken(event.getMember());
if(authMessage != null) {
event.reply("Gebe innerhalb der nächsten 10 Minuten ``/verify " + authMessage + "`` ein!").setEphemeral(true).complete();
} else {
event.reply("Du hast bereits einen Code am laufen").setEphemeral(true).complete();
}
}
if(event.getComponentId().equals("tada") && event.getChannelType() == ChannelType.PRIVATE) {
event.reply(":tada:").setEphemeral(false).complete();
}
if(event.getComponentId().equals("invalid") && event.getChannelType() == ChannelType.PRIVATE) {
SteamwarUser user = SteamwarUser.get(event.getUser().getIdLong());
if(user == null) {
event.reply(":questionmark: Da ist keine verknüpfung?").setEphemeral(false).complete();
} else {
user.setDiscordId(null);
event.reply(":x: Die Verknüpfung wurde beendet").setEphemeral(false).complete();
}
}
}
}
}

Datei anzeigen

@ -29,21 +29,17 @@ import net.dv8tion.jda.api.events.interaction.GenericComponentInteractionCreateE
import net.dv8tion.jda.api.interactions.InteractionType; import net.dv8tion.jda.api.interactions.InteractionType;
import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.Button; import net.dv8tion.jda.api.interactions.components.Button;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.awt.Color; import java.awt.Color;
import java.io.File;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
public class DiscordTicketListener extends BasicDiscordListener { public class DiscordTicketListener extends BasicDiscordListener {
@Override @Override
public void onGenericComponentInteractionCreate(@NotNull GenericComponentInteractionCreateEvent event) { public void onGenericComponentInteractionCreate(@NotNull GenericComponentInteractionCreateEvent event) {
if(event.getType() == InteractionType.COMPONENT && event.getTextChannel().getParent().getId().equals(SteamwarDiscordBotConfig.TICKET_CATEGORY)) { if(event.getType() == InteractionType.COMPONENT && event.getChannelType() == ChannelType.TEXT && event.getTextChannel().getParent() != null && event.getTextChannel().getParent().getId().equals(SteamwarDiscordBotConfig.TICKET_CATEGORY)) {
if(event.getTextChannel().getId().equals(SteamwarDiscordBotConfig.TICKET_CHANNEL) && SteamwarDiscordBotConfig.TICKET_TYPES.containsKey(event.getComponentId())) { if(event.getTextChannel().getId().equals(SteamwarDiscordBotConfig.TICKET_CHANNEL) && SteamwarDiscordBotConfig.TICKET_TYPES.containsKey(event.getComponentId())) {
DiscordTicketType ticketType = SteamwarDiscordBotConfig.TICKET_TYPES.get(event.getComponentId()); DiscordTicketType ticketType = SteamwarDiscordBotConfig.TICKET_TYPES.get(event.getComponentId());
Category ct = event.getGuild().getCategoryById(SteamwarDiscordBotConfig.TICKET_CATEGORY); Category ct = event.getGuild().getCategoryById(SteamwarDiscordBotConfig.TICKET_CATEGORY);

Datei anzeigen

@ -20,6 +20,7 @@
package de.steamwar.bungeecore.bot.listeners; package de.steamwar.bungeecore.bot.listeners;
import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.events.interaction.GenericComponentInteractionCreateEvent; import net.dv8tion.jda.api.events.interaction.GenericComponentInteractionCreateEvent;
import net.dv8tion.jda.api.interactions.InteractionType; import net.dv8tion.jda.api.interactions.InteractionType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -28,7 +29,7 @@ public class RolesInteractionButtonListener extends BasicDiscordListener {
@Override @Override
public void onGenericComponentInteractionCreate(@NotNull GenericComponentInteractionCreateEvent event) { public void onGenericComponentInteractionCreate(@NotNull GenericComponentInteractionCreateEvent event) {
if(event.getType() == InteractionType.COMPONENT && event.getTextChannel().getId().equals(SteamwarDiscordBotConfig.ROLES_CHANNEL) && SteamwarDiscordBotConfig.ROLES.stream().anyMatch(discordRole -> discordRole.getRoleId().equals(event.getComponentId()))) { if(event.getType() == InteractionType.COMPONENT && event.getChannelType() == ChannelType.TEXT && event.getTextChannel().getId().equals(SteamwarDiscordBotConfig.ROLES_CHANNEL) && SteamwarDiscordBotConfig.ROLES.stream().anyMatch(discordRole -> discordRole.getRoleId().equals(event.getComponentId()))) {
if (event.getMember().getRoles().stream().anyMatch(role -> role.getId().equals(event.getComponentId()))) { if (event.getMember().getRoles().stream().anyMatch(role -> role.getId().equals(event.getComponentId()))) {
event.getGuild().removeRoleFromMember(event.getMember(), event.getGuild().getRoleById(event.getComponentId())).complete(); event.getGuild().removeRoleFromMember(event.getMember(), event.getGuild().getRoleById(event.getComponentId())).complete();
event.reply(SteamwarDiscordBotConfig.ROLES_REMOVED.replace("%role%", event.getGuild().getRoleById(event.getComponentId()).getAsMention())).setEphemeral(true).complete(); event.reply(SteamwarDiscordBotConfig.ROLES_REMOVED.replace("%role%", event.getGuild().getRoleById(event.getComponentId()).getAsMention())).setEphemeral(true).complete();

Datei anzeigen

@ -23,6 +23,7 @@ import de.steamwar.bungeecore.bot.SteamwarDiscordBot;
import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Emoji;
import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.Button; import net.dv8tion.jda.api.interactions.components.Button;
@ -49,9 +50,11 @@ public class DiscordRulesMessage {
List<Button> buttons = new ArrayList<>(); List<Button> buttons = new ArrayList<>();
SteamwarDiscordBotConfig.RULES_LINKS.forEach(discordRulesLink -> buttons.add(discordRulesLink.toButton())); SteamwarDiscordBotConfig.RULES_LINKS.forEach(discordRulesLink -> buttons.add(discordRulesLink.toButton()));
Button authButton = Button.primary("auth", Emoji.fromUnicode("U+2705")).withLabel("Minecraft verknüpfen");
MessageBuilder messageBuilder = new MessageBuilder(); MessageBuilder messageBuilder = new MessageBuilder();
messageBuilder.setEmbeds(builder.build()); messageBuilder.setEmbeds(builder.build());
messageBuilder.setActionRows(ActionRow.of(buttons)); messageBuilder.setActionRows(ActionRow.of(buttons), ActionRow.of(authButton));
channel.sendMessage(messageBuilder.build()).complete(); channel.sendMessage(messageBuilder.build()).complete();
} }
} }

Datei anzeigen

@ -0,0 +1,60 @@
/*
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.commands;
import de.steamwar.bungeecore.BungeeCore;
import de.steamwar.bungeecore.Message;
import de.steamwar.bungeecore.bot.AuthManager;
import de.steamwar.bungeecore.sql.SteamwarUser;
import net.dv8tion.jda.api.entities.Member;
import net.md_5.bungee.api.CommandSender;
import java.util.Base64;
public class VerifyCommand extends BasicCommand {
public VerifyCommand() {
super("verify", "");
}
@Override
public void execute(CommandSender sender, String[] strings) {
if(strings.length < 1) {
Message.send("VERIFY_USAGE", sender);
return;
}
byte[] bytes = Base64.getDecoder().decode(strings[0]);
if(bytes.length != 16) {
Message.send("VERIFY_INVALID", sender);
return;
}
if(bytes[0] == 'D' && bytes[1] == 'C') {
Member member = AuthManager.connectAuth(SteamwarUser.get(sender.getName()), strings[0]);
if(member != null) {
BungeeCore.log(sender.getName() + " Verified with Discorduser: " + member.getIdLong());
Message.send("VERIFY_SUCCESS", sender, member.getUser().getAsTag());
} else {
Message.send("VERIFY_INVALID", sender);
}
} else {
Message.send("VERIFY_INVALID", sender);
}
}
}

Datei anzeigen

@ -52,10 +52,12 @@ public class SteamwarUser {
private int team; private int team;
private boolean leader; private boolean leader;
private Map<Punishment.PunishmentType, Punishment> punishments; private Map<Punishment.PunishmentType, Punishment> punishments;
private String discordId;
private static final Map<String, SteamwarUser> usersByName = new HashMap<>(); private static final Map<String, SteamwarUser> usersByName = new HashMap<>();
private static final Map<UUID, SteamwarUser> usersByUUID = new HashMap<>(); private static final Map<UUID, SteamwarUser> usersByUUID = new HashMap<>();
private static final Map<Integer, SteamwarUser> usersById = new HashMap<>(); private static final Map<Integer, SteamwarUser> usersById = new HashMap<>();
private static final Map<String, SteamwarUser> usersByDiscord = new HashMap<>();
private static final InetAddress LIXFEL_DE; private static final InetAddress LIXFEL_DE;
private static final String API_URL = "https://api.mojang.com/users/profiles/minecraft/"; private static final String API_URL = "https://api.mojang.com/users/profiles/minecraft/";
private static final JsonParser jsonParser = new JsonParser(); private static final JsonParser jsonParser = new JsonParser();
@ -76,9 +78,12 @@ public class SteamwarUser {
userGroup = UserGroup.getUsergroup(rs.getString("UserGroup")); userGroup = UserGroup.getUsergroup(rs.getString("UserGroup"));
team = rs.getInt("Team"); team = rs.getInt("Team");
leader = rs.getBoolean("Leader"); leader = rs.getBoolean("Leader");
discordId = rs.getString("DiscordId");
usersById.put(id, this); usersById.put(id, this);
usersByName.put(userName.toLowerCase(), this); usersByName.put(userName.toLowerCase(), this);
usersByUUID.put(uuid, this); usersByUUID.put(uuid, this);
if(discordId != null)
usersByDiscord.put(discordId, this);
punishments = Punishment.getPunishmentsOfPlayer(id); punishments = Punishment.getPunishmentsOfPlayer(id);
} }
@ -146,10 +151,17 @@ public class SteamwarUser {
return dbInit(SQL.select("SELECT * FROM UserData WHERE id = ?", id)); return dbInit(SQL.select("SELECT * FROM UserData WHERE id = ?", id));
} }
public static SteamwarUser get(Long discordId) {
if(usersByDiscord.containsKey(discordId.toString()))
return usersByDiscord.get(discordId.toString());
return dbInit(SQL.select("SELECT * FROM UserData WHERE DiscordId = ?", discordId));
}
public static void clearCache(){ public static void clearCache(){
usersById.clear(); usersById.clear();
usersByName.clear(); usersByName.clear();
usersByUUID.clear(); usersByUUID.clear();
usersByDiscord.clear();
} }
public static UUID loadUUID(String playerName) { public static UUID loadUUID(String playerName) {
@ -198,6 +210,19 @@ public class SteamwarUser {
return punishments.getOrDefault(type, null); return punishments.getOrDefault(type, null);
} }
public String getDiscordId() {
return discordId;
}
public void setDiscordId(String discordId) {
usersByDiscord.remove(this.discordId);
this.discordId = discordId;
SQL.update("Update UserData SET DiscordId = ? WHERE id = ?", discordId, id);
if(discordId != null) {
usersByDiscord.put(discordId, this);
}
}
public boolean isBanned() { public boolean isBanned() {
if(!punishments.containsKey(Punishment.PunishmentType.Ban)) if(!punishments.containsKey(Punishment.PunishmentType.Ban))
return false; return false;

Datei anzeigen

@ -511,4 +511,9 @@ WHOIS_TEAM=§7Team§8: §e[§{0}{1}§e] {2}
WHOIS_TEAM_HOVER=§e{0} anzeigen WHOIS_TEAM_HOVER=§e{0} anzeigen
WHOIS_PUNISHMENTS=§7Strafen: WHOIS_PUNISHMENTS=§7Strafen:
WHOIS_PUNISHMENT=§7{0}§8» §f§l{1}: §e{2} - {3} §f{4} WHOIS_PUNISHMENT=§7{0}§8» §f§l{1}: §e{2} - {3} §f{4}
WHOIS_NO_PUNISHMENT=§a✓ §7Der Spieler hat noch nichts getan. WHOIS_NO_PUNISHMENT=§a✓ §7Der Spieler hat noch nichts getan.
#VerifyCommand
VERIFY_USAGE=§c/verify [Code]
VERIFY_INVALID=§cInvalider Code
VERIFY_SUCCESS=§eErfolgreich mit dem Discord Account {0} verknüpft