Skip to content

Commit

Permalink
Merge pull request #59 from nano-devs/development
Browse files Browse the repository at this point in the history
Custom Prefix
  • Loading branch information
madeyoga authored Nov 13, 2020
2 parents 3221fa5 + 385968c commit 6273361
Show file tree
Hide file tree
Showing 31 changed files with 385 additions and 171 deletions.
20 changes: 10 additions & 10 deletions py/vote_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
user = os.environ['MYSQL_USER']
password = ''

connection = mysql.connector.connect(user=user,
password=password,
host='127.0.0.1',
database='nano')
cursor = connection.cursor()

insert_vote_query = """
INSERT INTO vote (user_id, weekend)
VALUES ({}, {});
Expand All @@ -36,7 +30,7 @@

create_user_query = """
INSERT INTO USER (ID, RECOMMENDATION_QUOTA, DAILY_QUOTA)
VALUES ({}, 8, 1)
VALUES ({}, 5, 1)
"""

VOTE_AUTH_TOKEN = os.environ["VOTE_AUTH_NANO"]
Expand All @@ -50,6 +44,12 @@ def vote_post():
# Invalid Authorization code
if headers["Authorization"] != VOTE_AUTH_TOKEN:
return 'error', 401

connection = mysql.connector.connect(user=user,
password=password,
host='127.0.0.1',
database='nano')
cursor = connection.cursor()

data = flask.request.get_json(force=True)

Expand Down Expand Up @@ -104,11 +104,11 @@ def vote_post():

connection.commit()

cursor.close()
connection.close()

return 'success', 200

if __name__ == '__main__':
app.run(host="0.0.0.0", port="5000")
print("End")

cursor.close()
connection.close()
9 changes: 7 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import command.*;
import command.UserPlaylistCommand.CreatePlaylistCommand;
import command.general.CustomPrefixCommand;
import command.general.HelpCommand;
import command.general.InviteCommand;
import command.general.VoteCommand;
Expand Down Expand Up @@ -56,13 +57,15 @@ public static void main(String[] args) {
commandClientBuilder.setEmojis("\uD83D\uDC4C", "\u26A0", "\u2717");
commandClientBuilder.setOwnerId("213866895806300161"); // Mandatory
commandClientBuilder.setCoOwnerIds("456130311365984267");
commandClientBuilder.setActivity(Activity.listening(prefix + "help"));
commandClientBuilder.setActivity(Activity.listening("m$help"));
commandClientBuilder.useHelpBuilder(false);
commandClientBuilder.setGuildSettingsManager(nano);

// Add Command & Inject Dependencies.
// Free Commands
commandClientBuilder.addCommand(new VoteCommand(nano));
commandClientBuilder.addCommand(new InviteCommand());
commandClientBuilder.addCommand(new CustomPrefixCommand());
commandClientBuilder.addCommand(new DjModeCommand(nano));
commandClientBuilder.addCommand(new JoinCommand(nano));
commandClientBuilder.addCommand(new LeaveCommand(nano));
Expand Down Expand Up @@ -108,6 +111,9 @@ public static void main(String[] args) {

CommandClient commandClient = commandClientBuilder.build();

// Help command
commandClient.addCommand(new HelpCommand(commandClient));

// JDA Builder
JDABuilder builder = JDABuilder.createDefault(botToken);

Expand All @@ -123,7 +129,6 @@ public static void main(String[] args) {
try {
JDA jda = builder.build();
nano.setJda(jda);
commandClient.addCommand(new HelpCommand(commandClient, jda));
} catch (LoginException e) {
e.printStackTrace();
}
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/client/CustomGuildSettingsManager.java

This file was deleted.

62 changes: 36 additions & 26 deletions src/main/java/client/NanoClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package client;

import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.command.GuildSettingsManager;
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
Expand All @@ -9,17 +9,18 @@
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import database.Entity.ClassicUser;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import org.jetbrains.annotations.Nullable;
import service.music.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class NanoClient {
public class NanoClient implements GuildSettingsManager {
private JDA jda;

private final Map<Long, GuildMusicManager> musicManagers;
Expand All @@ -46,6 +47,16 @@ public synchronized GuildMusicManager getGuildAudioPlayer(Guild guild) {
if (musicManager == null) {
musicManager = new GuildMusicManager(playerManager);
musicManagers.put(guildId, musicManager);

try {
musicManager.loadSetting(guildId);

if (!musicManager.canLoadSetting()) {
musicManager.loadDefaultSetting(guildId);
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}

guild.getAudioManager().setSendingHandler(musicManager.getSendHandler());
Expand Down Expand Up @@ -84,9 +95,11 @@ public void loadAndPlayUrl(GuildMusicManager musicManager, final TextChannel cha
playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack track) {
if (track.getDuration() > 900000) {
String errorMessage = ":negative_squared_cross_mark: | cannot load song with duration longer than 15 minutes";
channel.sendMessage(errorMessage).queue();
if (track.getDuration() > musicManager.getMaxSongDuration()) {
if (channel != null) {
String errorMessage = ":negative_squared_cross_mark: | Cannot load song with duration longer than 1 hour";
channel.sendMessage(errorMessage).queue();
}
return;
}
track.setUserData(requester);
Expand Down Expand Up @@ -122,7 +135,7 @@ public void playlistLoaded(AudioPlaylist playlist) {
index += 1;
continue;
}
if (track.getDuration() > 900000) {
if (track.getDuration() > musicManager.getMaxSongDuration()) {
continue;
}
track.setUserData(requester);
Expand All @@ -135,9 +148,18 @@ public void playlistLoaded(AudioPlaylist playlist) {

PremiumService.addHistory(playlist.getName(), trackUrl, requester.getGuild(), requester.getUser());

if (channel != null)
channel.sendMessage(":white_check_mark: | " + addedSize +
" entries from **"+ playlist.getName() + "** has been added to queue").queue();
if (channel != null) {
CustomEmbedBuilder embedBuilder = new CustomEmbedBuilder();

embedBuilder.setDescription(":white_check_mark: | " + addedSize +
" entries from **" + playlist.getName() + "** has been added to queue");

embedBuilder.setAuthor("Added to queue", requester.getUser().getEffectiveAvatarUrl(),
requester.getUser().getEffectiveAvatarUrl());

embedBuilder.setFooter("Only song with duration less than 1 hour added to queue");
channel.sendMessage(embedBuilder.build()).queue();
}
}

@Override
Expand Down Expand Up @@ -200,21 +222,9 @@ public void setWaiter(EventWaiter waiter) {
this.waiter = waiter;
}

public CustomEmbedBuilder getEmbeddedVoteLink(ClassicUser classicUser, CommandEvent event) {
String voteUrl = "";
String message = "[Vote]() & use **" + event.getClient().getPrefix() +
"claim** command to claim rewards :>\n" + voteUrl;

CustomEmbedBuilder embedBuilder = new CustomEmbedBuilder();
embedBuilder.setTitle(":headphones: | Thank you for using " + event.getSelfUser().getName() + "!");
embedBuilder.setAuthor(event.getAuthor().getName() + " Stocks",
event.getAuthor().getEffectiveAvatarUrl(),
event.getAuthor().getEffectiveAvatarUrl());
embedBuilder.addField("Daily Quota", String.valueOf(classicUser.getDailyQuota()), true);
embedBuilder.addField("Claimed Reward", String.valueOf(classicUser.getRecommendationQuota()), true);
embedBuilder.addField("Increase your stocks :chart_with_upwards_trend: ", message, false);
embedBuilder.setFooter("Have a nice dayy~");

return embedBuilder;
@Nullable
@Override
public Object getSettings(Guild guild) {
return this.getGuildAudioPlayer(guild);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import database.PremiumModel;
import database.TrackModel;
import net.dv8tion.jda.api.EmbedBuilder;
import service.music.GuildMusicManager;
import service.music.HelpProcess;

import java.sql.SQLException;
Expand Down Expand Up @@ -93,7 +94,10 @@ protected void execute(CommandEvent event)
return;
}

if (db.countPlaylistTrack(db.getPlaylistId(event.getGuild().getIdLong(), playlistName, this.table), this.table) >= this.maxTrack)
GuildMusicManager musicManager = event.getClient().getSettingsFor(event.getGuild());
int guildPlaylistTrackCount = db.countPlaylistTrack(
db.getPlaylistId(event.getGuild().getIdLong(), playlistName, this.table), this.table);
if (guildPlaylistTrackCount >= musicManager.getMaxPlaylistTrackCount())
{
embed.setTitle("Failed");
embed.addField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class CreatePlaylistCommand extends GuildPlaylistBaseCommand
{
private final int maxPlaylist = 3;
private final int maxPlaylist = 5;

public CreatePlaylistCommand()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import service.music.GuildMusicManager;
import service.music.HelpProcess;
import service.music.MusicUtils;
import service.music.PremiumService;

import java.util.ArrayList;

Expand Down
1 change: 0 additions & 1 deletion src/main/java/command/JoinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.managers.AudioManager;
import service.music.GuildMusicManager;
import service.music.HelpProcess;
import service.music.MusicUtils;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/command/PlayUrlCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import client.NanoClient;
import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.managers.AudioManager;
import org.apache.commons.validator.routines.UrlValidator;
Expand Down Expand Up @@ -35,7 +34,9 @@ protected void execute(CommandEvent event) {
}

String args = event.getArgs();
if (args.isEmpty()) {
String[] schemes = {"http","https"}; // DEFAULT schemes = "http", "https", "ftp"
UrlValidator urlValidator = new UrlValidator(schemes);
if (args.isEmpty() || !urlValidator.isValid(args)) {
CustomEmbedBuilder embedBuilder = new CustomEmbedBuilder();
embedBuilder.addField(":x: | Invalid Arguments", "Example usage: "
+ event.getClient().getPrefix() + this.name + " " + this.arguments, true);
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/command/RecommendationCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
import com.jagrosh.jdautilities.command.CommandEvent;
import database.Entity.ClassicUser;
import database.UserModel;
import service.music.CustomEmbedBuilder;
import service.music.*;
import net.dv8tion.jda.api.entities.VoiceChannel;
import service.music.GuildMusicManager;
import service.music.HelpProcess;
import service.music.MusicUtils;

import java.io.IOException;
import java.sql.SQLException;
Expand Down Expand Up @@ -66,7 +63,7 @@ protected void execute(CommandEvent event) {
}

// if a number, then check if number is in valid range.
if (requestNumber <= 0 || requestNumber >= 24) {
if (requestNumber <= 0 || requestNumber > 24) {
event.reply(":x: | Please provide a number `(1 - 24)`. Example `"
+event.getClient().getPrefix()+"recommend 5` to request 5 recommendations");
return;
Expand Down Expand Up @@ -101,7 +98,7 @@ protected void execute(CommandEvent event) {
recommend(event, musicManager, requestNumber);
CompletableFuture.runAsync(() -> {
try {
userModel.create(event.getAuthor().getIdLong(), 8, 0);
userModel.create(event.getAuthor().getIdLong(), 5, 0);
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
Expand All @@ -126,7 +123,7 @@ protected void execute(CommandEvent event) {

// if Daily Quota not available & Claimed quota is not available
if (classicUser.getRecommendationQuota() < 1) {
CustomEmbedBuilder embedBuilder = this.nanoClient.getEmbeddedVoteLink(classicUser, event);
CustomEmbedBuilder embedBuilder = MusicService.getEmbeddedVoteLink(classicUser, event);
event.reply(embedBuilder.build());
return;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/command/ShowPaginatedQueueCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.menu.Paginator;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.exceptions.PermissionException;
import service.music.GuildMusicManager;
Expand Down
Loading

0 comments on commit 6273361

Please sign in to comment.