如何基于React发送消息,然后根据用户消息发送另一条消息?使用JDA。

huangapple 未分类评论48阅读模式
英文:

How can I send message bassed on react and then send another bassed on user message? Using JDA

问题

我正在创建我的第一个Discord机器人。该机器人是为一个学生社团服务器而创建的。我想根据某些问题为用户分配角色/允许用户加入服务器。

目前,当新成员加入时,我会在欢迎频道中发送一条欢迎消息。我还会发送一个带有问题的嵌入式消息。如果他们用大拇指向上的表情回应,它会发送另一个嵌入式消息,询问他们的学生ID。如果他们用大拇指向下的表情回应,我会发送一个嵌入式消息,询问他们是否是学生,如果他们用大拇指向上的表情回应,我会继续询问他们的学生身份。

我如何获取他们的消息并检查它是否等于一个字符串?例如(用户输入学生ID:32984230875,然后检查是否等于字符串。)

我的当前反应监听方法:

public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
	
	Guild guild = event.getGuild();
	
	if(event.getMember().equals(member)) {
	
		if(event.getChannel().getId().equals("ChannelID") && event.getReactionEmote().getName().equals("👍")) {
			EmbedBuilder enterStudentID = new EmbedBuilder();
			enterStudentID.setTitle("Enter your Student ID:");
	        
			guild.getTextChannelById("ChannelID").sendMessage(enterStudentID.build()).queue();
				
		} else {
			EmbedBuilder isMemberStudent = new EmbedBuilder();
			isMemberStudent.setTitle("Are you a Student?");
	        
			guild.getTextChannelById("ChannelID").sendMessage(isMemberStudent.build()).queue(message -> {
	        	message.addReaction("👍").queue();
	        	message.addReaction("👎").queue();
	        });
			
			if(event.getChannel().getId().equals("ChannelID") && event.getReactionEmote().getName().equals("👍")) {
				EmbedBuilder enterStudentID = new EmbedBuilder();
				enterStudentID.setTitle("Enter your Student ID:");
		        
				guild.getTextChannelById("channelID").sendMessage(enterStudentID.build()).queue();
			}
			
		}
	}
}

附:第一个询问是否加入社团的嵌入式消息在成员加入方法中。这是第一个反应监听的内容。

提前感谢你的帮助。

英文:

I am creating my first Discord Bot. The bot is for a student society server. I am wanting to assign a role/allow a user join the server based on certain questions.

At the momment, when a new member joins, I send a message in the welcome channel with a welcome message. I also send an Embed with a question. I have it where if they react with thumbs up, it sends another embed asking for their student id. If they thumbs down I send an embed asking if they are student, if they thumb up react then I ask for their student.

How can I get their message and check it equals a String? e.g.(User enters student id: 32984230875 it is then check is equal to string.)

My current reaction listener method:

public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
	
	Guild guild = event.getGuild();
	
	if(event.getMember().equals(member)) {
	
		if(event.getChannel().getId().equals("ChannelID") && event.getReactionEmote().getName().equals("👍")) {
			EmbedBuilder enterStudentID = new EmbedBuilder();
			enterStudentID.setTitle("Enter your Student ID:");
	        
			guild.getTextChannelById("ChannelID").sendMessage(enterStudentID.build()).queue();
				
		} else {
			EmbedBuilder isMemberStudent = new EmbedBuilder();
			isMemberStudent.setTitle("Are you a Student?");
	        
			guild.getTextChannelById("ChannelID").sendMessage(isMemberStudent.build()).queue(message -> {
	        	message.addReaction("👍").queue();
	        	message.addReaction("👎").queue();
	        });
			
			if(event.getChannel().getId().equals("ChannelID") && event.getReactionEmote().getName().equals("👍")) {
				EmbedBuilder enterStudentID = new EmbedBuilder();
				enterStudentID.setTitle("Enter your Student ID:");
		        
				guild.getTextChannelById("channelID").sendMessage(enterStudentID.build()).queue();
			}
			
		}
	}
}

Ps. First embed asking if they society member, is on member join method. That's what the first react is listening to.

Thanks in advance.

答案1

得分: 0

每个 Discord 中的常规消息都是一个 String。你需要的学生 ID 也是一个 String,但可以转换为 long

为什么不用 int?
int:使用四个字节来存储范围从 -2,147,483,648 到 2,147,483,647 的值。
long:使用八个字节来存储范围从 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 的值。

所以从大小方面来看,它只能适应于 long

String myString = "123124";
long myLong;
try {
    myLong = Long.parseLong(myString);
    // 在这里你可以做更多的操作
} catch (NumberFormatException ex) {
    ex.printStackTrace();
    // 在这里你可以做其他处理异常的操作。
}

学生 ID:32984230875

如果你的学生 ID 有特定的公式,比如每个学生 ID 都以特定的数字开头,并且有固定的长度,那么你可以使用一个正则表达式来验证所提供的数据是否符合一组预定义的条件。

因此,如果 Stringlong 的转换成功,或者消息与正则表达式pattern匹配,那么该 ID 就是有效的,可以供你使用。

编辑:如果我完全误解了你的问题,另一件我能想到的事情是你需要监听一个 GuildMessageReceivedEvent,这是来自正在使用的频道并且来自与机器人交互的用户的消息。

英文:

Every regular message in Discord is a String. The student ID which you need is also a String, but can be converted to a long.

Why not int?
int: uses four bytes to store values from -2,147,483,648 to 2,147,483,647.
long: uses eight bytes to store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

So in terms of size, it can only fit into a long.

String myString = "123124";
long myLong;
try {
    myLong = Long.parseLong(myString);
    // You can do more stuff here  
} catch (NumberFormatException ex) {
    ex.printStackTrace();
    // You can do other stuff here to handle the exception.
}

> student id: 32984230875

If there's a specific formula for your student ID, let's say every student ID starts with a specific number, and has an exact length, then you can use a Regular expression which validates the provided data against a set of predefined criterion.

So if the String =>> long conversion is successful, or the message matches the regex pattern then the ID is valid for you to use.

EDIT: If I completely misunderstood your problem, then the other thing I can think of is you need to listen for a GuildMessageReceivedEvent, a message which is from the channel which is in use, and from the user who the bot is interacting with.

huangapple
  • 本文由 发表于 2020年4月9日 09:20:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61112496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定