mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2024-11-25 04:27:41 +08:00
添加字符串到命令数组解析,添加 /user 命令获取用户信息
This commit is contained in:
parent
cef31aa1fe
commit
ed5d3e00bc
@ -6,7 +6,7 @@ plugins {
|
||||
}
|
||||
|
||||
group 'cc.sukazyo'
|
||||
version '0.3.1'
|
||||
version '0.3.2'
|
||||
project.ext.archiveBaseName = 'Coeur_Morny_Cono'
|
||||
project.ext.artifactId = 'morny-coeur'
|
||||
mainClassName = 'cc.sukazyo.cono.morny.MornyCoeur'
|
||||
|
@ -4,6 +4,8 @@ import cc.sukazyo.cono.morny.MornyCoeur;
|
||||
import cc.sukazyo.cono.morny.MornySystem;
|
||||
import cc.sukazyo.cono.morny.MornyTrusted;
|
||||
import cc.sukazyo.cono.morny.bot.api.EventListener;
|
||||
import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId;
|
||||
import cc.sukazyo.cono.morny.util.StringUtils;
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
import com.pengrad.telegrambot.model.request.ParseMode;
|
||||
import com.pengrad.telegrambot.request.SendMessage;
|
||||
@ -23,7 +25,13 @@ public class OnCommandExecute extends EventListener {
|
||||
if (event.message().text() == null) {
|
||||
return false;
|
||||
}
|
||||
switch (event.message().text()) {
|
||||
String[] command = StringUtils.formatCommand(event.message().text());
|
||||
if (command.length == 0) return false;
|
||||
switch (command[0]) {
|
||||
case "/user":
|
||||
case "/user@" + MornyCoeur.USERNAME:
|
||||
GetUsernameAndId.exec(command, event);
|
||||
break;
|
||||
case "/o":
|
||||
case "/o@" + MornyCoeur.USERNAME:
|
||||
onCommandOnExec(event);
|
||||
@ -94,7 +102,7 @@ public class OnCommandExecute extends EventListener {
|
||||
MornySystem.VERSION,
|
||||
MornySystem.getJarMd5()
|
||||
)
|
||||
).parseMode(ParseMode.HTML));
|
||||
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,100 @@
|
||||
package cc.sukazyo.cono.morny.bot.event.on_commands;
|
||||
|
||||
import cc.sukazyo.cono.morny.MornyCoeur;
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
import com.pengrad.telegrambot.model.User;
|
||||
import com.pengrad.telegrambot.model.request.ParseMode;
|
||||
import com.pengrad.telegrambot.request.GetChatMember;
|
||||
import com.pengrad.telegrambot.request.SendMessage;
|
||||
import com.pengrad.telegrambot.response.GetChatMemberResponse;
|
||||
|
||||
public class GetUsernameAndId {
|
||||
|
||||
public static void exec (String[] command, Update event) {
|
||||
|
||||
if (command.length > 2) { MornyCoeur.getAccount().execute(new SendMessage(
|
||||
event.message().chat().id(),
|
||||
"[Unavailable] Too much arguments."
|
||||
).replyToMessageId(event.message().messageId())); return; }
|
||||
|
||||
long userId = 0;
|
||||
|
||||
if ( event.message().replyToMessage()!= null) {
|
||||
userId = event.message().replyToMessage().from().id();
|
||||
}
|
||||
if (command.length > 1) {
|
||||
try {
|
||||
userId = Long.parseLong(command[1]);
|
||||
} catch (NumberFormatException e) {
|
||||
MornyCoeur.getAccount().execute(new SendMessage(
|
||||
event.message().chat().id(),
|
||||
"[Unavailable] " + e.getMessage()
|
||||
).replyToMessageId(event.message().messageId()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (userId == 0) {
|
||||
MornyCoeur.getAccount().execute(new SendMessage(
|
||||
event.message().chat().id(),
|
||||
"[Unavailable] no userid given."
|
||||
).replyToMessageId(event.message().messageId()));
|
||||
return;
|
||||
}
|
||||
|
||||
GetChatMemberResponse response = MornyCoeur.getAccount().execute(
|
||||
new GetChatMember(event.message().chat().id(), userId)
|
||||
);
|
||||
|
||||
if (response.chatMember() == null) {
|
||||
MornyCoeur.getAccount().execute(new SendMessage(
|
||||
event.message().chat().id(),
|
||||
"[Unavailable] user not found."
|
||||
).replyToMessageId(event.message().messageId()));
|
||||
return;
|
||||
}
|
||||
|
||||
User user = response.chatMember().user();
|
||||
|
||||
StringBuilder userInformation = new StringBuilder();
|
||||
userInformation.append(String.format(
|
||||
"userid :\n" +
|
||||
"- <code>%d</code>\n" +
|
||||
"username :\n" +
|
||||
"- <code>%s</code>",
|
||||
userId, user.username()
|
||||
));
|
||||
if (user.firstName() == null) {
|
||||
userInformation.append("\nfirstname : <u>null</u>");
|
||||
} else {
|
||||
userInformation.append(String.format(
|
||||
"\nfirstname :\n" +
|
||||
"- <code>%s</code>",
|
||||
user.firstName()
|
||||
));
|
||||
}
|
||||
if (user.lastName() == null) {
|
||||
userInformation.append("\nlastname : <u>null</u>");
|
||||
} else {
|
||||
userInformation.append(String.format(
|
||||
"\nlastname :\n" +
|
||||
"- <code>%s</code>",
|
||||
user.lastName()
|
||||
));
|
||||
}
|
||||
if (user.languageCode() != null) {
|
||||
userInformation.append(String.format(
|
||||
"\nlanguage-code :\n" +
|
||||
"- <code>%s</code>",
|
||||
user.languageCode()
|
||||
));
|
||||
}
|
||||
|
||||
MornyCoeur.getAccount().execute(new SendMessage(
|
||||
event.message().chat().id(),
|
||||
userInformation.toString()
|
||||
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package cc.sukazyo.cono.morny.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class StringUtils {
|
||||
@ -10,4 +11,42 @@ public class StringUtils {
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
public static String[] formatCommand (String com) {
|
||||
|
||||
ArrayList<String> arr = new ArrayList<>();
|
||||
|
||||
StringBuilder tmp = new StringBuilder();
|
||||
char[] coma = com.toCharArray();
|
||||
for (int i = 0; i < coma.length; i++) {
|
||||
if (coma[i] == ' ') {
|
||||
if (!tmp.toString().equals("")) { arr.add(tmp.toString()); }
|
||||
tmp.setLength(0);
|
||||
} else if (coma[i] == '"') {
|
||||
while (true) {
|
||||
i++;
|
||||
if (coma[i] == '"') {
|
||||
break;
|
||||
} else if (coma[i] == '\\' && (coma[i+1] == '"' || coma[i+1] == '\\')) {
|
||||
i++;
|
||||
tmp.append(coma[i]);
|
||||
} else {
|
||||
tmp.append(coma[i]);
|
||||
}
|
||||
}
|
||||
} else if (coma[i] == '\\' && (coma[i+1] == ' ' || coma[i+1] == '"' || coma[i+1] == '\\')) {
|
||||
i++;
|
||||
tmp.append(coma[i]);
|
||||
} else {
|
||||
tmp.append(coma[i]);
|
||||
}
|
||||
}
|
||||
if (!tmp.toString().equals("")) { arr.add(tmp.toString()); }
|
||||
tmp.setLength(0);
|
||||
|
||||
String[] out = new String[arr.size()];
|
||||
arr.toArray(out);
|
||||
return out;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user