Skip to content

Commit

Permalink
Release 1.4
Browse files Browse the repository at this point in the history
Add /lottery and /tool command. Mention the user at lobby channel when get role. Optimize some code. Complete some keys of pom.xml. Translate some JSON keys.
  • Loading branch information
AlexCai2019 committed Feb 15, 2023
1 parent 5672309 commit d510bcb
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 71 deletions.
1 change: 1 addition & 0 deletions lang/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"help.name.cmd": "格式: `/cmd [<命令>]`",
"help.name.faq": "格式: `/faq [<问题>]`",
"help.name.dtp": "格式: `/dtp [<数据包功能>]`",
"help.name.tool": "格式: `/tool (uuid_string|uuid_array|pack_mcmeta) <参数...>`",
"help.name.lang": "格式: `/lang [<代码>]`",
"cmd.begin": "`命令: ",
"cmd.end": "`",
Expand Down
2 changes: 2 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"cmd",
"faq",
"dtp",
"tool",
"lang"
],
"help.name.cmd": "Syntax: `/cmd [<command>]`",
"help.name.faq": "Syntax: `/faq [<question>]`",
"help.name.dtp": "Syntax: `/dtp [<datapack feature>]`",
"help.name.tool": "Syntax: `/tool (uuid_string|uuid_array|pack_mcmeta) <arguments...>`",
"help.name.lang": "Syntax: `/lang [<code>]`",
"cmd.begin": "`Commands: ",
"cmd.end": "`",
Expand Down
1 change: 1 addition & 0 deletions lang/hk.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"help.name.cmd": "格式: `/cmd [<指令>]`",
"help.name.faq": "格式: `/faq [<問題>]`",
"help.name.dtp": "格式: `/dtp [<資料包功能>]`",
"help.name.tool": "格式: `/tool (uuid_string|uuid_array|pack_mcmeta) <引數...>`",
"help.name.lang": "格式: `/lang [<代碼>]`",
"cmd.begin": "`指令: ",
"cmd.end": "`",
Expand Down
1 change: 1 addition & 0 deletions lang/ta.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"help.name.cmd": "格式: `/cmd [<指令>]`",
"help.name.faq": "格式: `/faq [<問題>]`",
"help.name.dtp": "格式: `/dtp [<資料包功能>]`",
"help.name.tool": "格式: `/tool (uuid_string|uuid_array|pack_mcmeta) <引數...>`",
"help.name.lang": "格式: `/lang [<代碼>]`",
"cmd.begin": "`指令: ",
"cmd.end": "`",
Expand Down
1 change: 1 addition & 0 deletions lang/tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"help.name.cmd": "格式: `/cmd [<指令>]`",
"help.name.faq": "格式: `/faq [<問題>]`",
"help.name.dtp": "格式: `/dtp [<資料包功能>]`",
"help.name.tool": "格式: `/tool (uuid_string|uuid_array|pack_mcmeta) <引數...>`",
"help.name.lang": "格式: `/lang [<代碼>]`",
"cmd.begin": "指令: `",
"cmd.end": "`",
Expand Down
23 changes: 21 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@

<groupId>org.cartoland</groupId>
<artifactId>Cartoland</artifactId>
<version>1.0</version>
<version>1.4</version>

<name>Cartoland Bot</name>
<description>A Discord bot that belongs to a server: Cartoland</description>
<url>https://github.com/AlexCai2019/Cartoland</url>

<licenses>
<license>
<name>MIT License</name>
<url>https://github.com/AlexCai2019/Cartoland/blob/master/LICENSE.md</url>
<distribution>manual</distribution>
</license>
</licenses>

<developers>
<developer>
<name>Alex Cai</name>
<email>[email protected]</email>
</developer>
</developers>

<dependencies>
<!-- https://mvnrepository.com/artifact/net.dv8tion/JDA -->
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-beta.2</version>
<version>5.0.0-beta.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/cartoland/Cartoland.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.MemberCachePolicy;

import static cartoland.utilities.IDAndEntities.jda;

Expand All @@ -37,7 +38,8 @@ public static void main(String[] args) throws InterruptedException
new PrivateMessage(), //當有人傳私訊給機器人
new CommandUsage(), //當有人使用指令
new GetRole()) //有人獲得會員身分組
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
.enableIntents(GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_MEMBERS)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.setActivity(Activity.playing("Use /help to check more information")) //正在玩
.build();

Expand All @@ -48,6 +50,7 @@ public static void main(String[] args) throws InterruptedException
.addChoice("cmd", "cmd")
.addChoice("faq", "faq")
.addChoice("dtp", "dtp")
.addChoice("tool", "tool")
.addChoice("lang", "lang")),
Commands.slash("cmd", "Get help of Minecraft commands")
.addOption(OptionType.STRING, "cmd_name", "The name of a Minecraft command", false),
Expand All @@ -73,9 +76,9 @@ public static void main(String[] args) throws InterruptedException
.addOption(OptionType.INTEGER, "2", "The [2] of the array", true)
.addOption(OptionType.INTEGER, "3", "The [3] of the array", true),
new SubcommandData("pack_mcmeta", "Generate a pack.mcmeta")
.addOptions(new OptionData(OptionType.STRING, "pack_type", "Datapack or Resourcepack", true)
.addChoice("Datapack", "d")
.addChoice("Resourcepack", "r"))),
.addOptions(new OptionData(OptionType.STRING, "pack_type", "Data pack or Resource pack", true)
.addChoice("Data pack", "d")
.addChoice("Resource pack", "r"))),
Commands.slash("lang", "Change language or check current languages")
.addOption(OptionType.STRING, "lang_name", "The language that user want to change", false),
Commands.slash("language", "Change language or check current languages")
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/cartoland/events/ChannelMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

import java.util.regex.Pattern;

/**
* {@code ChannelMessage} is a listener that triggers when a user types anything in any channel that the bot can
* access. This class was registered in {@link cartoland.Cartoland#main}, with the build of JDA.
Expand Down Expand Up @@ -58,7 +60,10 @@ public class ChannelMessage extends ListenerAdapter
"☆めぐみんは最高だ!☆",
"☆めぐみん俺の嫁!☆"
};
private final MentionType[] botType = { MentionType.USER, MentionType.ROLE };

private static final MentionType[] BOT_TYPE = { MentionType.USER, MentionType.ROLE };

private static final Pattern MEGUMIN = Pattern.compile("(?i).*megumin.*");

/**
* The method that inherited from {@link ListenerAdapter}, triggers when receive a message from any
Expand All @@ -73,7 +78,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event)
if (!event.isFromType(ChannelType.TEXT)) //不是文字頻道
return;
User author = event.getAuthor();
if (author.isBot()) //傳訊息的是機器人
if (author.isBot() || author.isSystem()) //傳訊息的是機器人或系統
return; //不用執行

long userID = author.getIdLong();
Expand All @@ -85,12 +90,12 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event)
long categoryID = category.getIdLong();
TextChannel channel = (TextChannel) message.getChannel();

if (message.getMentions().isMentioned(IDAndEntities.botItself, botType)) //有人tag機器人
message.reply(userID == IDAndEntities.AC_ID ? replyACMention[IDAndEntities.random.nextInt(replyACMention.length)] : replyMention[IDAndEntities.random.nextInt(replyMention.length)])
if (message.getMentions().isMentioned(IDAndEntities.botItself, BOT_TYPE)) //有人tag機器人
message.reply(userID == IDAndEntities.AC_ID ? replyACMention[IDAndEntities.RANDOM.nextInt(replyACMention.length)] : replyMention[IDAndEntities.RANDOM.nextInt(replyMention.length)])
.mentionRepliedUser(false).queue();

if (rawMessage.matches("(?i).*megumin.*") || rawMessage.contains("惠惠") || rawMessage.contains("めぐみん"))
channel.sendMessage(megumin[IDAndEntities.random.nextInt(megumin.length)]).queue();
if (MEGUMIN.matcher(rawMessage).matches() || rawMessage.contains("惠惠") || rawMessage.contains("めぐみん"))
channel.sendMessage(megumin[IDAndEntities.RANDOM.nextInt(megumin.length)]).queue();

if (rawMessage.contains("早安"))
channel.sendMessage("早上好中國 現在我有Bing Chilling").queue();
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/cartoland/events/GetRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public class GetRole extends ListenerAdapter
public void onGuildMemberRoleAdd(@NotNull GuildMemberRoleAddEvent event)
{
if (event.getRoles().contains(IDAndEntities.memberRole))
IDAndEntities.lobbyChannel.sendMessage("歡迎 " + event.getUser().getAsTag() + " 加入 " + IDAndEntities.cartolandServer.getName()).queue();
{
String mentionUser = event.getUser().getAsMention();
String serverName = IDAndEntities.cartolandServer.getName();

IDAndEntities.lobbyChannel.sendMessage("歡迎 " + mentionUser + " 加入 " + serverName +
"\n" + mentionUser + ", welcome to " + serverName).queue();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/cartoland/events/PrivateMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event)
if (!event.isFromType(ChannelType.PRIVATE)) //不是私訊
return;
User author = event.getAuthor();
if (author.isBot()) //是機器人
if (author.isBot() || author.isSystem()) //是機器人或系統
return;

Message message = event.getMessage();
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/cartoland/events/commands/CommandUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.util.HashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* {@code CommandUsage} is a listener that triggers when a user uses slash command. This class was registered in
Expand Down Expand Up @@ -42,7 +41,6 @@ public class CommandUsage extends ListenerAdapter
*/
String commandName;
String subCommandName;
Stream<OptionMapping> optionsStream;

