tobby48 4 years ago
parent
commit
726649a8be

+ 2
- 2
src/main/java/kr/co/swh/lecture/opensource/project/discode/MakeSentimentalDictionary.java View File

@@ -20,8 +20,8 @@ public class MakeSentimentalDictionary {
20 20
 		// TODO Auto-generated method stub
21 21
 		Connection connection = null;
22 22
 		try {
23
-			Class.forName("com.mysql.jdbc.Driver");
24
-			connection = DriverManager.getConnection("jdbc:mysql://dev-swh.ga:3306/market","root","swhacademy!");
23
+			Class.forName("com.mysql.cj.jdbc.Driver");
24
+			connection = DriverManager.getConnection("jdbc:mysql://dev-swh.gq:3306/market?serverTimezone=UTC","root","swhacademy!");
25 25
 		} catch (SQLException e1) {
26 26
 			// TODO Auto-generated catch block
27 27
 			e1.printStackTrace();

+ 34
- 0
src/main/java/kr/co/swh/lecture/opensource/project/discode/music/MusicBot.java View File

@@ -0,0 +1,34 @@
1
+package kr.co.swh.lecture.opensource.project.discode.music;
2
+
3
+import javax.security.auth.login.LoginException;
4
+
5
+import kr.co.swh.lecture.opensource.project.discode.music.gui.GUI;
6
+import net.dv8tion.jda.api.JDA;
7
+import net.dv8tion.jda.api.JDABuilder;
8
+
9
+
10
+public class MusicBot {
11
+	
12
+	public void play() throws LoginException, InterruptedException {
13
+		// Note: It is important to register your ReadyListener before building
14
+        JDA jda = JDABuilder.createDefault("NjYzMjgzODYxMTUyNTk1OTkz.XhGROw.Ps3JJIKlEXoE1eah_5OKHwViThY")
15
+            .addEventListeners(new MusicListener())
16
+            .build();
17
+
18
+        // optionally block until JDA is ready
19
+        jda.awaitReady();
20
+		GUI gui = new GUI(this);
21
+//        this.setGUI(gui);
22
+        gui.init();	
23
+	}
24
+	public static void main(String[] args) throws LoginException, InterruptedException {
25
+		// TODO Auto-generated method stub
26
+		MusicBot bot = new MusicBot();
27
+		bot.play();
28
+	}
29
+	
30
+	 public void shutdown()
31
+	 {
32
+		 System.exit(0);
33
+	 }
34
+}

+ 112
- 0
src/main/java/kr/co/swh/lecture/opensource/project/discode/music/MusicListener.java View File

@@ -0,0 +1,112 @@
1
+package kr.co.swh.lecture.opensource.project.discode.music;
2
+
3
+import java.awt.Color;
4
+
5
+import net.dv8tion.jda.api.EmbedBuilder;
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
+
12
+public class MusicListener extends ListenerAdapter{
13
+	
14
+	@Override
15
+	public void onMessageReceived(MessageReceivedEvent event) {
16
+		// TODO Auto-generated method stub
17
+		User user = event.getAuthor();
18
+		TextChannel tc = event.getTextChannel();
19
+		Message msg = event.getMessage();
20
+		if(user.isBot()) return;
21
+		
22
+		
23
+		String message = msg.getContentRaw();
24
+		System.out.println(message.substring(0, 3));
25
+		
26
+		String m="";
27
+		if(message.substring(0, 3).equals("!! ")) {
28
+			m = message.substring(3);
29
+		}
30
+		
31
+//		// 형태소 분석
32
+//		for (Eojeol eojeol : Analyzer.parseEojeolJava(msg.getContentRaw())) {
33
+//			for (LNode node : eojeol.nodesJava()) {
34
+//				Morpheme m = node.morpheme();
35
+//				tc.sendMessage(m.surface() + " / 품사:" + m.feature().head()).queue();
36
+//			}
37
+//		}
38
+		
39
+//		EmbedBuilder eb = new EmbedBuilder();
40
+//		eb.setTitle("It's GIVEAWAY-TIME!");
41
+//		eb.setDescription("Prize: " + "\n Winners: " + 1);
42
+//		eb.setColor(Color.BLUE);
43
+//		eb.setFooter("바닥");
44
+		
45
+		// Create the EmbedBuilder instance
46
+		EmbedBuilder eb = new EmbedBuilder();
47
+
48
+		/*
49
+		    Set the title:
50
+		    1. Arg: title as string
51
+		    2. Arg: URL as string or could also be null
52
+		 */
53
+		eb.setTitle(m, null);
54
+
55
+		/*
56
+		    Set the color
57
+		 */
58
+		eb.setColor(Color.red);
59
+		eb.setColor(new Color(0xF40C0C));
60
+		eb.setColor(new Color(255, 0, 54));
61
+
62
+		/*
63
+		    Set the text of the Embed:
64
+		    Arg: text as string
65
+		 */
66
+		eb.setDescription("Text");
67
+
68
+		/*
69
+		    Add fields to embed:
70
+		    1. Arg: title as string
71
+		    2. Arg: text as string
72
+		    3. Arg: inline mode true / false
73
+		 */
74
+		eb.addField("Title of field", "test of field", false);
75
+
76
+		/*
77
+		    Add spacer like field
78
+		    Arg: inline mode true / false
79
+		 */
80
+		eb.addBlankField(false);
81
+
82
+		/*
83
+		    Add embed author:
84
+		    1. Arg: name as string
85
+		    2. Arg: url as string (can be null)
86
+		    3. Arg: icon url as string (can be null)
87
+		 */
88
+		eb.setAuthor("name", null, "https://github.com/zekroTJA/DiscordBot/blob/master/.websrc/zekroBot_Logo_-_round_small.png");
89
+
90
+		/*
91
+		    Set footer:
92
+		    1. Arg: text as string
93
+		    2. icon url as string (can be null)
94
+		 */
95
+		eb.setFooter("Text", "https://github.com/zekroTJA/DiscordBot/blob/master/.websrc/zekroBot_Logo_-_round_small.png");
96
+
97
+		/*
98
+		    Set image:
99
+		    Arg: image url as string
100
+		 */
101
+		eb.setImage("https://github.com/zekroTJA/DiscordBot/blob/master/.websrc/logo%20-%20title.png");
102
+
103
+		/*
104
+		    Set thumbnail image:
105
+		    Arg: image url as string
106
+		 */
107
+		eb.setThumbnail("https://github.com/zekroTJA/DiscordBot/blob/master/.websrc/logo%20-%20title.png");
108
+		event.getTextChannel().sendMessage(eb.build()).queue();
109
+		
110
+	}
111
+
112
+}

+ 52
- 0
src/main/java/kr/co/swh/lecture/opensource/project/discode/music/gui/ConsolePanel.java View File

@@ -0,0 +1,52 @@
1
+package kr.co.swh.lecture.opensource.project.discode.music.gui; 
2
+
3
+/**
4
+ * <pre>
5
+ * kr.co.swh.lecture.opensource.project.discode.music.gui 
6
+ * ConsolePanel.java
7
+ *
8
+ * 설명 :
9
+ * </pre>
10
+ * 
11
+ * @since : 2021. 5. 30.
12
+ * @author : tobby48
13
+ * @version : v1.0
14
+ */
15
+import java.awt.Dimension;
16
+import java.awt.GridLayout;
17
+import java.io.PrintStream;
18
+
19
+import javax.swing.JPanel;
20
+import javax.swing.JScrollPane;
21
+import javax.swing.JTextArea;
22
+
23
+/**
24
+ *
25
+ * @author John Grosh <john.a.grosh@gmail.com>
26
+ */
27
+public class ConsolePanel extends JPanel {
28
+    
29
+    /**
30
+	 * 
31
+	 */
32
+	private static final long serialVersionUID = 1L;
33
+
34
+	public ConsolePanel()
35
+    {
36
+        super();
37
+        JTextArea text = new JTextArea();
38
+        text.setLineWrap(true);
39
+        text.setWrapStyleWord(true);
40
+        text.setEditable(false);
41
+        PrintStream con=new PrintStream(new TextAreaOutputStream(text));
42
+        System.setOut(con);
43
+        System.setErr(con);
44
+        
45
+        JScrollPane pane = new JScrollPane();
46
+        pane.setViewportView(text);
47
+        
48
+        super.setLayout(new GridLayout(1,1));
49
+        super.add(pane);
50
+        super.setPreferredSize(new Dimension(400,300));
51
+    }
52
+}

+ 72
- 0
src/main/java/kr/co/swh/lecture/opensource/project/discode/music/gui/GUI.java View File

@@ -0,0 +1,72 @@
1
+package kr.co.swh.lecture.opensource.project.discode.music.gui; 
2
+
3
+/**
4
+ * <pre>
5
+ * kr.co.swh.lecture.opensource.project.discode.music.gui 
6
+ * GUI.java
7
+ *
8
+ * 설명 :
9
+ * </pre>
10
+ * 
11
+ * @since : 2021. 5. 30.
12
+ * @author : tobby48
13
+ * @version : v1.0
14
+ */
15
+import java.awt.event.WindowEvent;
16
+import java.awt.event.WindowListener;
17
+
18
+import javax.swing.JFrame;
19
+import javax.swing.JTabbedPane;
20
+import javax.swing.WindowConstants;
21
+
22
+import kr.co.swh.lecture.opensource.project.discode.music.MusicBot;
23
+
24
+
25
+/**
26
+ *
27
+ * @author John Grosh <john.a.grosh@gmail.com>
28
+ */
29
+public class GUI extends JFrame 
30
+{
31
+    private final ConsolePanel console;
32
+    private final MusicBot bot;
33
+    
34
+    public GUI(MusicBot bot) 
35
+    {
36
+        super();
37
+        this.bot = bot;
38
+        console = new ConsolePanel();
39
+    }
40
+    
41
+    public void init()
42
+    {
43
+        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
44
+        setTitle("JMusicBot");
45
+        JTabbedPane tabs = new JTabbedPane();
46
+        tabs.add("Console", console);
47
+        getContentPane().add(tabs);
48
+        pack();
49
+        setLocationRelativeTo(null);
50
+        setVisible(true);
51
+        addWindowListener(new WindowListener() 
52
+        {
53
+            @Override public void windowOpened(WindowEvent e) { /* unused */ }
54
+            @Override public void windowClosing(WindowEvent e) 
55
+            {
56
+                try
57
+                {
58
+                    bot.shutdown();
59
+                }
60
+                catch(Exception ex)
61
+                {
62
+                    System.exit(0);
63
+                }
64
+            }
65
+            @Override public void windowClosed(WindowEvent e) { /* unused */ }
66
+            @Override public void windowIconified(WindowEvent e) { /* unused */ }
67
+            @Override public void windowDeiconified(WindowEvent e) { /* unused */ }
68
+            @Override public void windowActivated(WindowEvent e) { /* unused */ }
69
+            @Override public void windowDeactivated(WindowEvent e) { /* unused */ }
70
+        });
71
+    }
72
+}

+ 157
- 0
src/main/java/kr/co/swh/lecture/opensource/project/discode/music/gui/TextAreaOutputStream.java View File

@@ -0,0 +1,157 @@
1
+package kr.co.swh.lecture.opensource.project.discode.music.gui; 
2
+
3
+/**
4
+ * <pre>
5
+ * kr.co.swh.lecture.opensource.project.discode.music.gui 
6
+ * TextAreaOutputStream.java
7
+ *
8
+ * 설명 :
9
+ * </pre>
10
+ * 
11
+ * @since : 2021. 5. 30.
12
+ * @author : tobby48
13
+ * @version : v1.0
14
+ */
15
+import java.awt.EventQueue;
16
+import java.io.OutputStream;
17
+import java.io.UnsupportedEncodingException;
18
+import java.util.ArrayList;
19
+import java.util.LinkedList;
20
+import java.util.List;
21
+
22
+import javax.swing.JTextArea;
23
+/**
24
+ *
25
+ * @author Lawrence Dol
26
+ */
27
+public class TextAreaOutputStream extends OutputStream {
28
+
29
+// *************************************************************************************************
30
+// INSTANCE MEMBERS
31
+// *************************************************************************************************
32
+
33
+private byte[]                          oneByte;                                                    // array for write(int val);
34
+private Appender                        appender;                                                   // most recent action
35
+
36
+public TextAreaOutputStream(JTextArea txtara) {
37
+    this(txtara,1000);
38
+    }
39
+
40
+public TextAreaOutputStream(JTextArea txtara, int maxlin) {
41
+    if(maxlin<1) { throw new IllegalArgumentException("TextAreaOutputStream maximum lines must be positive (value="+maxlin+")"); }
42
+    oneByte=new byte[1];
43
+    appender=new Appender(txtara,maxlin);
44
+    }
45
+
46
+/** Clear the current console text area. */
47
+public synchronized void clear() {
48
+    if(appender!=null) { appender.clear(); }
49
+    }
50
+
51
+@Override
52
+public synchronized void close() {
53
+    appender=null;
54
+    }
55
+
56
+@Override
57
+public synchronized void flush() {
58
+    /* empty */
59
+    }
60
+
61
+@Override
62
+public synchronized void write(int val) {
63
+    oneByte[0]=(byte)val;
64
+    write(oneByte,0,1);
65
+    }
66
+
67
+@Override
68
+public synchronized void write(byte[] ba) {
69
+    write(ba,0,ba.length);
70
+    }
71
+
72
+@Override
73
+public synchronized void write(byte[] ba,int str,int len) {
74
+    if(appender!=null) { appender.append(bytesToString(ba,str,len)); }
75
+    }
76
+
77
+//@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_DEFAULT_ENCODING")
78
+static private String bytesToString(byte[] ba, int str, int len) {
79
+    try { 
80
+        return new String(ba,str,len,"UTF-8"); 
81
+    } catch(UnsupportedEncodingException thr) { 
82
+        return new String(ba,str,len); 
83
+    } // all JVMs are required to support UTF-8
84
+    }
85
+
86
+// *************************************************************************************************
87
+// STATIC MEMBERS
88
+// *************************************************************************************************
89
+
90
+    static class Appender
91
+    implements Runnable
92
+    {
93
+    static private final String         EOL1="\n";
94
+    static private final String         EOL2=System.getProperty("line.separator",EOL1);
95
+    
96
+    private final JTextArea             textArea;
97
+    private final int                   maxLines;                                                   // maximum lines allowed in text area
98
+    private final LinkedList<Integer>   lengths;                                                    // length of lines within text area
99
+    private final List<String>          values;                                                     // values waiting to be appended
100
+
101
+    private int                         curLength;                                                  // length of current line
102
+    private boolean                     clear;
103
+    private boolean                     queue;
104
+
105
+    Appender(JTextArea txtara, int maxlin) {
106
+        textArea =txtara;
107
+        maxLines =maxlin;
108
+        lengths  =new LinkedList<>();
109
+        values   =new ArrayList<>();
110
+
111
+        curLength=0;
112
+        clear    =false;
113
+        queue    =true;
114
+        }
115
+
116
+    private synchronized void append(String val) {
117
+        values.add(val);
118
+        if(queue) { 
119
+            queue=false; 
120
+            EventQueue.invokeLater(this); 
121
+        }
122
+        }
123
+
124
+    private synchronized void clear() {
125
+        clear=true;
126
+        curLength=0;
127
+        lengths.clear();
128
+        values.clear();
129
+        if(queue) { 
130
+            queue=false; 
131
+            EventQueue.invokeLater(this); 
132
+        }
133
+        }
134
+
135
+    // MUST BE THE ONLY METHOD THAT TOUCHES textArea!
136
+    @Override
137
+    public synchronized void run() {
138
+        if(clear) { textArea.setText(""); }
139
+        values.stream().map((val) -> {
140
+            curLength+=val.length();
141
+            return val;
142
+        }).map((val) -> {
143
+            if(val.endsWith(EOL1) || val.endsWith(EOL2)) {
144
+                if(lengths.size()>=maxLines) { textArea.replaceRange("",0,lengths.removeFirst()); }
145
+                lengths.addLast(curLength);
146
+                curLength=0;
147
+            }
148
+            return val;
149
+        }).forEach((val) -> {
150
+            textArea.append(val);
151
+        });
152
+        values.clear();
153
+        clear =false;
154
+        queue =true;
155
+        }
156
+    }
157
+} /* END PUBLIC CLASS */