Skip to content

Commit

Permalink
Added the Twitch Live Listener
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jul 12, 2024
1 parent eb7a80f commit ac204c8
Show file tree
Hide file tree
Showing 14 changed files with 666 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/main/java/com/beanbeanjuice/cafebot/CafeBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.beanbeanjuice.cafebot.commands.interaction.*;
import com.beanbeanjuice.cafebot.commands.social.MemberCountCommand;
import com.beanbeanjuice.cafebot.commands.social.vent.VentCommand;
import com.beanbeanjuice.cafebot.commands.twitch.TwitchCommand;
import com.beanbeanjuice.cafebot.utility.commands.CommandHandler;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import com.beanbeanjuice.cafebot.utility.listeners.BotAddListener;
Expand All @@ -32,6 +33,7 @@
import com.beanbeanjuice.cafebot.utility.sections.game.TicTacToeListener;
import com.beanbeanjuice.cafebot.utility.sections.generic.HelpHandler;
import com.beanbeanjuice.cafebot.utility.sections.generic.HelpListener;
import com.beanbeanjuice.cafebot.utility.sections.twitch.TwitchHandler;
import com.sun.management.OperatingSystemMXBean;
import lombok.Getter;
import net.dv8tion.jda.api.EmbedBuilder;
Expand Down Expand Up @@ -76,6 +78,7 @@ public class CafeBot {
private CommandHandler commandHandler;
@Getter private MenuHandler menuHandler;
@Getter private HelpHandler helpHandler;
@Getter private TwitchHandler twitchHandler;

// Additional Items
@Getter private int commandsRun = 0;
Expand Down Expand Up @@ -192,10 +195,6 @@ private void setupCommands() {
new GameCommand(this),
new TicTacToeCommand(this),

// Social
new MemberCountCommand(this),
new VentCommand(this),

// Interactions
new AmazedCommand(this),
new AskCommand(this),
Expand Down Expand Up @@ -234,13 +233,21 @@ private void setupCommands() {
new UWUCommand(this),
new WaveCommand(this),
new WinkCommand(this),
new YellCommand(this)
new YellCommand(this),

// Social
new MemberCountCommand(this),
new VentCommand(this),

// Twitch
new TwitchCommand(this)

// new EmbedCommand(this)
);

this.JDA.addEventListener(commandHandler);
this.helpHandler = new HelpHandler(commandHandler);
this.twitchHandler = new TwitchHandler(System.getenv("CAFEBOT_TWITCH_ACCESS_TOKEN"), this);
}

private void setupListeners() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.beanbeanjuice.cafebot.commands.twitch;

import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.commands.twitch.channel.TwitchChannelRemoveSubCommand;
import com.beanbeanjuice.cafebot.commands.twitch.channel.TwitchChannelSetSubCommand;
import com.beanbeanjuice.cafebot.commands.twitch.role.TwitchRoleRemoveSubCommand;
import com.beanbeanjuice.cafebot.commands.twitch.role.TwitchRoleSetSubCommand;
import com.beanbeanjuice.cafebot.commands.twitch.user.TwitchAddUserSubCommand;
import com.beanbeanjuice.cafebot.commands.twitch.user.TwitchListUsersSubCommand;
import com.beanbeanjuice.cafebot.commands.twitch.user.TwitchRemoveUserSubCommand;
import com.beanbeanjuice.cafebot.utility.commands.*;
import net.dv8tion.jda.api.Permission;

public class TwitchCommand extends Command implements ICommand {

public TwitchCommand(final CafeBot cafeBot) {
super(cafeBot);
}

@Override
public String getName() {
return "twitch";
}

@Override
public String getDescription() {
return "Add or remove channels/notification channels.";
}

@Override
public CommandCategory getCategory() {
return CommandCategory.TWITCH;
}

@Override
public Permission[] getPermissions() {
return new Permission[] {
Permission.MANAGE_CHANNEL,
Permission.MANAGE_EVENTS,
Permission.MANAGE_SERVER
};
}

@Override
public boolean isEphemeral() {
return true;
}

@Override
public boolean isNSFW() {
return false;
}

@Override
public boolean allowDM() {
return false;
}

@Override
public SubCommandGroup[] getSubCommandGroups() {
SubCommandGroup userSubCommandGroup = new SubCommandGroup("user", "Add, list, or remove twitch channels.");
userSubCommandGroup.addSubCommands(new ISubCommand[] {
new TwitchAddUserSubCommand(cafeBot),
new TwitchRemoveUserSubCommand(cafeBot),
new TwitchListUsersSubCommand(cafeBot)
});

SubCommandGroup roleSubCommandGroup = new SubCommandGroup("role", "Add or remove the live notifications role.");
roleSubCommandGroup.addSubCommands(new ISubCommand[] {
new TwitchRoleSetSubCommand(cafeBot),
new TwitchRoleRemoveSubCommand(cafeBot)
});

SubCommandGroup channelSubCommandGroup = new SubCommandGroup("channel", "Add or remove the live notifications channel.");
channelSubCommandGroup.addSubCommands(new ISubCommand[] {
new TwitchChannelSetSubCommand(cafeBot),
new TwitchChannelRemoveSubCommand(cafeBot)
});

return new SubCommandGroup[] { userSubCommandGroup, roleSubCommandGroup, channelSubCommandGroup };
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.beanbeanjuice.cafebot.commands.twitch.channel;

import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType;
import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.commands.Command;
import com.beanbeanjuice.cafebot.utility.commands.ISubCommand;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;

public class TwitchChannelRemoveSubCommand extends Command implements ISubCommand {

public TwitchChannelRemoveSubCommand(final CafeBot cafeBot) {
super(cafeBot);
}

@Override
public void handle(SlashCommandInteractionEvent event) {
cafeBot.getCafeAPI().getGuildsEndpoint()
.updateGuildInformation(event.getGuild().getId(), GuildInformationType.TWITCH_CHANNEL_ID, "0")
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Channel Removed",
"The twitch notifications channel has been successfully removed."
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Removing Channel",
String.format("There was an error removing the twitch notifications channel: %s", e.getMessage())
)).queue();
return null;
});
}