/**
* 356 images about Megumin.
Expand All @@ -61,33 +59,33 @@ public CommandUsage()
commands.put("invite", event -> event.reply("https://discord.gg/UMYxwHyRNE").queue());

//help
commands.put("help", event -> event.reply(minecraftCommandRelated("help")).queue());
commands.put("help", event -> event.reply(minecraftCommandRelated("help", event)).queue());

//cmd
alias = event -> event.reply(minecraftCommandRelated("cmd")).queue();
alias = event -> event.reply(minecraftCommandRelated("cmd", event)).queue();
commands.put("cmd", alias);
commands.put("mcc", alias);
commands.put("command", alias);

//faq
alias = event -> event.reply(minecraftCommandRelated("faq")).queue();
alias = event -> event.reply(minecraftCommandRelated("faq", event)).queue();
commands.put("faq", alias);
commands.put("question", alias);

//dtp
alias = event -> event.reply(minecraftCommandRelated("dtp")).queue();
alias = event -> event.reply(minecraftCommandRelated("dtp", event)).queue();
commands.put("dtp", alias);
commands.put("datapack", alias);

//lang
alias = event -> event.reply(minecraftCommandRelated("lang")).queue();
alias = event -> event.reply(minecraftCommandRelated("lang", event)).queue();
commands.put("lang", alias);
commands.put("language", alias);

commands.put("megumin", event ->
{
TA author = twitterAuthors[IDAndEntities.random.nextInt(twitterAuthors.length)];
long artwork = author.artworks()[IDAndEntities.random.nextInt(author.artworks().length)];
TA author = twitterAuthors[IDAndEntities.RANDOM.nextInt(twitterAuthors.length)];
long artwork = author.artworks()[IDAndEntities.RANDOM.nextInt(author.artworks().length)];
event.reply("https://twitter.com/" + author.name() + "/status/" + artwork).queue();
FileHandle.log(userName + "(" + userID + ") used /megumin.");
}); //隨機一張惠惠
Expand Down Expand Up @@ -127,11 +125,12 @@ public CommandUsage()
commands.put("lottery", new LotteryCommand(this));

//tool
commands.put("tool", new ToolCommand(this));
commands.put("tool", new ToolCommand());
}

/**
* The method that inherited from {@link ListenerAdapter}, triggers when a user uses slash command.
*
* @param event The event that carries information of the user and the command.
*/
@Override
Expand All @@ -142,19 +141,18 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
userName = user.getName();
commandName = event.getName();
subCommandName = event.getSubcommandName();
optionsStream = event.getOptions().stream();
FileHandle.log(userName + "(" + userID + ") used /" + commandName +
(subCommandName != null ? " " + subCommandName + " " : " ") +
optionsStream.map(String::valueOf).collect(Collectors.joining(" ", " ", "")));
event.getOptions().stream().map(OptionMapping::getName).collect(Collectors.joining(" ")));
commands.get(commandName).commandProcess(event);
}

