Search in sources :

Example 6 with Activity

use of net.dv8tion.jda.api.entities.Activity in project Ninbot by Nincodedo.

the class StreamListenerTest method userStartsStreamingNotAlreadyStreamingNoCooldown.

@Test
void userStartsStreamingNotAlreadyStreamingNoCooldown() {
    UserActivityStartEvent userActivityStartEvent = mock(UserActivityStartEvent.class);
    Member member = mock(Member.class);
    Guild guild = mock(Guild.class);
    Activity activity = mock(Activity.class);
    User user = mock(User.class);
    TextChannel textChannel = mock(TextChannel.class);
    MessageAction messageAction = mock(MessageAction.class);
    Role streamingRole = mock(Role.class);
    RichPresence richPresence = mock(RichPresence.class);
    AuditableRestAction auditableRestAction = mock(AuditableRestAction.class);
    Message message = mock(Message.class);
    when(configService.getValuesByName("123", ConfigConstants.STREAMING_ANNOUNCE_USERS)).thenReturn(List.of("123"));
    when(configService.getSingleValueByName("123", ConfigConstants.STREAMING_ANNOUNCE_CHANNEL)).thenReturn(Optional.of("123"));
    when(configService.getSingleValueByName("123", ConfigConstants.STREAMING_ROLE)).thenReturn(Optional.of("123"));
    when(userActivityStartEvent.getMember()).thenReturn(member);
    when(member.getUser()).thenReturn(user);
    when(member.getId()).thenReturn("123");
    when(userActivityStartEvent.getGuild()).thenReturn(guild);
    when(guild.getId()).thenReturn("123");
    when(userActivityStartEvent.getNewActivity()).thenReturn(activity);
    when(activity.getType()).thenReturn(Activity.ActivityType.STREAMING);
    when(userActivityStartEvent.getUser()).thenReturn(user);
    when(user.getName()).thenReturn("Nin");
    when(activity.isRich()).thenReturn(true);
    when(activity.asRichPresence()).thenReturn(richPresence);
    when(activity.getUrl()).thenReturn("https://twitch.tv/nincodedo");
    when(richPresence.getState()).thenReturn("Zeldo Breath of the Wild 2");
    when(guild.getLocale()).thenReturn(Locale.ENGLISH);
    when(guild.getMember(user)).thenReturn(member);
    when(guild.getTextChannelById("123")).thenReturn(textChannel);
    when(textChannel.sendMessage(any(Message.class))).thenReturn(messageAction);
    when(guild.getRoleById("123")).thenReturn(streamingRole);
    when(guild.addRoleToMember(member, streamingRole)).thenReturn(auditableRestAction);
    when(textChannel.getLatestMessageId()).thenReturn("123");
    when(textChannel.retrieveMessageById("123")).thenReturn(auditableRestAction);
    when(auditableRestAction.complete()).thenReturn(message);
    when(message.getContentRaw()).thenReturn("some other stream announcement");
    streamListener.onGenericUserPresence(userActivityStartEvent);
    verify(messageAction, times(1)).queue(any());
    verify(auditableRestAction, times(1)).queue();
}
Also used : Role(net.dv8tion.jda.api.entities.Role) TextChannel(net.dv8tion.jda.api.entities.TextChannel) User(net.dv8tion.jda.api.entities.User) AuditableRestAction(net.dv8tion.jda.api.requests.restaction.AuditableRestAction) Message(net.dv8tion.jda.api.entities.Message) MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction) Activity(net.dv8tion.jda.api.entities.Activity) UserActivityStartEvent(net.dv8tion.jda.api.events.user.UserActivityStartEvent) Guild(net.dv8tion.jda.api.entities.Guild) Member(net.dv8tion.jda.api.entities.Member) RichPresence(net.dv8tion.jda.api.entities.RichPresence) Test(org.junit.jupiter.api.Test)

Example 7 with Activity

use of net.dv8tion.jda.api.entities.Activity in project Ninbot by Nincodedo.

the class StreamListenerTest method userNotStreamingStartsOtherActivity.

@Test
void userNotStreamingStartsOtherActivity() {
    UserActivityStartEvent userActivityStartEvent = mock(UserActivityStartEvent.class);
    Member member = mock(Member.class);
    Guild guild = mock(Guild.class);
    Activity activity = mock(Activity.class);
    MessageAction messageAction = mock(MessageAction.class);
    AuditableRestAction auditableRestAction = mock(AuditableRestAction.class);
    when(configService.getValuesByName("123", ConfigConstants.STREAMING_ANNOUNCE_USERS)).thenReturn(List.of("123"));
    when(userActivityStartEvent.getMember()).thenReturn(member);
    when(member.getId()).thenReturn("123");
    when(userActivityStartEvent.getGuild()).thenReturn(guild);
    when(guild.getId()).thenReturn("123");
    when(userActivityStartEvent.getNewActivity()).thenReturn(activity);
    when(activity.getType()).thenReturn(Activity.ActivityType.PLAYING);
    streamListener.onGenericUserPresence(userActivityStartEvent);
    verify(messageAction, times(0)).queue();
    verify(auditableRestAction, times(0)).queue();
}
Also used : AuditableRestAction(net.dv8tion.jda.api.requests.restaction.AuditableRestAction) MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction) Activity(net.dv8tion.jda.api.entities.Activity) UserActivityStartEvent(net.dv8tion.jda.api.events.user.UserActivityStartEvent) Guild(net.dv8tion.jda.api.entities.Guild) Member(net.dv8tion.jda.api.entities.Member) Test(org.junit.jupiter.api.Test)

