Search in sources :

Example 6 with EventWaiter

use of com.jagrosh.jdautilities.commons.waiter.EventWaiter in project MusicBot by jagrosh.

the class JMusicBot method startBot.

private static void startBot() {
    // create prompt to handle startup
    Prompt prompt = new Prompt("JMusicBot");
    // startup checks
    OtherUtil.checkVersion(prompt);
    OtherUtil.checkJavaVersion(prompt);
    // load config
    BotConfig config = new BotConfig(prompt);
    config.load();
    if (!config.isValid())
        return;
    LOG.info("Loaded config from " + config.getConfigLocation());
    // set up the listener
    EventWaiter waiter = new EventWaiter();
    SettingsManager settings = new SettingsManager();
    Bot bot = new Bot(waiter, config, settings);
    AboutCommand aboutCommand = new AboutCommand(Color.BLUE.brighter(), "a music bot that is [easy to host yourself!](https://github.com/jagrosh/MusicBot) (v" + OtherUtil.getCurrentVersion() + ")", new String[] { "High-quality music playback", "FairQueue™ Technology", "Easy to host yourself" }, RECOMMENDED_PERMS);
    aboutCommand.setIsAuthor(false);
    // 🎶
    aboutCommand.setReplacementCharacter("\uD83C\uDFB6");
    // set up the command client
    CommandClientBuilder cb = new CommandClientBuilder().setPrefix(config.getPrefix()).setAlternativePrefix(config.getAltPrefix()).setOwnerId(Long.toString(config.getOwnerId())).setEmojis(config.getSuccess(), config.getWarning(), config.getError()).setHelpWord(config.getHelp()).setLinkedCacheSize(200).setGuildSettingsManager(settings).addCommands(aboutCommand, new PingCommand(), new SettingsCmd(bot), new LyricsCmd(bot), new NowplayingCmd(bot), new PlayCmd(bot), new PlaylistsCmd(bot), new QueueCmd(bot), new RemoveCmd(bot), new SearchCmd(bot), new SCSearchCmd(bot), new ShuffleCmd(bot), new SkipCmd(bot), new ForceRemoveCmd(bot), new ForceskipCmd(bot), new MoveTrackCmd(bot), new PauseCmd(bot), new PlaynextCmd(bot), new RepeatCmd(bot), new SkiptoCmd(bot), new StopCmd(bot), new VolumeCmd(bot), new PrefixCmd(bot), new SetdjCmd(bot), new SkipratioCmd(bot), new SettcCmd(bot), new SetvcCmd(bot), new AutoplaylistCmd(bot), new DebugCmd(bot), new PlaylistCmd(bot), new SetavatarCmd(bot), new SetgameCmd(bot), new SetnameCmd(bot), new SetstatusCmd(bot), new ShutdownCmd(bot));
    if (config.useEval())
        cb.addCommand(new EvalCmd(bot));
    boolean nogame = false;
    if (config.getStatus() != OnlineStatus.UNKNOWN)
        cb.setStatus(config.getStatus());
    if (config.getGame() == null)
        cb.useDefaultGame();
    else if (config.getGame().getName().equalsIgnoreCase("none")) {
        cb.setActivity(null);
        nogame = true;
    } else
        cb.setActivity(config.getGame());
    if (!prompt.isNoGUI()) {
        try {
            GUI gui = new GUI(bot);
            bot.setGUI(gui);
            gui.init();
        } catch (Exception e) {
            LOG.error("Could not start GUI. If you are " + "running on a server or in a location where you cannot display a " + "window, please run in nogui mode using the -Dnogui=true flag.");
        }
    }
    // attempt to log in and start
    try {
        JDA jda = JDABuilder.create(config.getToken(), Arrays.asList(INTENTS)).enableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE).disableCache(CacheFlag.ACTIVITY, CacheFlag.CLIENT_STATUS, CacheFlag.EMOTE, CacheFlag.ONLINE_STATUS).setActivity(nogame ? null : Activity.playing("loading...")).setStatus(config.getStatus() == OnlineStatus.INVISIBLE || config.getStatus() == OnlineStatus.OFFLINE ? OnlineStatus.INVISIBLE : OnlineStatus.DO_NOT_DISTURB).addEventListeners(cb.build(), waiter, new Listener(bot)).setBulkDeleteSplittingEnabled(true).build();
        bot.setJDA(jda);
    } catch (LoginException ex) {
        prompt.alert(Prompt.Level.ERROR, "JMusicBot", ex + "\nPlease make sure you are " + "editing the correct config.txt file, and that you have used the " + "correct token (not the 'secret'!)\nConfig Location: " + config.getConfigLocation());
        System.exit(1);
    } catch (IllegalArgumentException ex) {
        prompt.alert(Prompt.Level.ERROR, "JMusicBot", "Some aspect of the configuration is " + "invalid: " + ex + "\nConfig Location: " + config.getConfigLocation());
        System.exit(1);
    }
}
Also used : GUI(com.jagrosh.jmusicbot.gui.GUI) CommandClientBuilder(com.jagrosh.jdautilities.command.CommandClientBuilder) LoginException(javax.security.auth.login.LoginException) SettingsManager(com.jagrosh.jmusicbot.settings.SettingsManager) EventWaiter(com.jagrosh.jdautilities.commons.waiter.EventWaiter) LoginException(javax.security.auth.login.LoginException) Prompt(com.jagrosh.jmusicbot.entities.Prompt)

Example 7 with EventWaiter

use of com.jagrosh.jdautilities.commons.waiter.EventWaiter in project flowermoon by wolf-yuan-6115.

the class JMusicBot method main.

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // startup log
    Logger log = LoggerFactory.getLogger("啟動");
    // create prompt to handle startup
    Prompt prompt = new Prompt("機器人", "正在切換到無視窗模式,您可以使用 -Dnogui=false 來關閉這個功能");
    // get and check latest version
    String version = OtherUtil.checkVersion(prompt);
    // check for valid java version
    if (!System.getProperty("java.vm.name").contains("64"))
        prompt.alert(Prompt.Level.WARNING, "Java版本", "您的Java可能無法跟此程式相容,請使用64位元的Java");
    // load config
    BotConfig config = new BotConfig(prompt);
    config.load();
    if (!config.isValid())
        return;
    // set up the listener
    EventWaiter waiter = new EventWaiter();
    SettingsManager settings = new SettingsManager();
    Bot bot = new Bot(waiter, config, settings);
    AboutCommand aboutCommand = new AboutCommand(Color.BLUE.brighter(), "一個 [可以自己運行的音樂機器人](https://github.com/wolf-yuan-6115/flowermoon) (" + version + ")", new String[] { "高品質的音樂", "FairQueue™ 技術" }, RECOMMENDED_PERMS);
    aboutCommand.setIsAuthor(false);
    // 🎶
    aboutCommand.setReplacementCharacter("\uD83C\uDFB6");
    // set up the command client
    CommandClientBuilder cb = new CommandClientBuilder().setPrefix(config.getPrefix()).setAlternativePrefix(config.getAltPrefix()).setOwnerId(Long.toString(config.getOwnerId())).setEmojis(config.getSuccess(), config.getWarning(), config.getError()).setHelpWord(config.getHelp()).setLinkedCacheSize(200).setGuildSettingsManager(settings).addCommands(aboutCommand, new PingCommand(), new SettingsCmd(bot), new LyricsCmd(bot), new NowplayingCmd(bot), new PlayCmd(bot), new PlaylistsCmd(bot), new QueueCmd(bot), new RemoveCmd(bot), new SearchCmd(bot), new SCSearchCmd(bot), new ShuffleCmd(bot), new SkipCmd(bot), new ForceRemoveCmd(bot), new ForceskipCmd(bot), new MoveTrackCmd(bot), new PauseCmd(bot), new PlaynextCmd(bot), new RepeatCmd(bot), new SkiptoCmd(bot), new StopCmd(bot), new VolumeCmd(bot), new PrefixCmd(bot), new SetdjCmd(bot), new SettcCmd(bot), new SetvcCmd(bot), new AutoplaylistCmd(bot), new DebugCmd(bot), new PlaylistCmd(bot), new SetavatarCmd(bot), new SetgameCmd(bot), new SetnameCmd(bot), new SetstatusCmd(bot), new ShutdownCmd(bot));
    if (config.useEval())
        cb.addCommand(new EvalCmd(bot));
    boolean nogame = false;
    if (config.getStatus() != OnlineStatus.UNKNOWN)
        cb.setStatus(config.getStatus());
    if (config.getGame() == null)
        cb.useDefaultGame();
    else if (config.getGame().getName().equalsIgnoreCase("none")) {
        cb.setActivity(null);
        nogame = true;
    } else
        cb.setActivity(config.getGame());
    if (!prompt.isNoGUI()) {
        try {
            GUI gui = new GUI(bot);
            bot.setGUI(gui);
            gui.init();
        } catch (Exception e) {
            log.error("無法開啟視窗,如果您的伺服器或電腦沒有螢幕,請使用 -Dnogui=true 來啟動機器人");
        }
    }
    log.info("成功從 " + config.getConfigLocation() + " 讀取配置");
    // attempt to log in and start
    try {
        JDA jda = JDABuilder.create(config.getToken(), Arrays.asList(INTENTS)).enableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE).disableCache(CacheFlag.ACTIVITY, CacheFlag.CLIENT_STATUS, CacheFlag.EMOTE, CacheFlag.ONLINE_STATUS).setActivity(nogame ? null : Activity.playing("載入中...")).setStatus(config.getStatus() == OnlineStatus.INVISIBLE || config.getStatus() == OnlineStatus.OFFLINE ? OnlineStatus.INVISIBLE : OnlineStatus.DO_NOT_DISTURB).addEventListeners(cb.build(), waiter, new Listener(bot)).setBulkDeleteSplittingEnabled(true).build();
        bot.setJDA(jda);
    } catch (LoginException ex) {
        prompt.alert(Prompt.Level.ERROR, "機器人", ex + "\n請確定您更改了正確的配置文件,並且放置了正確的機器人Token (不是secret)" + "\n配置文件路徑: " + config.getConfigLocation());
        System.exit(1);
    } catch (IllegalArgumentException ex) {
        prompt.alert(Prompt.Level.ERROR, "機器人", "部分配置文件語法錯誤: " + ex + "\n配置文件路徑: " + config.getConfigLocation());
        System.exit(1);
    }
}
Also used : GUI(com.jagrosh.jmusicbot.gui.GUI) CommandClientBuilder(com.jagrosh.jdautilities.command.CommandClientBuilder) LoginException(javax.security.auth.login.LoginException) Logger(org.slf4j.Logger) SettingsManager(com.jagrosh.jmusicbot.settings.SettingsManager) EventWaiter(com.jagrosh.jdautilities.commons.waiter.EventWaiter) LoginException(javax.security.auth.login.LoginException) Prompt(com.jagrosh.jmusicbot.entities.Prompt)

Example 8 with EventWaiter

use of com.jagrosh.jdautilities.commons.waiter.EventWaiter in project clancy by brendonmiranda.

the class PlayCmd method command.

@Override
public void command(SlashCommandEvent event) {
    OptionMapping option = event.getOption(MUSIC_ARG);
    String args = option.getAsString();
    logger.debug("PlayCmd loading track: {}", args);
    AudioPlayer audioPlayer = audioPlayerManager.createPlayer();
    audioPlayer.addListener(audioEventListener);
    Consumer<Message> success = (message) -> {
        Guild guild = event.getGuild();
        AudioManager audioManager = guild.getAudioManager();
        PlayResultHandler playResultHandler = new PlayResultHandler(audioPlayer, guild, audioManager, event, audioPlayerManager, eventWaiter, message, false, audioQueueService);
        audioPlayerManager.loadItemOrdered(event.getGuild(), args, playResultHandler);
    };
    event.replyEmbeds(MessageUtil.buildMessage("Searching...")).queue(interactionHook -> {
        interactionHook.retrieveOriginal().queue(success);
    });
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) Message(net.dv8tion.jda.api.entities.Message) OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) MessageUtil(io.github.brendonmiranda.bot.clancy.util.MessageUtil) Logger(org.slf4j.Logger) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) AudioManager(net.dv8tion.jda.api.managers.AudioManager) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) EventWaiter(com.jagrosh.jdautilities.commons.waiter.EventWaiter) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) SlashCommandEvent(net.dv8tion.jda.api.events.interaction.SlashCommandEvent) Consumer(java.util.function.Consumer) AudioQueueService(io.github.brendonmiranda.bot.clancy.service.AudioQueueService) Component(org.springframework.stereotype.Component) PlayResultHandler(io.github.brendonmiranda.bot.clancy.listener.PlayResultHandler) Guild(net.dv8tion.jda.api.entities.Guild) OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) AudioEventListener(io.github.brendonmiranda.bot.clancy.listener.AudioEventListener) Collections(java.util.Collections) AudioManager(net.dv8tion.jda.api.managers.AudioManager) Message(net.dv8tion.jda.api.entities.Message) PlayResultHandler(io.github.brendonmiranda.bot.clancy.listener.PlayResultHandler) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) Guild(net.dv8tion.jda.api.entities.Guild)

Example 9 with EventWaiter

use of com.jagrosh.jdautilities.commons.waiter.EventWaiter in project RtaB6 by Telnaior.

the class RaceToABillionBot method main.

/**
 * Load the JDA and log in to the bot's account, then pass to connectToChannels().
 *
 * @param args - ignored!
 * @throws IOException - if config.txt doesn't exist or cannot be read
 * @throws LoginException - if we can't log in to the bot's account
 * @throws InterruptedException - if the bot can't sleep
 */
public static void main(String[] args) throws IOException, LoginException, InterruptedException {
    // Read initial config file to get important things
    List<String> list = Files.readAllLines(Paths.get("config.txt"));
    String token = list.get(0);
    String owner = list.get(1);
    // Set up JDA Utilities with the command list
    CommandClientBuilder utilities = new CommandClientBuilder();
    utilities.setOwnerId(owner);
    utilities.setPrefix("!");
    utilities.setHelpWord("commands");
    utilities.addCommands(// Basic Game Commands
    new JoinCommand(), new QuitCommand(), new PeekCommand(), // Hidden Commands
    new FoldCommand(), new RepelCommand(), new BlammoCommand(), new DefuseCommand(), new WagerCommand(), new BonusCommand(), new TruesightCommand(), new FailsafeCommand(), new MinesweeperCommand(), // Minigame Commands
    new EnhanceCommand(), new SkipCommand(), new TestMinigameCommand(), // Info Commands
    new PlayersCommand(), new BoardCommand(), new TotalsCommand(), new NextCommand(), new RankCommand(), new TopCommand(), new LivesCommand(), new StatsCommand(), new AnnuitiesCommand(), new HistoryCommand(), new LevelCommand(), new ListAchievementsCommand(), new HiddenCommandCommand(), // Side Mode Commands
    new ReadyCommand(), // Mod Commands
    new StartCommand(), new ResetCommand(), new SaveCommand(), new ViewBombsCommand(), new GridListCommand(), // Channel Management Commands
    new GameChannelAddCommand(), new GameChannelEnableCommand(), new GameChannelDisableCommand(), new GameChannelModifyCommand(), new ListGameChannelsCommand(), new ResetSeasonCommand(), new ArchiveSeasonCommand(), new AddBotCommand(), new DemoCommand(), // Owner Commands
    new ReconnectCommand(), new ShutdownCommand(), new SendMessagesCommand(), new RecalcLevelCommand(), new ListGuildsCommand(), new LeaveGuildCommand(), // Misc Commands
    new PingCommand(), new HelpCommand(), // Joke Commands
    new MemeCommand(), new LuckyNumberCommand(), new MysteryChanceCommand(), new TriforceCommand());
    // Set up the JDA itself
    JDABuilder prepareBot = JDABuilder.createDefault(token);
    commands = utilities.build();
    waiter = new EventWaiter(Executors.newSingleThreadScheduledExecutor(new EventWaiterThreadFactory()), true);
    // This order is actually important lol
    prepareBot.addEventListeners(waiter, commands);
    betterBot = prepareBot.build();
    // Once the bot is ready, move on to setting up game controllers
    betterBot.awaitReady();
    betterBot.getPresence().setActivity(Activity.playing("Type !help"));
    scanGuilds();
}
Also used : CommandClientBuilder(com.jagrosh.jdautilities.command.CommandClientBuilder) JDABuilder(net.dv8tion.jda.api.JDABuilder) EventWaiter(com.jagrosh.jdautilities.commons.waiter.EventWaiter)

Aggregations

EventWaiter (com.jagrosh.jdautilities.commons.waiter.EventWaiter)9 CommandClientBuilder (com.jagrosh.jdautilities.command.CommandClientBuilder)5 LoginException (javax.security.auth.login.LoginException)4 Prompt (com.jagrosh.jmusicbot.entities.Prompt)2 GUI (com.jagrosh.jmusicbot.gui.GUI)2 SettingsManager (com.jagrosh.jmusicbot.settings.SettingsManager)2 IOException (java.io.IOException)2 Guild (net.dv8tion.jda.api.entities.Guild)2 Logger (org.slf4j.Logger)2 HttpUtil (site.purrbot.bot.util.HttpUtil)2 Command (com.jagrosh.jdautilities.command.Command)1 SlashCommand (com.jagrosh.jdautilities.command.SlashCommand)1 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)1 AudioPlayerManager (com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager)1 AudioEventListener (io.github.brendonmiranda.bot.clancy.listener.AudioEventListener)1 PlayResultHandler (io.github.brendonmiranda.bot.clancy.listener.PlayResultHandler)1 AudioQueueService (io.github.brendonmiranda.bot.clancy.service.AudioQueueService)1 MessageUtil (io.github.brendonmiranda.bot.clancy.util.MessageUtil)1 FileInputStream (java.io.FileInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1