Skip to content

Commit

Permalink
Merge pull request #1 from MadeByIToncek/itoncek-updates
Browse files Browse the repository at this point in the history
02-04-2022 update
  • Loading branch information
MadeByIToncek authored Apr 2, 2022
2 parents 053ea3d + 16d10ce commit 28a93da
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 96 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ buildNumber.properties
*.log
/.idea
.idea/vcs.xml
.idea/workspace.xml
.idea/workspace.xml
.classpath
.project
.settings/org.eclipse.core.resources.prefs
.settings/org.eclipse.jdt.core.prefs
.settings/org.eclipse.m2e.core.prefs
cml.iml
runtime-cml/
120 changes: 46 additions & 74 deletions src/main/java/cf/itoncek/cml/CML.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,40 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@SuppressWarnings("OptionalGetWithoutIsPresent")
public class CML {

public static final Logger logger = LoggerFactory.getLogger("cf.itoncek.cml.CML");

public static void main(final String[] args) {
// ENVs required:
// - TOKEN
// - V_CHANNEL
// - GUILD_ID
final String token = System.getenv("TOKEN");
final DiscordClient client = DiscordClient.create(token);
final GatewayDiscordClient gateway = client.login().block();

try {
new GlobalCommandRegistrar(client).registerCommands();
} catch (Exception e) {
logger.error(e.toString());
} catch(Exception e) {
logger.error(e.getMessage());
for(StackTraceElement element : e.getStackTrace()) {
logger.error(element.toString());
}
logger.error(e.getLocalizedMessage());
}

assert gateway != null;
gateway.on(ChatInputInteractionEvent.class, event -> {
switch (event.getCommandName()) {
switch(event.getCommandName()) {
case "ping":
return event.reply("Beep boop, I'm awake").withEphemeral(true);
case "jsem":
String id = event.getOption("tabor")
.flatMap(ApplicationCommandInteractionOption::getValue)
.map(ApplicationCommandInteractionOptionValue::asString)
.flatMap(ApplicationCommandInteractionOption :: getValue)
.map(ApplicationCommandInteractionOptionValue :: asString)
.orElse("null");
List<ActionComponent> actionComponentList = new ArrayList<>();
Button yes = Button.success("xyz" + event.getInteraction().getUser().getId().asLong() + "." + id, "Povolit");
Expand All @@ -55,17 +63,18 @@ public static void main(final String[] args) {
actionComponentList.add(no);
ActionRow ac = ActionRow.of(actionComponentList);
MessageCreateRequest msg = MessageCreateRequest.builder().content(event.getInteraction().getUser().getMention() + " požádal o roli <@&" + id + ">").addComponent(ac.getData()).build();
client.getChannelById(Snowflake.of(System.getenv("V_CHANNEL"))).createMessage(msg).block();
client.getChannelById(Snowflake.of(Long.parseLong(System.getenv("V_CHANNEL")))).createMessage(msg).block();
return event.reply("Žádost o roli <@&" + id + "> byla odeslána").withEphemeral(true);
case "ban":
if (event.getInteraction().getGuild().block().getMemberById(event.getInteraction().getUser().getId()).block().getBasePermissions().block().contains(Permission.BAN_MEMBERS)) {
User target = event.getOption("user")
.flatMap(ApplicationCommandInteractionOption::getValue)
.map(ApplicationCommandInteractionOptionValue::asUser)
.orElse(null)
if(Objects.requireNonNull(Objects.requireNonNull(Objects.requireNonNull(event.getInteraction().getGuild().block()).getMemberById(event.getInteraction().getUser().getId()).block()).getBasePermissions().block()).contains(Permission.BAN_MEMBERS)) {
User target = Objects.requireNonNull(event.getOption("user")
.flatMap(ApplicationCommandInteractionOption :: getValue)
.map(ApplicationCommandInteractionOptionValue :: asUser)
.orElse(null))
.block();
assert target != null;
logger.info(target.getUsername());
event.getInteraction().getGuild().block().getMemberById(target.getId()).block().ban(BanQuerySpec.builder().reason("Banned by CML").build()).block();
Objects.requireNonNull(Objects.requireNonNull(event.getInteraction().getGuild().block()).getMemberById(target.getId()).block()).ban(BanQuerySpec.builder().reason("Banned by CML").build()).block();
return event.reply(target.getUsername() + " byl zabanován.").withEphemeral(true);
} else {
return event.reply("""
Expand Down Expand Up @@ -94,13 +103,14 @@ public static void main(final String[] args) {

}
case "kick":
if (event.getInteraction().getGuild().block().getMemberById(event.getInteraction().getUser().getId()).block().getBasePermissions().block().contains(Permission.KICK_MEMBERS)) {
User trg = event.getOption("user")
.flatMap(ApplicationCommandInteractionOption::getValue)
.map(ApplicationCommandInteractionOptionValue::asUser)
.orElse(null)
if(Objects.requireNonNull(Objects.requireNonNull(Objects.requireNonNull(event.getInteraction().getGuild().block()).getMemberById(event.getInteraction().getUser().getId()).block()).getBasePermissions().block()).contains(Permission.KICK_MEMBERS)) {
User trg = Objects.requireNonNull(event.getOption("user")
.flatMap(ApplicationCommandInteractionOption :: getValue)
.map(ApplicationCommandInteractionOptionValue :: asUser)
.orElse(null))
.block();
event.getInteraction().getGuild().block().getMemberById(trg.getId()).block().kick("Banned by CML").block();
assert trg != null;
Objects.requireNonNull(Objects.requireNonNull(event.getInteraction().getGuild().block()).getMemberById(trg.getId()).block()).kick("Banned by CML").block();
return event.reply(trg.getUsername() + " byl vyhozen.").withEphemeral(true);
} else {
return event.reply("""
Expand Down Expand Up @@ -129,7 +139,7 @@ public static void main(final String[] args) {
}
case "rolepurge":

if (event.getInteraction().getGuild().block().getMemberById(event.getInteraction().getUser().getId()).block().getBasePermissions().block().contains(Permission.MANAGE_ROLES)) {
if(Objects.requireNonNull(Objects.requireNonNull(Objects.requireNonNull(event.getInteraction().getGuild().block()).getMemberById(event.getInteraction().getUser().getId()).block()).getBasePermissions().block()).contains(Permission.MANAGE_ROLES)) {
methodThatTakesALongTime(event);
return event.deferReply().withEphemeral(true);

Expand Down Expand Up @@ -158,79 +168,41 @@ public static void main(final String[] args) {
⠄⠄⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣶⣾⣿⣿⡿⠟⠄⠄
⠠⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄""").withEphemeral(true);
}
/*case "timeout":
if(Objects.requireNonNull(event.getInteraction().getGuild().block().getMemberById(event.getInteraction().getUser().getId()).block()).getBasePermissions().block().contains(Permission.BAN_MEMBERS)) {
User timeout = event.getOption("user")
.flatMap(ApplicationCommandInteractionOption::getValue)
.map(ApplicationCommandInteractionOptionValue::asUser)
.orElse(null)
.block();
Long minutes = event.getOption("time")
.flatMap(ApplicationCommandInteractionOption::getValue)
.map(ApplicationCommandInteractionOptionValue::asLong)
.orElse(null);
logger.info(timeout.getUsername() + timeout.getDiscriminator());
logger.info(String.valueOf(Duration.ofMinutes(minutes)));
client.getGuildById(event.getInteraction().getGuild().block().getId()).getMember(timeout.getId()).block().user().
MemberData to = client.getGuildById(event.getInteraction().getGuild().block().getId()).getMember(timeout.getId()).timeout(Duration.ofMinutes(minutes)).block();
return event.reply(timeout.getUsername() + " byl pozastaven.").withEphemeral(true);
} else {
Long minutes = event.getOption("time").get().getValue().get().asLong();
logger.info(String.valueOf(minutes));
gateway.getGuildById(event.getInteraction().getGuild().block().getId()).block().getMemberById(event.getInteraction().getUser().getId()).timeout(Duration.ofMinutes(minutes)).block();
return event.reply("⠐⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠂\n" +
"⠄⠄⣰⣾⣿⣿⣿⠿⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣆⠄⠄\n" +
"⠄⠄⣿⣿⣿⡿⠋⠄⡀⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⠋⣉⣉⣉⡉⠙⠻⣿⣿⠄⠄\n" +
"⠄⠄⣿⣿⣿⣇⠔⠈⣿⣿⣿⣿⣿⡿⠛⢉⣤⣶⣾⣿⣿⣿⣿⣿⣿⣦⡀⠹⠄⠄\n" +
"⠄⠄⣿⣿⠃⠄⢠⣾⣿⣿⣿⠟⢁⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠄⠄\n" +
"⠄⠄⣿⣿⣿⣿⣿⣿⣿⠟⢁⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠄⠄\n" +
"⠄⠄⣿⣿⣿⣿⣿⡟⠁⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠄⠄\n" +
"⠄⠄⣿⣿⣿⣿⠋⢠⣾⣿⣿⣿⣿⣿⣿⡿⠿⠿⠿⠿⣿⣿⣿⣿⣿⣿⣿⣿⠄⠄\n" +
"⠄⠄⣿⣿⡿⠁⣰⣿⣿⣿⣿⣿⣿⣿⣿⠗⠄⠄⠄⠄⣿⣿⣿⣿⣿⣿⣿⡟⠄⠄\n" +
"⠄⠄⣿⡿⠁⣼⣿⣿⣿⣿⣿⣿⡿⠋⠄⠄⠄⣠⣄⢰⣿⣿⣿⣿⣿⣿⣿⠃⠄⠄\n" +
"⠄⠄⡿⠁⣼⣿⣿⣿⣿⣿⣿⣿⡇⠄⢀⡴⠚⢿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢠⠄⠄\n" +
"⠄⠄⠃⢰⣿⣿⣿⣿⣿⣿⡿⣿⣿⠴⠋⠄⠄⢸⣿⣿⣿⣿⣿⣿⣿⡟⢀⣾⠄⠄\n" +
"⠄⠄⢀⣿⣿⣿⣿⣿⣿⣿⠃⠈⠁⠄⠄⢀⣴⣿⣿⣿⣿⣿⣿⣿⡟⢀⣾⣿⠄⠄\n" +
"⠄⠄⢸⣿⣿⣿⣿⣿⣿⣿⠄⠄⠄⠄⢶⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣾⣿⣿⠄⠄\n" +
"⠄⠄⣿⣿⣿⣿⣿⣿⣿⣷⣶⣶⣶⣶⣶⣿⣿⣿⣿⣿⣿⣿⠋⣠⣿⣿⣿⣿⠄⠄\n" +
"⠄⠄⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢁⣼⣿⣿⣿⣿⣿⠄⠄\n" +
"⠄⠄⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢁⣴⣿⣿⣿⣿⣿⣿⣿⠄⠄\n" +
"⠄⠄⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⢁⣴⣿⣿⣿⣿⠗⠄⠄⣿⣿⠄⠄\n" +
"⠄⠄⣆⠈⠻⢿⣿⣿⣿⣿⣿⣿⠿⠛⣉⣤⣾⣿⣿⣿⣿⣿⣇⠠⠺⣷⣿⣿⠄⠄\n" +
"⠄⠄⣿⣿⣦⣄⣈⣉⣉⣉⣡⣤⣶⣿⣿⣿⣿⣿⣿⣿⣿⠉⠁⣀⣼⣿⣿⣿⠄⠄\n" +
"⠄⠄⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣶⣾⣿⣿⡿⠟⠄⠄\n" +
"⠠⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄").withEphemeral(true);
}*/
}
return event.reply("c");
return event.reply("c");
}).subscribe();

gateway.on(ButtonInteractionEvent.class, event -> {
String id = event.getCustomId();
if (id.startsWith("rjc")) {
event.getMessage().get().delete("Denied");
if(id.startsWith("rjc")) {
if(event.getMessage().isPresent()) {
event.getMessage().get().delete("Denied");
}
return event.reply("Uživatel byl odmítnut").withEphemeral(true);
} else if (id.startsWith("xyz")) {
} else if(id.startsWith("xyz")) {
id = id.substring(3);
String userID = id.split("[.]")[0];
String roleID = id.split("[.]")[1];
Guild guild = event.getInteraction().getGuild().block();
event.getMessage().get().delete("Allowed").block();
guild.getMemberById(Snowflake.of(userID)).block().addRole(Snowflake.of(roleID)).block();
return event.reply(gateway.getUserById(Snowflake.of(userID)).block().getUsername() + " byl povolen přístup k kanálům tábora '" + event.getInteraction().getGuild().block().getRoleById(Snowflake.of(roleID)).block().getName() + "'");
assert guild != null;
Objects.requireNonNull(guild.getMemberById(Snowflake.of(userID)).block()).addRole(Snowflake.of(roleID)).block();
return event.reply(Objects.requireNonNull(gateway.getUserById(Snowflake.of(userID)).block()).getUsername() + " byl povolen přístup k kanálům tábora '" + Objects.requireNonNull(Objects.requireNonNull(event.getInteraction().getGuild().block()).getRoleById(Snowflake.of(roleID)).block()).getName() + "'");
}
return event.reply("test").withEphemeral(true);
}).subscribe();

gateway.onDisconnect().block();
}

private static void methodThatTakesALongTime(ChatInputInteractionEvent event) {
User target = event.getOption("user")
.flatMap(ApplicationCommandInteractionOption::getValue)
.map(ApplicationCommandInteractionOptionValue::asUser)
.orElse(null)
User target = Objects.requireNonNull(event.getOption("user")
.flatMap(ApplicationCommandInteractionOption :: getValue)
.map(ApplicationCommandInteractionOptionValue :: asUser)
.orElse(null))
.block();
event.getInteraction().getGuild().block().getMemberById(target.getId()).block().getRoleIds().forEach(snowflake -> event.getInteraction().getGuild().block().getMemberById(target.getId()).block().removeRole(snowflake).block());
assert target != null;
Objects.requireNonNull(Objects.requireNonNull(event.getInteraction().getGuild().block()).getMemberById(target.getId()).block()).getRoleIds().forEach(snowflake -> Objects.requireNonNull(Objects.requireNonNull(event.getInteraction().getGuild().block()).getMemberById(target.getId()).block()).removeRole(snowflake).block());
event.createFollowup("This took awhile but I'm done!");
}
}
Loading

0 comments on commit 28a93da

Please sign in to comment.