@Override
public String getName() {
return "remove";
}

@Override
public String getDescription() {
return "Remove the twitch notifications channel.";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.beanbeanjuice.cafebot.commands.twitch.channel;

import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType;
import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.commands.Command;
import com.beanbeanjuice.cafebot.utility.commands.ISubCommand;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;

import java.util.Optional;

public class TwitchChannelSetSubCommand extends Command implements ISubCommand {

public TwitchChannelSetSubCommand(final CafeBot cafeBot) {
super(cafeBot);
}

@Override
public void handle(SlashCommandInteractionEvent event) {
Optional<OptionMapping> channelMapping = Optional.ofNullable(event.getOption("channel"));

String channelID = channelMapping
.map(OptionMapping::getAsChannel)
.map(GuildChannelUnion::asTextChannel)
.map(TextChannel::getId)
.orElse(event.getChannel().getId());

cafeBot.getCafeAPI().getGuildsEndpoint()
.updateGuildInformation(event.getGuild().getId(), GuildInformationType.TWITCH_CHANNEL_ID, channelID)
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Twitch Notifications Channel Set",
String.format("The live notifications channel has been successfully set to %s!", event.getGuild().getChannelById(TextChannel.class, channelID).getAsMention())
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Setting Twitch Notifications Channel",
String.format("There was an error setting the twitch notifications channel: %s", e.getMessage())
)).queue();
return null;
});
}

@Override
public String getName() {
return "set";
}

@Override
public String getDescription() {
return "Set the twitch notifications channel.";
}

@Override
public OptionData[] getOptions() {
return new OptionData[] {
new OptionData(OptionType.CHANNEL, "channel", "The channel to set the twitch channel to.", false)
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.beanbeanjuice.cafebot.commands.twitch.role;

import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType;
import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.commands.Command;
import com.beanbeanjuice.cafebot.utility.commands.ISubCommand;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;

public class TwitchRoleRemoveSubCommand extends Command implements ISubCommand {

public TwitchRoleRemoveSubCommand(final CafeBot cafeBot) {
super(cafeBot);
}

@Override
public void handle(SlashCommandInteractionEvent event) {
cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.LIVE_NOTIFICATIONS_ROLE_ID, "0")
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Successfully Removed Live Notifications Role",
"The live notifications role has been successfully removed."
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Removing Live Notifications Role",
String.format("There was an error removing the live notifications role: %s", e.getMessage())
)).queue();
return null;
});
}

@Override
public String getName() {
return "remove";
}

@Override
public String getDescription() {
return "Remove the live notifications role.";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.beanbeanjuice.cafebot.commands.twitch.role;

import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType;
import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.commands.Command;
import com.beanbeanjuice.cafebot.utility.commands.ISubCommand;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;

public class TwitchRoleSetSubCommand extends Command implements ISubCommand {

public TwitchRoleSetSubCommand(final CafeBot cafeBot) {
super(cafeBot);
}

@Override
public void handle(SlashCommandInteractionEvent event) {
Role role = event.getOption("role").getAsRole(); // Should not be null.

cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.LIVE_NOTIFICATIONS_ROLE_ID, role.getId())
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Live Notifications Role Set",
String.format("The live notifications role has been successfully set to %s.", role.getAsMention())
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Setting Live Notification Role",
String.format("There was an error setting the live notification role: %s", e.getMessage())
)).queue();
return null;
});
}

@Override
public String getName() {
return "set";
}

@Override
public String getDescription() {
return "Set the live notification role!";
}

@Override
public OptionData[] getOptions() {
return new OptionData[] {
new OptionData(OptionType.ROLE, "role", "The live notification role you want to set.", true)
};
}

}
Loading

0 comments on commit ac204c8

Please sign in to comment.