Guest User

Untitled

a guest
Jan 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. package com.k35trel.twitchbot;
  2.  
  3. public class Main
  4. {
  5.  
  6. private static final String HOST = "xxxxx";
  7. private static final int PORT = xxxxx;
  8. private static final String PASS = "oauth:xxxxx";
  9. private static final String IDENT = "xxxxx";
  10. private static final String CHANNEL = "xxxxx";
  11.  
  12. public static void main(String[] args)
  13. {
  14. Bot twitchBot = new Bot(IDENT, HOST, PASS, CHANNEL, PORT);
  15.  
  16. twitchBot.start();
  17. }
  18. }
  19.  
  20. package com.k35trel.twitchbot;
  21.  
  22. import java.io.*;
  23. import java.net.Socket;
  24.  
  25. public class Bot
  26. {
  27. private String IDENT;
  28. private String HOST;
  29. private String PASS;
  30. private String CHANNEL;
  31. private int PORT;
  32.  
  33. private Socket s;
  34.  
  35. private BufferedWriter bw;
  36. private BufferedReader br;
  37. private Chat chat;
  38.  
  39. private boolean isRunning = false;
  40.  
  41. public Bot(String IDENT, String HOST, String PASS, String CHANNEL, int PORT)
  42. {
  43. this.IDENT = IDENT.toLowerCase();
  44. this.HOST = HOST.toLowerCase();
  45. this.PASS = PASS.toLowerCase();
  46. this.CHANNEL = CHANNEL.toLowerCase();
  47. this.PORT = PORT;
  48. }
  49.  
  50. private void init()
  51. {
  52. System.out.println("Version 6");
  53.  
  54. try
  55. {
  56. s = new Socket(HOST, PORT);
  57.  
  58. bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
  59. br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  60. chat = new Chat(IDENT, CHANNEL);
  61.  
  62. chat.sendToServer(bw, "PASS " + PASS);
  63. chat.sendToServer(bw, "NICK " + IDENT);
  64. chat.sendToServer(bw, "USER " + IDENT);
  65. chat.sendToServer(bw, "CAP REQ :twitch.tv/membership");
  66. chat.sendToServer(bw, "CAP REQ :twitch.tv/commands");
  67. chat.sendToServer(bw, "JOIN #" + CHANNEL);
  68. }
  69. catch (IOException e)
  70. {
  71. e.printStackTrace();
  72. }
  73. }
  74.  
  75. private void loop()
  76. {
  77. isRunning = true;
  78. String line = "";
  79.  
  80. try
  81. {
  82. while((line = br.readLine()) != null && isRunning)
  83. {
  84. if(line.contains("PRIVMSG"))
  85. {
  86. String user = line.substring(1, line.indexOf("!"));
  87. String message = line.substring(line.indexOf(" :") + 2);
  88.  
  89. System.out.println(user + " >> " + message);
  90.  
  91. int command = chat.processCommands(bw, user, message);
  92. executeCommand(command);
  93.  
  94. }
  95. else if(line.contains("PING"))
  96. {
  97. chat.sendToServer(bw, "PONG :tmi.twitch.tv");
  98. }
  99. else if(line.contains("JOIN"))
  100. {
  101. String user = line.substring(1, line.indexOf("!"));
  102.  
  103. System.out.println(user + " has joined " + CHANNEL + "'s Channel");
  104. }
  105. else if(line.contains("PART"))
  106. {
  107. String user = line.substring(1, line.indexOf("!"));
  108.  
  109. System.out.println(user + " has left " + CHANNEL + "'s Channel");
  110. }
  111. else if(line.contains("WHISPER"))
  112. {
  113. String user = line.substring(1, line.indexOf("!"));
  114. String message = line.substring(line.indexOf(" :") + 2);
  115.  
  116. System.out.println(user + " ~~ " + message);
  117. }
  118. chat.sendToServer(bw, "PING :tmi.twitch.tv");
  119.  
  120. s.close();
  121. bw.close();
  122. br.close();
  123. }
  124.  
  125. chat.sendToServer(bw, "PART #" + CHANNEL);
  126. }
  127. catch (IOException e)
  128. {
  129. e.printStackTrace();
  130. }
  131. }
  132.  
  133. private void executeCommand(int command)
  134. {
  135. if(command == -1)
  136. {
  137. System.out.println("shutting down");
  138. isRunning = false;
  139. }
  140. }
  141.  
  142. public void start()
  143. {
  144. if(isRunning)
  145. {
  146. return;
  147. }
  148.  
  149. init();
  150. loop();
  151. }
  152. }
  153.  
  154. package com.k35trel.twitchbot;
  155.  
  156. import java.io.BufferedWriter;
  157. import java.io.IOException;
  158.  
  159. public class Chat
  160. {
  161. private String IDENT;
  162. private String CHANNEL;
  163.  
  164. public Chat(String IDENT, String CHANNEL)
  165. {
  166. this.IDENT = IDENT;
  167. this.CHANNEL = CHANNEL;
  168. }
  169.  
  170. public int processCommands(BufferedWriter bw, String user, String message)
  171. {
  172. String[] command = message.split(" ");
  173.  
  174. //example commands.
  175.  
  176. if(command[0].equals("!shutdown") && user.equals(CHANNEL))
  177. {
  178. return -1;
  179. }
  180. else if(command[0].equals("!ping"))
  181. {
  182. sendToChat(bw, "@" + user + " pong!");
  183. return 0;
  184. }
  185. else
  186. {
  187. return 0;
  188. }
  189. }
  190.  
  191. public void sendToServer(BufferedWriter bw, String message)
  192. {
  193. try
  194. {
  195. bw.write(message + "rn");
  196. bw.flush();
  197. }
  198. catch (IOException e)
  199. {
  200. e.printStackTrace();
  201. }
  202. }
  203.  
  204. public void sendToChat(BufferedWriter bw, String message)
  205. {
  206. sendToServer(bw, "PRIVMSG #" + CHANNEL + " :" + message);
  207. System.out.println(IDENT + " >> " + message);
  208. }
  209.  
  210. public void sendToUser(BufferedWriter bw, String message)
  211. {
  212. sendToServer(bw, "WISPER #" + CHANNEL + " :" + message);
  213. System.out.println(IDENT + " >> " + message);
  214. }
  215. }
  216.  
  217. package com.k35trel.twitchbot;
  218.  
  219. import java.util.HashMap;
  220.  
  221. public class User
  222. {
  223. public final String NAME;
  224. public String RANK;
  225. public int WARNINGS;
  226. private static HashMap<String, User> users = new HashMap<String, User>();
  227.  
  228. public User(String NAME)
  229. {
  230. this.NAME = NAME;
  231. RANK = "VIEWER";
  232. WARNINGS = 0;
  233. users.put(NAME, this);
  234. }
  235.  
  236. public static User getUser(String user)
  237. {
  238. if(users.containsKey(user))
  239. {
  240. return users.get(user);
  241. }
  242. else
  243. {
  244. return new User(user);
  245. }
  246. }
  247.  
  248. public static void promoteViewer(String user)
  249. {
  250. User u = getUser(user);
  251. if(u.RANK.equals("VIEWER"))
  252. {
  253. u.RANK = "MODERATOR";
  254. }
  255. }
  256.  
  257. public static void demoteModerator(String user)
  258. {
  259. User u = getUser(user);
  260. if(u.RANK.equals("MODERATOR"))
  261. {
  262. u.RANK = "VIEWER";
  263. }
  264. }
  265.  
  266. public static void addWarning(String user)
  267. {
  268. User u = getUser(user);
  269. if(u.WARNINGS < 3)
  270. {
  271. u.WARNINGS++;
  272. }
  273. }
  274.  
  275. public static void removeWarning(String user)
  276. {
  277. User u = getUser(user);
  278. if(u.WARNINGS > 0)
  279. {
  280. u.WARNINGS--;
  281. }
  282. }
  283. }
Add Comment
Please, Sign In to add comment