Example 8 with Activity

use of net.dv8tion.jda.api.entities.Activity in project map-ads by cerus-mc.

the class DiscordBot method start.

public CompletableFuture<Void> start() {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    this.executorService.execute(() -> {
        try {
            this.jda = JDABuilder.createDefault(this.config.token, Arrays.asList(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGES)).addEventListeners(new MessageDeleteListener(this.storage), new ButtonInteractListener(this.storage, this.config, this.callback)).build().awaitReady();
            Activity.ActivityType type;
            try {
                type = Activity.ActivityType.valueOf(this.config.activityType);
            } catch (final IllegalArgumentException ignored) {
                type = Activity.ActivityType.LISTENING;
            }
            OnlineStatus status;
            try {
                status = OnlineStatus.valueOf(this.config.onlineStatus);
            } catch (final IllegalArgumentException ignored) {
                status = OnlineStatus.ONLINE;
            }
            final Activity activity = Activity.of(type, this.config.activityString);
            this.jda.getPresence().setPresence(status, activity);
            future.complete(null);
        } catch (final InterruptedException | LoginException e) {
            future.completeExceptionally(e);
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ButtonInteractListener(dev.cerus.mapads.discordbot.listener.ButtonInteractListener) MessageDeleteListener(dev.cerus.mapads.discordbot.listener.MessageDeleteListener) OnlineStatus(net.dv8tion.jda.api.OnlineStatus) Activity(net.dv8tion.jda.api.entities.Activity) LoginException(javax.security.auth.login.LoginException)

Example 9 with Activity

use of net.dv8tion.jda.api.entities.Activity in project mc-discord-bridge by Selicre.

the class DiscordBotImpl method onUserUpdateActivities.

@Override
public void onUserUpdateActivities(@Nonnull UserUpdateActivitiesEvent event) {
    String streamLink = null;
    for (Activity activity : event.getMember().getActivities()) {
        if (activity.getType() == Activity.ActivityType.STREAMING && Activity.isValidStreamingUrl(activity.getUrl())) {
            streamLink = activity.getUrl();
            break;
        }
    }
    UUID uuid = playerLookup.getPlayerProfileId(event.getUser());
    if (uuid != null) {
        boolean wasLive = livePlayers.contains(uuid);
        ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid);
        if (streamLink != null && !wasLive) {
            livePlayers.add(uuid);
            if (player != null) {
                MutableText message = new LiteralText("").append(discordUserToMinecraft(event.getUser(), getGuild(), false)).append(" is now streaming to ").append(new LiteralText(streamLink).setStyle(Style.EMPTY.withUnderline(true).withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, streamLink))));
                if (config.hideChatFromStreamers) {
                    message.append(" - Chat has been disabled.");
                }
                broadcastNoMirror(message);
            }
        } else if (streamLink == null && wasLive) {
            livePlayers.remove(uuid);
            if (player != null) {
                broadcastNoMirror(new LiteralText("").append(discordUserToMinecraft(event.getUser(), getGuild(), false)).append(" is no longer streaming. Chat has been reenabled."));
            }
        }
    }
}
Also used : MutableText(net.minecraft.text.MutableText) ClickEvent(net.minecraft.text.ClickEvent) Activity(net.dv8tion.jda.api.entities.Activity) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) UUID(java.util.UUID) LiteralText(net.minecraft.text.LiteralText)

Example 10 with Activity

use of net.dv8tion.jda.api.entities.Activity in project radio by toadless.

the class Radio method switchStatus.

private void switchStatus(JDA jda) {
    ShardManager manager = jda.getShardManager();
    if (manager == null) {
        return;
    }
    List<Activity> status = List.of(Activity.listening(Constants.DEFAULT_BOT_PREFIX + "help"), Activity.playing("music"), Activity.playing("dank tunes"), Activity.competing("best dj"), Activity.listening("music"), Activity.watching("you suck at being a dj"));
    jda.getPresence().setPresence(OnlineStatus.ONLINE, status.get(new Random().nextInt(status.size())));
}
Also used : Random(java.util.Random) Activity(net.dv8tion.jda.api.entities.Activity) ShardManager(net.dv8tion.jda.api.sharding.ShardManager)

Aggregations

Activity (net.dv8tion.jda.api.entities.Activity)10 UserActivityStartEvent (net.dv8tion.jda.api.events.user.UserActivityStartEvent)4 Guild (net.dv8tion.jda.api.entities.Guild)3 Member (net.dv8tion.jda.api.entities.Member)3 CacheFlag (net.dv8tion.jda.api.utils.cache.CacheFlag)3 ArrayList (java.util.ArrayList)2 OnlineStatus (net.dv8tion.jda.api.OnlineStatus)2 AuditableRestAction (net.dv8tion.jda.api.requests.restaction.AuditableRestAction)2 MessageAction (net.dv8tion.jda.api.requests.restaction.MessageAction)2 DefaultShardManagerBuilder (net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder)2 Test (org.junit.jupiter.api.Test)2 Preconditions (com.google.common.base.Preconditions)1 ButtonInteractListener (dev.cerus.mapads.discordbot.listener.ButtonInteractListener)1 MessageDeleteListener (dev.cerus.mapads.discordbot.listener.MessageDeleteListener)1 java.util (java.util)1 Random (java.util.Random)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1