Skip to content

Commit

Permalink
Merge pull request #43 from Mazawrath/Twitch
Browse files Browse the repository at this point in the history
Twitch update
  • Loading branch information
Mazawrath authored Jan 6, 2019
2 parents 865a7a0 + 1d5af8d commit 4383fd5
Show file tree
Hide file tree
Showing 16 changed files with 880 additions and 130 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ build/*
out/
.gradle*

# Twitch Files
config/

launch.sh

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ beanBOT is my own personal bot that I made for myself, for a discord server I'm
- Download or clone this repository.
- Install [RethinkDB.](https://www.rethinkdb.com/docs/install/)
- Set up an application on [discord.](https://discordapp.com/developers/applications/)
- Add a bot and obtain its token (<TOKEN>).
- Add a bot and obtain its token (TOKEN).

### Build
`$ gradlew build`

### Run
- Via gradle:
`$ gradlew run -Ptoken="<TOKEN>"`
`$ gradlew run -Ptoken="TOKEN"`
- Standalone:
`$ java -jar beanBOT-2.0.jar "<TOKEN>"`
`$ java -jar beanBOT-2.0.jar "TOKEN"`

## Things to note about this bot
This bot is made for my own personal use. It's currently being used by a streamer and there are a lot of commands made specificly for him such as the one under copypasta and the entire Bean Market. There are also `.admin` commands that allow you to change values and modify the bot. Although these are not in the `.help` command, you can find these commands in the source code under `admin`. All commands can only be used by the bot owner and some can be used by the server owner.
This bot is made for my own personal use. It's currently being used by a streamer and there are a lot of commands made specifically for him such as the one under copypasta and the entire Bean Market. There are also `.admin` commands that allow you to change values and modify the bot. Although these are not in the `.help` command, you can find these commands in the source code under `admin`. All commands can only be used by the bot owner and some can be used by the server owner.

beanBOT is a Java bot that I have made for https://www.twitch.tv/shteeeb. Check him out for #EpicGamerMoments
73 changes: 52 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'com.github.johnrengelman.shadow' version '4.0.3'
id 'com.github.johnrengelman.shadow' version '4.0.3'
}

apply plugin: 'java'
Expand All @@ -8,32 +8,63 @@ sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
maven {
url 'https://jitpack.io'
}
mavenCentral()
maven {
url 'https://jitpack.io'
}
mavenCentral()
}

dependencies {
implementation 'org.javacord:javacord:3.0.0'

compile (
'com.github.Mazawrath.sdcf4j:sdcf4j-core:v1.1.0',
'com.github.Mazawrath.sdcf4j:sdcf4j-javacord:v1.1.0',
'com.rethinkdb:rethinkdb-driver:2.3.3',
'com.yahoofinance-api:YahooFinanceAPI:3.14.0',
'com.vdurmont:emoji-java:4.0.0',
'org.apache.commons:commons-lang3:3.8',
'org.slf4j:slf4j-api:1.7.5',
'org.slf4j:slf4j-log4j12:1.7.5'

compileOnly('org.projectlombok:lombok:1.18.4')
annotationProcessor 'org.projectlombok:lombok:1.18.4'

compile(
'com.github.Mazawrath.sdcf4j:sdcf4j-core:v1.1.0',
'com.github.Mazawrath.sdcf4j:sdcf4j-javacord:v1.1.0',

'com.rethinkdb:rethinkdb-driver:2.3.3',

'com.yahoofinance-api:YahooFinanceAPI:3.14.0',

'com.vdurmont:emoji-java:4.0.0',

'org.apache.commons:commons-lang3:3.8',

'com.github.twitch4j:twitch4j:v0.12.0',

'org.slf4j:slf4j-api:1.7.25',
'org.slf4j:slf4j-simple:1.7.25',

'org.eclipse.jetty:jetty-server:9.4.6.v20170531',
'org.eclipse.jetty:jetty-servlet:9.4.6.v20170531',

'org.glassfish.jersey.core:jersey-server:2.27',
'org.glassfish.jersey.containers:jersey-container-servlet-core:2.27',
'org.glassfish.jersey.containers:jersey-container-jetty-http:2.27',
'org.glassfish.jersey.media:jersey-media-json-jackson:2.27',
'org.glassfish.jersey.inject:jersey-hk2:2.27',

'javax.xml.bind:jaxb-api:2.3.0',
'javax.activation:activation:1.1',

'org.toile-libre.libe:curl:0.0.23',

'com.google.api-client:google-api-client:1.24.1'
)

testCompile (
'junit:junit:4.12'
runtime(
'org.glassfish.jersey.media:jersey-media-json-jackson:2.27'
)


testCompile(
'junit:junit:4.12'
)
}

shadowJar{
shadowJar {
manifest {
attributes 'Main-Class': 'com.mazawrath.beanbot.Main'
}
Expand All @@ -44,15 +75,15 @@ build.dependsOn {
shadowJar
}

task run(dependsOn:shadowJar) {
task run(dependsOn: shadowJar) {
doLast {
if (project.hasProperty('token')) {
javaexec {
main='-jar'
main = '-jar'
args jar.archivePath, project.property('token')
}
} else {
throw new GradleException('Missing property token')
}
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group = com.mazawrath.beanbot
version = 2.0
projectName = beanBOT
description = beanBOT
description = beanBOT
43 changes: 26 additions & 17 deletions src/main/java/com/mazawrath/beanbot/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,41 @@
import com.mazawrath.beanbot.commands.copypasta.GiveModCommand;
import com.mazawrath.beanbot.commands.beancoin.*;
import com.mazawrath.beanbot.commands.copypasta.*;
import com.mazawrath.beanbot.utilities.Lottery;
import com.mazawrath.beanbot.utilities.*;
import com.mazawrath.beanbot.commands.admin.*;
import com.mazawrath.beanbot.utilities.Points;
import com.mazawrath.beanbot.utilities.StockMarket;
import com.mazawrath.beanbot.utilities.jersey.RestServer;
import com.rethinkdb.net.Connection;
import de.btobastian.sdcf4j.CommandHandler;
import de.btobastian.sdcf4j.handler.JavacordHandler;
import org.apache.log4j.BasicConfigurator;
//import org.apache.log4j.BasicConfigurator;
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.util.logging.FallbackLoggerConfiguration;

import static com.rethinkdb.RethinkDB.r;

public class Main {
private static DiscordApi api;

public static void main(String[] args) {
BasicConfigurator.configure();
//BasicConfigurator.configure();
// Enable debugging, if no slf4j logger was found
FallbackLoggerConfiguration.setDebug(false);
//FallbackLoggerConfiguration.setDebug(false);

Points points = new Points();
StockMarket stockMarket = new StockMarket();
Lottery lottery = new Lottery();
Connection conn = r.connection().hostname("localhost").port(28015).connect();

points.connectDatabase();
stockMarket.connectDatabase();
lottery.connectDatabase();
Points points = new Points(conn);
StockMarket stockMarket = new StockMarket(conn);
Lottery lottery = new Lottery(conn);
Thread restServer = new Thread(new RestServer());
restServer.start();
Twitch twitch = new Twitch(args[1], args[2], conn);

String token = args[0];

new DiscordApiBuilder().setToken(token).login().thenAccept(api -> {
//System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
new DiscordApiBuilder().setToken(args[0]).login().thenAccept(api -> {
System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());

Main.api = api;
Twitch.setApi(api);

// Instantiate command handler
CommandHandler cmdHandler = new JavacordHandler(api);
Expand All @@ -59,7 +66,7 @@ public static void main(String[] args) {
// Bean Market
cmdHandler.registerCommand(new BeanMarketCommand());
cmdHandler.registerCommand(new BeanInvestCommand(points, stockMarket));
// Bean Lottery
// Bean Lottery Commands
cmdHandler.registerCommand(new BeanLotteryCommand(points, lottery));
// Admin commands
cmdHandler.registerCommand(new AdminPostChangeLogCommand());
Expand All @@ -69,6 +76,7 @@ public static void main(String[] args) {
cmdHandler.registerCommand(new AdminRemoveBeanCoinCommand(points));
cmdHandler.registerCommand(new AdminPostMessageCommand());
cmdHandler.registerCommand(new AdminPostHelpCommand(cmdHandler));
cmdHandler.registerCommand(new AdminTwitch(twitch));
// Copypasta
cmdHandler.registerCommand(new Top500Command(points));
cmdHandler.registerCommand(new GiveModCommand(points));
Expand All @@ -84,4 +92,5 @@ public static void main(String[] args) {
cmdHandler.registerCommand(new EightBallCommand(points));
});
}

}
20 changes: 10 additions & 10 deletions src/main/java/com/mazawrath/beanbot/commands/UserInfoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ public UserInfoCommand(Points points) {
public void onCommand(String[] args, DiscordApi api, ServerTextChannel serverTextChannel, User author, Server server) {
final String[] roleList = {""};
final String[] playing = {""};
String userName;
final String[] userName = new String[1];

if (args.length != 0) {
if (!args[0].contains("#")) {
if (args[0].contains("@")) {
userName[0] = args[0].substring(2, args[0].length() - 1);
} else if (args[0].contains("#")) {
api.getCachedUserByDiscriminatedNameIgnoreCase(args[0]).ifPresent(user -> userName[0] = user.getIdAsString());
} else {
serverTextChannel.sendMessage("Username is not valid!");
userName = "null#000000000000";
} else if (args[0].contains("@")) {
serverTextChannel.sendMessage("Do not mention the user, put in their full username (Example#0000) without a '@' in front.");
userName = "null#000000000000";
} else
userName = args[0];
return;
}
} else
userName = author.getDiscriminatedName();
api.getCachedUserByDiscriminatedNameIgnoreCase(userName).ifPresent(user -> {
userName[0] = author.getIdAsString();
api.getUserById(userName[0]).thenAccept(user -> {
if (user.getRoles(server).get(0).getName().equals("@everyone") && user.getRoles(server).size() == 1) {
roleList[0] = roleList[0] + "None";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ public void onCommand(String[] args, DiscordApi api, ServerTextChannel serverTex

private String getRecentChangeLog() {
return "**New beanBOT update released.**\n" +
"Release can be found on https://github.com/Mazawrath/beanBOT/releases/tag/v3.0.1\n" +
"Detailed changelog can be found on https://github.com/Mazawrath/beanBOT/compare/v3.0.0...v3.0.1\n" +
"Release can be found on https://github.com/Mazawrath/beanBOT/releases/tag/v3.1.0\n" +
"Detailed changelog can be found on https://github.com/Mazawrath/beanBOT/compare/v3.0.1...v3.1.0\n" +
"\n" +
"**v3.0.1**\n" +
// "**New**\n" +
// "\t- Added the Bean Lottery.\n" +
// "**Changes**\n" +
// "\t- Completely redesigned way to build and run beanBOT.\n" +
// "\t- Reverted Beanmas.\n" +
"**Bug Fixes**\n" +
"\t- Fixed an issue with `.beanlottery` not giving help information.\n";
"**v3.1.0**\n" +
"**New**\n" +
"\t- Twitch Update\n" +
"\t\t- beanBOT can now notify servers when a Twitch channel goes live.\n" +
"\t\t- Server owners can now type `.admintwitch add [twitch channel name]` to subscribe to live notifications for a twitch channel.\n" +
"**Changes**\n" +
"\t- Increased price of `.beanlottery draw` from 400 beanCoin to 50,000 beanCoin.\n" +
"\t- Users can now be mentioned when transferring beancoin or looking up user info.\n" +
"\t\t- Commands like `.userinfo` and `.beantransfer` now can have users tagged with `@`.\n" +
"\t- Added a mysterious tax for coins going to beanBOT...";
// "**Bug Fixes**\n" +
// "\t- Fixed an issue with `.beanlottery` not giving help information.\n";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.mazawrath.beanbot.commands.admin;

import com.mazawrath.beanbot.utilities.Twitch;
import de.btobastian.sdcf4j.Command;
import de.btobastian.sdcf4j.CommandExecutor;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.channel.ServerTextChannel;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;

import java.util.concurrent.ExecutionException;

public class AdminTwitch implements CommandExecutor {
private Twitch twitch;

public AdminTwitch(Twitch twitch) {
this.twitch = twitch;
}

@Command(
aliases = {"AdminTwitch"},
privateMessages = false,
showInHelpPage = false
)

public void onCommand(String[] args, DiscordApi api, ServerTextChannel serverTextChannel, User author, Server server) throws ExecutionException, InterruptedException {
if (!author.isBotOwner() && !server.isOwner(author)) {
serverTextChannel.sendMessage("Only " + api.getOwner().get().getDiscriminatedName() + " or " + server.getOwner().getDisplayName(server) + " can use this command.");
return;
}

if (args[0].equalsIgnoreCase("add")) {
int subscriptionStatus = twitch.addServer(args[1], server.getIdAsString(), serverTextChannel.getIdAsString());
if (subscriptionStatus == 1)
serverTextChannel.sendMessage("Subscribed to live notifications for " + args[1] + ".");
else if (subscriptionStatus == 0)
serverTextChannel.sendMessage("Could not subscribe to " + args[1] + ".");
else
serverTextChannel.sendMessage("Could not subscribe to " + args[1] + ". You are already subscribed to livestream " +
"notifications for another channel. Use `.admintwitchsettings remove` to unsubscribe from those notifications.");
} else if (args[0].equalsIgnoreCase("remove")) {
if (twitch.removeServer(server.getIdAsString()))
serverTextChannel.sendMessage("Unsubscribed from live notifications.");
else
serverTextChannel.sendMessage("Could not unsubscribe from live notifications. Most likely you are not subscribed to " +
"live notifications for a channel. Use `.admintwitchsettings add [channel name]` to subscribe for livestream notifications for that channel.");
} else if (args[0].equals("set")) {
String serverTextChannelId = args[1].substring(2,args[1].length() -1);
if (api.getServerTextChannelById(serverTextChannelId).isPresent()) {
if (twitch.setChannel(server.getIdAsString(), serverTextChannelId))
serverTextChannel.sendMessage("Notification channel set to " + api.getServerTextChannelById(serverTextChannelId).get().getName() + ".");
else
serverTextChannel.sendMessage("Could not set a text channel for live notifications. Most likely you are not subscribed to " +
"live notifications for a twitch channel. Use `.admintwitchsettings add [channel name]` to subscribe for livestream notifications for that channel.");
} else
serverTextChannel.sendMessage("Invalid channel.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ public BeanTransferCommand(Points points) {

public void onCommand(String[] args, ServerTextChannel serverTextChannel, DiscordApi api, User author, Server server) {
if (args.length >= 2) {
if (!args[0].contains("#")) {
if (args[0].contains("@")) {
args[0] = args[0].substring(2,args[0].length() - 1);
} else if (args[0].contains("#")) {
api.getCachedUserByDiscriminatedNameIgnoreCase(args[0]).ifPresent(user -> args[0] = user.getIdAsString());
} else {
serverTextChannel.sendMessage("Username is not valid!");
args[0] = "null#000000000000";
} else if (args[0].contains("@")) {
serverTextChannel.sendMessage("Do not mention the user, put in their full username (Example#0000) without a '@' in front.");
args[0] = "null#000000000000";
return;
}
api.getCachedUserByDiscriminatedNameIgnoreCase(args[0]).ifPresent(user -> {

api.getUserById(args[0]).thenAccept(user -> {
if (Points.isProperDecimal(args[1])) {
BigDecimal transferPoints = new BigDecimal(args[1]).setScale(Points.SCALE, Points.ROUNDING_MODE);
if (transferPoints.compareTo(BigDecimal.ZERO) != 0) {
Expand Down
Loading

0 comments on commit 4383fd5

Please sign in to comment.