private String minecraftCommandRelated(String jsonKey)
private String minecraftCommandRelated(String jsonKey, SlashCommandInteractionEvent event)
{
OptionMapping argument = optionsStream.filter(optionMapping -> optionMapping.getName().equals(jsonKey + "_name")).findAny().orElse(null); //獲得參數
String argument = event.getOption(jsonKey + "_name", OptionMapping::getAsString); //獲得參數
if (argument == null) //沒有參數
return JsonHandle.command(userID, jsonKey);
return JsonHandle.command(userID, jsonKey, argument.getAsString());
return JsonHandle.command(userID, jsonKey, argument);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/cartoland/events/commands/LotteryCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ public class LotteryCommand implements ICommand
public void commandProcess(SlashCommandInteractionEvent event)
{
long nowHave = JsonHandle.getCommandBlocks(commandCore.userID);
OptionMapping argument = commandCore.optionsStream.filter(optionMapping -> optionMapping.getName().equals("bet")).findAny().orElse(null);
String betString = event.getOption("bet", OptionMapping::getAsString);

if (argument == null) //不帶參數
if (betString == null) //不帶參數
{
event.reply(JsonHandle.getJsonKey(commandCore.userID, "lottery.query").formatted(nowHave)).queue();
return;
}

String betString = argument.getAsString();
long bet;

if (betString.matches("\\d+")) //賭數字
Expand Down Expand Up @@ -62,7 +61,7 @@ else if (betString.matches("\\d+%")) //賭%數

long afterBet;
String result;
if (IDAndEntities.random.nextBoolean()) //賭贏
if (IDAndEntities.RANDOM.nextBoolean()) //賭贏
{
afterBet = nowHave + bet;
if (afterBet < 0)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cartoland/events/commands/OneATwoBCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void commandProcess(@NotNull SlashCommandInteractionEvent event)
default -> argument + " = " + ab / 10 + " A " + ab % 10 + " B";
};

if (ab / 10 == OneATwoBGame.ANSWER_LENGTH)//沒有猜出ANSWER_LENGTH個A 遊戲繼續
if (ab / 10 != OneATwoBGame.ANSWER_LENGTH)//沒有猜出ANSWER_LENGTH個A 遊戲繼續
{
event.reply(shouldReply).queue();
return;
Expand All @@ -73,8 +73,8 @@ public void commandProcess(@NotNull SlashCommandInteractionEvent event)
//猜出ANSWER_LENGTH個A 遊戲結束
long second = oneATwoB.getTimePassed();
event.reply(shouldReply + "\nGame Over, the answer is **" + argument + "**.\n" +
"Used Time: " + second / 60 + " minutes " + second % 60 + " seconds.\n" +
"Guesses: " + oneATwoB.getGuesses() + " times.").queue();
"Used Time: " + second / 60 + " minutes " + second % 60 + " seconds\n" +
"Guesses: " + oneATwoB.getGuesses() + " times").queue();
commandCore.games.remove(commandCore.userID);
}
}
Loading

0 comments on commit d510bcb

Please sign in to comment.