Skip to content

Instantly share code, notes, and snippets.

@BadLamb
Last active August 29, 2015 14:26
Show Gist options
  • Save BadLamb/6f2a9e9f13cbaf876622 to your computer and use it in GitHub Desktop.
Save BadLamb/6f2a9e9f13cbaf876622 to your computer and use it in GitHub Desktop.
ClaudiBot
This is still a wip, index has some problems, just need to call onToastDismiss and add a ended listener. Java uses Pircbot and gson as libraries
package claudibot.main;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.jibble.pircbot.PircBot;
public class Bot extends PircBot {
static List < String > song = new ArrayList < String > ();
public Bot() {
this.setName("thatlambda");
}
//Filtered words, skip shit is a wip
String[] filter = {
"rape", "troll", "trap", "autism", "retard", "allah", "jihad"
};
public void onMessage(String channel, String sender, String login, String hostname, String message) {
if (message.contains("bro ")) {
sendMessage("#thatlambda", "It's not bro, its bruh");
} else if (message.equalsIgnoreCase("!time")) {
String time = new java.util.Date().toString();
sendMessage("#thatlambda", time);
} else if (message.equalsIgnoreCase("!mod")) {
sendMessage("#thatlambda", sender + " should be mod Kappa");
} else if (message.toLowerCase().startsWith("!addsong ")) {
Boolean blocked = false;
if (message.substring(8).contains("https://soundcloud.com/")) {
for (String s: filter) {
if (message.toLowerCase().contains(s)) {
sendMessage("#thatlambda", "Song is blocked by the filter!");
blocked = true;
}
}
if (!blocked) {
song.add(message.substring(8));
sendMessage("#thatlambda", "Song has been added!");
}
} else {
sendMessage("#thatlambda", "FailFish");
}
}
}
public List < String > getList() {
return song;
}
public void delete(Object o) {
song.remove(o);
}
}
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/css/materialize.min.css">
<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="music.css" rel='stylesheet' type='text/css'>
<title>ClaudiBot
</title>
</head>
<body>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/js/materialize.min.js"></script>
<script>
var idv;
var songs = ["https://soundcloud.com/proximity-chill/echosmith-cool-kids-itachi", "https://soundcloud.com/proximity/vicetone-ft-kat-nestel-id", "https://soundcloud.com/virtual-riot/virtual-riot-idols-edm-mashup", "https://soundcloud.com/proximity/3lau-five-voices-3lau-mashup","https://soundcloud.com/proximity/maroon-5-sugar-karboncopy-remix-premiere","https://soundcloud.com/proximity-sounds/vicetone-united-we-dance","https://soundcloud.com/absencemusik/zhu-faded-absence-remix","https://soundcloud.com/proximity/sultan-shepard-ft-lauren-mason-chasing-in-the-night"];
function toastAndHide(id) {
Materialize.toast('<span>Item Deleted</span><a class="btn-flat yellow-text" onclick="toastAndUndo();">Undo<a>', 5000);
document.getElementById(id).style.display = 'none';
idv = id;
}
function getVolume(){
}
function toastAndUndo() {
Materialize.toast('<span>Item Restored</span>', 5000);
document.getElementById(idv).style.display = 'block';
}
window.onload = function() {
genList();
};
function genList() {
var items = [];
var ul = document.getElementById("songen");
for (x in songs) {
newurl = "https://api.soundcloud.com/resolve.json?url="+songs[x]+"&client_id=4dd4aaf3b11fab8f402b02219662f369";
$.getJSON( newurl, function( data ) {
$.each( data, function( key, val ) {
if(key=="title"){
items.push(val);
var li = document.createElement("li");
var btn = document.createElement('a');
btn.setAttribute("class", "secondary-content");
btn.setAttribute("href", "#");
li.setAttribute("id", x);
btn.setAttribute("onclick", "toastAndHide("+x+")");
btn.appendChild(document.createTextNode("Skip"))
li.appendChild(document.createTextNode(val));
li.appendChild(btn);
li.setAttribute("class", "collection-item");
ul.appendChild(li);
}
});
})
}
}<
<div class="container">
<div class="bar">
<h1>ClaudiBot</h1>
</div>
<ul class="collection with-header" id="songen">
<li class="collection-header">
<h4>Songs</h4>
</li>
</ul>
</ul>
<div class="player">
<a class="btn-floating btn-medium waves-effect waves-light orange accent-4" id="play_button"><i class="material-icons">play_arrow</i></a>
<div class="col s8">
<form action="#">
<p class="range-field" id="volumen">
<input type="range"class="orange accent-4" oninput="" id="test5" min="0" max="10" />
</p>
</form>
</div>
</div>
</div>
<div class="footerc">
<footer class="page-footer orange darken-3">
<div class="container">
<div class="row">
<div class="col l6 s12">
<h5 class="white-text">ClaudiBot</h5>
<p class="grey-text text-lighten-5">Coded by ThatLambda with <3 in Venice and Zurich.</p>
</div>
<div class="col l4 offset-l2 s12">
<h5 class="white-text">Powered by:</h5>
<img src="https://developers.soundcloud.com/assets/logo_big_white-65c2b096da68dd533db18b9f07d14054.png">
</div>
</div>
</div>
<div class="footer-copyright">
<div class="container">&copy 2015 ThatLambda</div>
</div>
</footer>
</div>
</body>
</html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/css/materialize.min.css">
<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<style>
.bar h1{
font-family: 'Oswald';
color:#ff6d00;
}
.imgs{
position: relative;
bottom: 0;
right: 0;
}
#volumen{
width:95%;
float:right;
padding-bottom;60px;
}
#play_button{
float:left;
margin-top:15px;
}
body {
display: flex;
min-height: 100vh;
flex-direction: column;
}
main {
flex: 1 0 auto;
}
.footerc{
position: absolute;
bottom: 0;
clear:both;
width:100%;
}
</style>
<title>ClaudiBot
</title>
</head>
<body>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/js/materialize.min.js"></script>
<div class="container">
<div class="bar">
<h1>ClaudiBot</h1>
</div>
<ul class="collection with-header" id="songen">
<li class="collection-header">
<h4>Songs</h4>
</li>
</ul>
</ul>
<div class="player">
<a class="btn-floating btn-medium waves-effect waves-light orange accent-4" onclick="playSongs();" id="play_button"><i id="button-icon" class="material-icons">pause</i></a>
<div class="col s8">
<form action="#">
<p class="range-field" id="volumen">
<input type="range"class="orange accent-4" id="test5" min="0" max="100" value="0" />
</p>
</form>
</div>
</div>
</div>
<audio id = "playeraudio"></audio>
<div class="footerc">
<footer class="page-footer orange darken-3">
<div class="container">
<div class="row">
<div class="col l6 s12">
<h5 class="white-text">ClaudiBot</h5>
<p class="grey-text text-lighten-5">Coded by ThatLambda with <3 in Venice and Zurich.</p>
</div>
<div class="col l4 offset-l2 s12">
<h5 class="white-text">Powered by:</h5>
<a href="http:/soundcloud.com"><img src="https://developers.soundcloud.com/assets/logo_big_white-65c2b096da68dd533db18b9f07d14054.png"></a>
</div>
</div>
</div>
<div class="footer-copyright">
<div class="container">&copy 2015 ThatLambda</div>
</div>
</footer>
</div>
<script>
var idv;
var loop = 0;
var ids = [];
var mslenght;
var songs = ["https://soundcloud.com/proximity/misterwives-our-own-house-heavy-mellow-remix", "https://soundcloud.com/proximity/3lau-five-voices-3lau-mashup", "https://soundcloud.com/virtual-riot/virtual-riot-idols-edm-mashup"];
var state = true;
var audio = document.getElementById("playeraudio");
function playSongs(){
if(state==false){
audio.play();
$("#button-icon").text("pause");
state=true;
}else{
audio.pause();
$("#button-icon").text("play_arrow");
state=false;
}
}
function updateVolume(volu){
audio.volume = volu/100;
}
var range = document.getElementById("test5");
range.onchange = function() {
updateVolume(range.value);
}
function toastAndHide(id) {
Materialize.toast('<span>Item Deleted</span><a class="btn-flat yellow-text" onclick="toastAndUndo();">Undo<a>', 5000, '',function(){ $("#songen").empty();songs.splice(id, 1);audio.pause();genList();});
document.getElementById(id).style.display = 'none';
idv = id;
}
function toastAndUndo() {
Materialize.toast('<span>Item Restored</span>', 5000);
document.getElementById(idv).style.display = 'block';
}
window.onload = function() {
genList();
};
function genList() {
loop=0;
var items = [];
var ul = document.getElementById("songen");
$.each( songs, function( key, val ) {
newurl = "https://api.soundcloud.com/resolve.json?url="+val+"&client_id=4dd4aaf3b11fab8f402b02219662f369";
$.getJSON( newurl, function( data ) {
$.each( data, function( key, val ) {
if(key=="title"){
items.push(val);
var li = document.createElement("li");
var btn = document.createElement('a');
btn.setAttribute("class", "secondary-content");
btn.setAttribute("href", "#");
li.setAttribute("id", loop);
btn.setAttribute("onclick", "toastAndHide("+loop+")");
loop=loop+1;
btn.appendChild(document.createTextNode("Skip"))
li.appendChild(document.createTextNode(val));
li.appendChild(btn);
li.setAttribute("class", "collection-item");
ul.appendChild(li);
}else if(key=="id"&&loop==0){
audio = new Audio('http://api.soundcloud.com/tracks/'+val+'/stream?consumer_key=4dd4aaf3b11fab8f402b02219662f369');
audio.volume = 0.1;
audio.play();
}else if(key =="duration"){
mslenght = val;
}
});
})
});
}
</script>
</body>
</html>
package claudibot.main;
import java.net.InetSocketAddress;
import java.net.URI;
import com.google.gson.Gson;
import com.sun.net.httpserver.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class main {
static String token;
private static Map < String, String > args = new HashMap < > ();
public static void main(String[] args) throws InterruptedException {
// ClaudiBot; an IRC twitch bot coded with <3 in Venice
try {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/", new MyHandler());
server.createContext("/music", new GetMusicHandler());
server.createContext("/remove", new RemoveMusicHandler());
server.setExecutor(null);
server.start();
String url = "https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=bj3ldtn7o228462ppg4gr7c949ghgoa&redirect_uri=http://localhost:8000&scope=chat_login";
Desktop.getDesktop().browse(new URI(url));
} catch (Exception e) {
System.out.println("An error happened, please send this to Lambda for a free hug!:");
System.out.println(e.getMessage());
java.awt.Toolkit.getDefaultToolkit().beep();
Thread.sleep(1000000000);
}
}
static class GetMusicHandler implements HttpHandler {@Override
public void handle(HttpExchange t) throws IOException {
Bot b = new Bot();
String json = new Gson().toJson(b.getList());
String response = json;
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
static class RemoveMusicHandler implements HttpHandler {@Override
public void handle(HttpExchange t) throws IOException {
String response;
try {
Map argso = queryToMap(t.getRequestURI().getQuery());
Bot b = new Bot();
b.delete(argso.get("song"));
response = b.getList().toString();
} catch (Exception e) {
response = "An error happened! Send this to Lambda for a free hug!\n" + e.toString();
}
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
static class MyHandler implements HttpHandler {@Override
public void handle(HttpExchange t) throws IOException {
String root = "C:";
URI uri = t.getRequestURI();
File file = new File(root + "/music.html");
// Object exists and is a file: accept with response code 200.
t.sendResponseHeaders(200, 0);
OutputStream os = t.getResponseBody();
FileInputStream fs = new FileInputStream(file);
final byte[] buffer = new byte[0x10000];
int count = 0;
while ((count = fs.read(buffer)) >= 0) {
os.write(buffer, 0, count);
}
fs.close();
os.close();
args = queryToMap(t.getRequestURI().getQuery());
//We got the codes! now we need to post to twitch to get the token
String url = "https://api.twitch.tv/kraken/oauth2/token";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "USER_AGENT");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "client_id=bj3ldtn7o228462ppg4gr7c949ghgoa&client_secret=g5dg4fjsdghxhhm38gm0ft7ogbfcm8g&grant_type=authorization_code&redirect_uri=http://localhost:8000&code=" + args.get("code");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer resp = new StringBuffer();
while ((inputLine = in .readLine()) != null) {
resp.append(inputLine);
} in .close();
Map jsonJavaRootObject = new Gson().fromJson(resp.toString(), Map.class);
token = jsonJavaRootObject.get("access_token").toString();
ircBot();
}
void ircBot() {
Bot b = new Bot();
b.setVerbose(true);
try {
b.connect("irc.twitch.tv", 6667, "oauth:" + token);
b.joinChannel("#thatlambda");
} catch (Exception e) {
}
}
}
public static Map < String, String > queryToMap(String query) {
Map < String, String > result = new HashMap < String, String > ();
for (String param: query.split("&")) {
String pair[] = param.split("=");
if (pair.length > 1) {
result.put(pair[0], pair[1]);
} else {
result.put(pair[0], "");
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment