aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/commands/CommandManager.java
blob: d82033ee2a5fad26ed5673a88469055ea22f922e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package commands;

import builders.ScheduleMessageBuilder;
import fileutils.UpcomingChannelsManager;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import vtuber.ScheduleHandler;

import java.util.ArrayList;

public class CommandManager extends ListenerAdapter {
    private ScheduleHandler scheduleHandler;
    private ScheduleMessageBuilder scb;
    private UpcomingChannelsManager ucm;
    private long adminRole;
    public CommandManager(String holodexAPIKey, long adminRole) {
        scheduleHandler = new ScheduleHandler(holodexAPIKey);
        scb = new ScheduleMessageBuilder();
        ucm = new UpcomingChannelsManager();
        this.adminRole = adminRole;
        System.out.println("CommandManager initialized");
    }
    @Override
    public void onSlashCommandInteraction(SlashCommandInteractionEvent e) {
        String command = e.getName();
        MessageEmbed scheduleMessage;
        switch (command) {
            case "remove-config":
                if (!hasPermission(e)){
                    e.reply("Sorry, you don't have permission to run this command").queue();
                    return;
                }
                String searchTerm = e.getOption("term").getAsString();
                long currentDiscChannelID = e.getChannel().getIdLong();
                ucm.removeEntry(searchTerm, currentDiscChannelID);
                e.reply("Successfully removed " + searchTerm + " from this channel. Please restart the bot for this to take effect").queue();
                break;
            case "configure":
                if (!hasPermission(e)){
                    e.reply("Sorry, you don't have permission to run this command").queue();
                    return;
                }
                System.out.println("RUNNING?");
                String type  = e.getOption("type").getAsString();
                String id = e.getOption("id").getAsString();
                long discordChannelId = e.getChannel().getIdLong();
                if (checkValidConfig(type, id) == false){
                    e.reply("Sorry, I couldn't find any information on that channel. Please check the ID you provided").queue();
                    return;
                }
                ucm.addNewEntry(type, id, Long.toString(discordChannelId));
                e.reply("Successfully added " + type + " " + id + " feed to this channel. Please restart the bot for this to take effect").queue();
                break;
            case "schedule-channel":
                String channelId = e.getOption("channel-id").getAsString();
                if (scheduleHandler.channelExists(channelId) == false) {
                    e.reply("Sorry, I couldn't find any information on that channel. Please check the ID you provided").queue();
                    return;
                }
                scheduleMessage = scb.buildLiveAndUpcomingMessage(scheduleHandler.getScheduleChannelId(channelId, 10));
                e.deferReply().queue();
                e.getHook().sendMessageEmbeds(scheduleMessage).queue();
                break;
            case "schedule":
                String organization = e.getOption("organization").getAsString();
                organization = organization.replaceAll(" ", "%20");
                if (scheduleHandler.organizationExists(organization) == false) {
                    e.reply("Sorry, I couldn't find any information on that organization. Please ensure it matches Holodex's spelling").queue();
                    return;
                }
                scheduleMessage = scb.buildLiveAndUpcomingMessage(scheduleHandler.getSchedule(organization, 10));
                e.deferReply().queue();
                e.getHook().sendMessageEmbeds(scheduleMessage).queue();
                break;
            default:
                e.reply("Unknown command received").queue();
                break;
        }
    }

    public ArrayList<MessageEmbed> updateUpcomingChannel(String name, String type){
        ArrayList<MessageEmbed> messageEmbeds = new ArrayList<>();
        switch (type) {
            case "org":
                messageEmbeds = scb.getUpcomingLiveListMessages(scheduleHandler.getSchedule(name));
                break;
            case "channel":
                messageEmbeds = scb.getUpcomingLiveListMessages(scheduleHandler.getScheduleChannelId(name));
                break;
            default:
                System.out.println("Unknown type");
                break;
        }
        return messageEmbeds;
    }

    public boolean checkValidConfig(String type, String id){
        switch (type) {
            case "org":
                if (scheduleHandler.organizationExists(id) == false) {
                    return false;
                }
                break;
            case "channel":
                if (scheduleHandler.channelExists(id) == false) {
                    return false;
                }
                break;
            default:
                System.out.println("Unknown type");
                throw new IllegalArgumentException("Unknown type");
        }
        return true;
    }

    public boolean hasPermission(SlashCommandInteractionEvent e){
        if (e.getMember().isOwner() || e.getMember().getRoles().contains(e.getGuild().getRoleById(adminRole))){
            return true;
        }
        return false;
    }

}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage