Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.k35trel.twitchbot;
- public class Main
- {
- private static final String HOST = "xxxxx";
- private static final int PORT = xxxxx;
- private static final String PASS = "oauth:xxxxx";
- private static final String IDENT = "xxxxx";
- private static final String CHANNEL = "xxxxx";
- public static void main(String[] args)
- {
- Bot twitchBot = new Bot(IDENT, HOST, PASS, CHANNEL, PORT);
- twitchBot.start();
- }
- }
- package com.k35trel.twitchbot;
- import java.io.*;
- import java.net.Socket;
- public class Bot
- {
- private String IDENT;
- private String HOST;
- private String PASS;
- private String CHANNEL;
- private int PORT;
- private Socket s;
- private BufferedWriter bw;
- private BufferedReader br;
- private Chat chat;
- private boolean isRunning = false;
- public Bot(String IDENT, String HOST, String PASS, String CHANNEL, int PORT)
- {
- this.IDENT = IDENT.toLowerCase();
- this.HOST = HOST.toLowerCase();
- this.PASS = PASS.toLowerCase();
- this.CHANNEL = CHANNEL.toLowerCase();
- this.PORT = PORT;
- }
- private void init()
- {
- System.out.println("Version 6");
- try
- {
- s = new Socket(HOST, PORT);
- bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- chat = new Chat(IDENT, CHANNEL);
- chat.sendToServer(bw, "PASS " + PASS);
- chat.sendToServer(bw, "NICK " + IDENT);
- chat.sendToServer(bw, "USER " + IDENT);
- chat.sendToServer(bw, "CAP REQ :twitch.tv/membership");
- chat.sendToServer(bw, "CAP REQ :twitch.tv/commands");
- chat.sendToServer(bw, "JOIN #" + CHANNEL);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- private void loop()
- {
- isRunning = true;
- String line = "";
- try
- {
- while((line = br.readLine()) != null && isRunning)
- {
- if(line.contains("PRIVMSG"))
- {
- String user = line.substring(1, line.indexOf("!"));
- String message = line.substring(line.indexOf(" :") + 2);
- System.out.println(user + " >> " + message);
- int command = chat.processCommands(bw, user, message);
- executeCommand(command);
- }
- else if(line.contains("PING"))
- {
- chat.sendToServer(bw, "PONG :tmi.twitch.tv");
- }
- else if(line.contains("JOIN"))
- {
- String user = line.substring(1, line.indexOf("!"));
- System.out.println(user + " has joined " + CHANNEL + "'s Channel");
- }
- else if(line.contains("PART"))
- {
- String user = line.substring(1, line.indexOf("!"));
- System.out.println(user + " has left " + CHANNEL + "'s Channel");
- }
- else if(line.contains("WHISPER"))
- {
- String user = line.substring(1, line.indexOf("!"));
- String message = line.substring(line.indexOf(" :") + 2);
- System.out.println(user + " ~~ " + message);
- }
- chat.sendToServer(bw, "PING :tmi.twitch.tv");
- s.close();
- bw.close();
- br.close();
- }
- chat.sendToServer(bw, "PART #" + CHANNEL);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- private void executeCommand(int command)
- {
- if(command == -1)
- {
- System.out.println("shutting down");
- isRunning = false;
- }
- }
- public void start()
- {
- if(isRunning)
- {
- return;
- }
- init();
- loop();
- }
- }
- package com.k35trel.twitchbot;
- import java.io.BufferedWriter;
- import java.io.IOException;
- public class Chat
- {
- private String IDENT;
- private String CHANNEL;
- public Chat(String IDENT, String CHANNEL)
- {
- this.IDENT = IDENT;
- this.CHANNEL = CHANNEL;
- }
- public int processCommands(BufferedWriter bw, String user, String message)
- {
- String[] command = message.split(" ");
- //example commands.
- if(command[0].equals("!shutdown") && user.equals(CHANNEL))
- {
- return -1;
- }
- else if(command[0].equals("!ping"))
- {
- sendToChat(bw, "@" + user + " pong!");
- return 0;
- }
- else
- {
- return 0;
- }
- }
- public void sendToServer(BufferedWriter bw, String message)
- {
- try
- {
- bw.write(message + "rn");
- bw.flush();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- public void sendToChat(BufferedWriter bw, String message)
- {
- sendToServer(bw, "PRIVMSG #" + CHANNEL + " :" + message);
- System.out.println(IDENT + " >> " + message);
- }
- public void sendToUser(BufferedWriter bw, String message)
- {
- sendToServer(bw, "WISPER #" + CHANNEL + " :" + message);
- System.out.println(IDENT + " >> " + message);
- }
- }
- package com.k35trel.twitchbot;
- import java.util.HashMap;
- public class User
- {
- public final String NAME;
- public String RANK;
- public int WARNINGS;
- private static HashMap<String, User> users = new HashMap<String, User>();
- public User(String NAME)
- {
- this.NAME = NAME;
- RANK = "VIEWER";
- WARNINGS = 0;
- users.put(NAME, this);
- }
- public static User getUser(String user)
- {
- if(users.containsKey(user))
- {
- return users.get(user);
- }
- else
- {
- return new User(user);
- }
- }
- public static void promoteViewer(String user)
- {
- User u = getUser(user);
- if(u.RANK.equals("VIEWER"))
- {
- u.RANK = "MODERATOR";
- }
- }
- public static void demoteModerator(String user)
- {
- User u = getUser(user);
- if(u.RANK.equals("MODERATOR"))
- {
- u.RANK = "VIEWER";
- }
- }
- public static void addWarning(String user)
- {
- User u = getUser(user);
- if(u.WARNINGS < 3)
- {
- u.WARNINGS++;
- }
- }
- public static void removeWarning(String user)
- {
- User u = getUser(user);
- if(u.WARNINGS > 0)
- {
- u.WARNINGS--;
- }
- }
- }
Add Comment
Please, Sign In to add comment