Skip to content

Commit

Permalink
feat(/profile) add Lichess username auto complete fixes #43
Browse files Browse the repository at this point in the history
  • Loading branch information
jalpp authored May 10, 2024
1 parent e6db6c8 commit 64178bd
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Chesslise is open source Chess based Discord application, which allows users to
## Discord Support Server
[Join](https://discord.gg/uncmhknmYg)

## Version number: 13.6
## Version number: 13.7

## Road map for chesslise
- add chessxplain API
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/Abstraction/Context/ContextHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import chariot.Client;
import com.github.bhlangonijr.chesslib.Board;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;

public interface ContextHandler {


void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, Client client, Board board, Board blackboard
void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, CommandAutoCompleteInteractionEvent autoEvent , Client client, Board board, Board blackboard
, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit);


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package Discord.HandlerModules;

import Abstraction.Context.ContextHandler;
import Discord.HelperModules.AutoCompleteHelperModule;
import Discord.MainHandler.AntiSpam;
import chariot.Client;
import com.github.bhlangonijr.chesslib.Board;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;

public class AutoCompleteContextModule implements ContextHandler {


@Override
public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, CommandAutoCompleteInteractionEvent autoEvent, Client client, Board board, Board blackboard, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit) {

AutoCompleteHelperModule autoHelper = new AutoCompleteHelperModule();

if(autoEvent.getName().equalsIgnoreCase("profile")){
autoHelper.onLichessProfileAutoComplete(autoEvent, client);
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import chariot.Client;
import com.github.bhlangonijr.chesslib.Board;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
Expand All @@ -23,7 +24,7 @@ public ButtonContextModule() {
}

@Override
public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, Client client, Board board, Board blackboard, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit) {
public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, CommandAutoCompleteInteractionEvent autoEvent, Client client, Board board, Board blackboard, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit) {
BotContextModule fish = new BotContextModule();

CommandInfo commandInfo = new CommandInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import chariot.Client;
import com.github.bhlangonijr.chesslib.Board;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
Expand All @@ -20,7 +21,7 @@ public MessageContextModule() {
}

@Override
public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, Client client, Board board, Board blackboard, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit) {
public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, CommandAutoCompleteInteractionEvent autoEvent, Client client, Board board, Board blackboard, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit) {

ToolContextModule tools = new ToolContextModule();
boolean isSlash = false;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/Discord/HandlerModules/ModalContextModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
import chariot.Client;
import com.github.bhlangonijr.chesslib.Board;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;

public class ModalContextModule implements ContextHandler {

@Override
public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, Client client, Board board, Board blackboard, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit) {
public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, CommandAutoCompleteInteractionEvent autoEvent, Client client, Board board, Board blackboard, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit) {

String name = eventModal.getModalId();
ModalHelperContextModule modalHelper = new ModalHelperContextModule();

switch (name) {
case "modalwatch" -> modalHelper.handleGameInputOnFormSubmit(eventModal, client);
case "modalpro" -> modalHelper.handleLichessProfileOnFormSubmit(eventModal, client);
case "modalproc" -> modalHelper.handleChessComProfileOnFormSubmit(eventModal);
case "modal-self-user" -> modalHelper.handlePlayFriendChallenge(eventModal, client, new Game());
}
}

}
}
8 changes: 6 additions & 2 deletions src/main/java/Discord/HandlerModules/SlashContextModule.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Discord.HandlerModules;

import Abstraction.Context.ContextHandler;
import Discord.HelperModules.AutoCompleteHelperModule;
import Discord.HelperModules.BotContextModule;
import Discord.HelperModules.ModalHelperContextModule;
import Discord.HelperModules.ToolContextModule;
Expand All @@ -9,6 +10,7 @@
import chariot.Client;
import com.github.bhlangonijr.chesslib.Board;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
Expand All @@ -21,12 +23,13 @@ public class SlashContextModule implements ContextHandler {


@Override
public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, Client client, Board board, Board blackboard, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit) {
public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, CommandAutoCompleteInteractionEvent autoEvent, Client client, Board board, Board blackboard, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit) {
String name = slashEvent.getName();
BotContextModule botCommands = new BotContextModule();
CommandInfo info_sender = new CommandInfo();
ToolContextModule tools = new ToolContextModule();
ModalHelperContextModule form_tools = new ModalHelperContextModule();
AutoCompleteHelperModule autoHelper = new AutoCompleteHelperModule();

switch (name) {

Expand All @@ -52,7 +55,7 @@ public void handleLogic(MessageContextInteractionEvent context, SlashCommandInte

case "play" -> tools.sendPlayChallengeCommand(slashEvent, context, true);

case "profile" -> form_tools.sendLichessUserProfileInputForm(slashEvent);
case "profile" -> autoHelper.onSelectLichessUserNameHandleRequest(slashEvent, client);

case "profilecc" -> form_tools.sendChessComUserProfileInputForm(slashEvent);

Expand All @@ -75,3 +78,4 @@ public void handleLogic(MessageContextInteractionEvent context, SlashCommandInte

}


44 changes: 44 additions & 0 deletions src/main/java/Discord/HelperModules/AutoCompleteHelperModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package Discord.HelperModules;

import Lichess.UserProfile;
import chariot.Client;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.Command;
import java.util.List;

public class AutoCompleteHelperModule {

public AutoCompleteHelperModule(){

}


public void onLichessProfileAutoComplete(CommandAutoCompleteInteractionEvent event, Client client){

if(event.getName().equalsIgnoreCase("profile") && event.getFocusedOption().getName().equalsIgnoreCase("search-user")){
List<Command.Choice> options = client.users().autocompleteNames(event.getFocusedOption().getValue()).stream()
.filter(username -> username.startsWith(event.getFocusedOption().getValue()))
.map(username -> new Command.Choice(username, username))
.toList();
event.replyChoices(options).queue();
}

}


public void onSelectLichessUserNameHandleRequest(SlashCommandInteractionEvent event, Client client){

String userID = event.getOptionsByName("search-user").get(0).getAsString();
UserProfile userProfile = new UserProfile(client, userID);
event.deferReply(false).queue();
event.getHook().sendMessage(userProfile.getUserProfile()).setEphemeral(false).queue();


}





}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import Discord.MainHandler.AntiSpam;
import Lichess.Game;
import Lichess.UserGame;
import Lichess.UserProfile;
import chariot.Client;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
Expand All @@ -23,9 +22,6 @@ public ModalHelperContextModule() {

}




private void buildInputForm(SlashCommandInteractionEvent slashEvent, String inputid, String label, String placeholder, String modalid, String modaltitle) {
TextInput ptext = TextInput.create(inputid, label, TextInputStyle.SHORT)
.setPlaceholder(placeholder)
Expand Down Expand Up @@ -63,11 +59,7 @@ private void buildButtonInputForm(ButtonInteractionEvent buttonEvent, String inp
public void sendSelfUserInputForm(ButtonInteractionEvent buttonEvent){
buildButtonInputForm(buttonEvent,"self-user","Input Your Lichess Username", "Input Your Lichess Username", "modal-self-user", "Challenge Friend ");
}


public void sendLichessUserProfileInputForm(SlashCommandInteractionEvent slashCommandInteractionEvent) {
buildInputForm(slashCommandInteractionEvent, "profileuser", "Input Lichess Username", "Input Lichess Username", "modalpro", "View Lichess Profiles!");
}


public void sendChessComUserProfileInputForm(SlashCommandInteractionEvent slashCommandInteractionEvent) {
buildInputForm(slashCommandInteractionEvent, "profileusercc", "Input Chess.com Username", "Input Chess.com Username", "modalproc", "View Chess.com Profiles!");
Expand Down Expand Up @@ -116,14 +108,6 @@ public void handleGameInputOnFormSubmit(ModalInteractionEvent eventModal, Client
}
}


public void handleLichessProfileOnFormSubmit(ModalInteractionEvent eventModal, Client client) {
String userID = eventModal.getValue("profileuser").getAsString().trim();
UserProfile userProfile = new UserProfile(client, userID);
eventModal.deferReply(true).queue();
eventModal.getChannel().sendMessage(userProfile.getUserProfile()).queue();
}

public void handleChessComProfileOnFormSubmit(ModalInteractionEvent eventModal) {
String usercc = eventModal.getValue("profileusercc").getAsString().trim();
CCProfile ccProfile = new CCProfile(usercc);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Runner/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void main(String[] args) {
commands.addCommands(Commands.slash("service", "Read our Terms of Service"));
commands.addCommands(Commands.slash("arena", "See Swiss/Arena standings").addOption(OptionType.STRING, "arenaid", "Input Lichess arena link", true));
commands.addCommands(Commands.slash("watchmaster", "Watch GM Games in gif"));
commands.addCommands(Commands.slash("profile", "See Lichess Profile of given user"));
commands.addCommands(Commands.slash("profile", "See Lichess Profile of given user").addOptions(new OptionData(OptionType.STRING, "search-user", "Search Lichess username", true).setAutoComplete(true).setRequired(true)));
commands.addCommands(Commands.slash("streamers", "See current Live Streamers"));
commands.addCommands(Commands.slash("puzzle", "do random/daily puzzles").addOptions(new OptionData(OptionType.STRING, "pick-puzzle", "pick type of puzzles", true).addChoice("Lichess daily puzzle", "lip").addChoice("Chess.com daily puzzle", "cpp").addChoice("Chess.com random puzzle", "random")).addOptions(
new OptionData(OptionType.STRING, "pick-mode", "pick interaction mode", true)
Expand Down Expand Up @@ -87,7 +87,7 @@ public void onReady(ReadyEvent event) {
JDA jda = event.getJDA();
int guildCount = jda.getGuilds().size();

jda.getPresence().setActivity(Activity.watching("V13.5 Servers: " + guildCount));
jda.getPresence().setActivity(Activity.watching("V13.7 Servers: " + guildCount));
}


Expand Down

0 comments on commit 64178bd

Please sign in to comment.