瀏覽代碼

sentimental

tobby48 5 年之前
父節點
當前提交
93e66c1692

+ 6
- 0
pom.xml 查看文件

@@ -237,6 +237,12 @@
237 237
 		    <artifactId>JDA</artifactId>
238 238
 		    <version>LATEST</version>
239 239
 		</dependency>
240
+		<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
241
+		<dependency>
242
+			<groupId>org.apache.httpcomponents</groupId>
243
+			<artifactId>httpclient</artifactId>
244
+			<version>4.5.10</version>
245
+		</dependency>
240 246
 	    
241 247
 		<dependency>
242 248
 			<groupId>com.sparkjava</groupId>

+ 64
- 0
src/main/java/kr/co/swh/lecture/opensource/discord/project/SendJDA.java 查看文件

@@ -0,0 +1,64 @@
1
+package kr.co.swh.lecture.opensource.discord.project;
2
+
3
+import java.io.IOException;
4
+import java.util.ArrayList;
5
+import java.util.Arrays;
6
+import java.util.List;
7
+
8
+import javax.security.auth.login.LoginException;
9
+
10
+import org.apache.http.HttpResponse;
11
+import org.apache.http.client.methods.HttpGet;
12
+import org.apache.http.impl.client.CloseableHttpClient;
13
+import org.apache.http.impl.client.HttpClients;
14
+import org.apache.http.util.EntityUtils;
15
+
16
+import com.google.gson.Gson;
17
+
18
+import net.dv8tion.jda.api.AccountType;
19
+import net.dv8tion.jda.api.JDA;
20
+import net.dv8tion.jda.api.JDABuilder;
21
+import net.dv8tion.jda.api.OnlineStatus;
22
+
23
+public class SendJDA {
24
+
25
+	public static JDA jda;
26
+	public static void main(String[] args) {
27
+		// TODO Auto-generated method stub
28
+		List<SentimentalDic> dicList = initDatas();
29
+		JDABuilder jb = new JDABuilder(AccountType.BOT);
30
+		jb.setAutoReconnect(true);
31
+		jb.setStatus(OnlineStatus.DO_NOT_DISTURB);
32
+		jb.setToken("NjYzMjgzODYxMTUyNTk1OTkz.XhGR3g.GubjRZ-9njci3CUCoasIFj02mmU");
33
+		jb.addEventListeners(new TListener(dicList));
34
+		
35
+		try {
36
+			jda = jb.build();
37
+		} catch (LoginException e) {
38
+			// TODO Auto-generated catch block
39
+			e.printStackTrace();
40
+		}
41
+	}
42
+
43
+	public static List<SentimentalDic> initDatas() {
44
+		Gson gson = new Gson();
45
+		List<SentimentalDic> dicList = new ArrayList<SentimentalDic>();
46
+		CloseableHttpClient client = HttpClients.createDefault();
47
+		HttpResponse response = null;
48
+		String responseString = "";
49
+		HttpGet requestGet = new HttpGet("https://raw.githubusercontent.com/park1200656/KnuSentiLex/master/data/SentiWord_info.json");
50
+		try {
51
+			response = client.execute(requestGet);
52
+			responseString = EntityUtils.toString(response.getEntity());
53
+			SentimentalDic[] value = gson.fromJson(responseString, SentimentalDic[].class);
54
+			for(SentimentalDic dic : value) {
55
+				System.out.println(dic.getWord());
56
+			}
57
+			dicList.addAll(Arrays.asList(value));
58
+		} catch (IOException e2) {
59
+			// TODO Auto-generated catch block
60
+			e2.printStackTrace();
61
+		}
62
+		return dicList;
63
+	}
64
+}

+ 27
- 0
src/main/java/kr/co/swh/lecture/opensource/discord/project/SentimentalDic.java 查看文件

@@ -0,0 +1,27 @@
1
+package kr.co.swh.lecture.opensource.discord.project;
2
+
3
+public class SentimentalDic {
4
+	private String word;
5
+	private String word_root;
6
+	private String polarity;
7
+	public String getWord() {
8
+		return word;
9
+	}
10
+	public void setWord(String word) {
11
+		this.word = word;
12
+	}
13
+	public String getWord_root() {
14
+		return word_root;
15
+	}
16
+	public void setWord_root(String word_root) {
17
+		this.word_root = word_root;
18
+	}
19
+	public String getPolarity() {
20
+		return polarity;
21
+	}
22
+	public void setPolarity(String polarity) {
23
+		this.polarity = polarity;
24
+	}
25
+	
26
+	
27
+}

+ 38
- 0
src/main/java/kr/co/swh/lecture/opensource/discord/project/TListener.java 查看文件

@@ -0,0 +1,38 @@
1
+package kr.co.swh.lecture.opensource.discord.project;
2
+
3
+import java.util.List;
4
+
5
+import kr.co.swh.lecture.opensource.discord.TwitterContents;
6
+import net.dv8tion.jda.api.entities.Message;
7
+import net.dv8tion.jda.api.entities.TextChannel;
8
+import net.dv8tion.jda.api.entities.User;
9
+import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
10
+import net.dv8tion.jda.api.hooks.ListenerAdapter;
11
+import twitter4j.Status;
12
+
13
+public class TListener extends ListenerAdapter{
14
+	List<SentimentalDic> dicList;
15
+	
16
+	public TListener(List<SentimentalDic> dicList) {
17
+		this.dicList = dicList;
18
+	}
19
+	@Override
20
+	public void onMessageReceived(MessageReceivedEvent event) {
21
+		// TODO Auto-generated method stub
22
+		User user = event.getAuthor();
23
+		TextChannel tc = event.getTextChannel();
24
+		Message msg = event.getMessage();
25
+		if(user.isBot()) return;
26
+		if(msg.getContentRaw().charAt(0) == '!') {
27
+			String[] args = msg.getContentRaw().substring(1).split(" ");
28
+			if(args.length < 0) return;
29
+			if(args.length == 2 && args[0].equalsIgnoreCase("lol")) {
30
+				List<Status> status = new TwitterContents().getContents(args[1]);
31
+				for(Status s : status) {
32
+					tc.sendMessage(s.getText()).queue();
33
+				}
34
+			}
35
+		}
36
+	}
37
+
38
+}