Skip to content

Commit ee25322

Browse files
committed
Readd team and voting commands and fix the placeholders in the language files so they match the project fluent system
1 parent 3b39195 commit ee25322

35 files changed

Lines changed: 986 additions & 888 deletions

backend/src/main/java/de/chojo/pluginjam/bot/Bot.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
import com.google.inject.Guice;
1010
import de.chojo.pluginjam.bot.commands.CommandContextProvider;
11+
import de.chojo.pluginjam.bot.listener.InviteButtonListener;
1112
import de.chojo.pluginjam.database.repository.SettingsRepository;
1213
import de.chojo.pluginjam.service.JamService;
1314
import de.chojo.pluginjam.service.SettingsService;
1415
import de.chojo.pluginjam.service.TeamService;
16+
import de.chojo.pluginjam.service.VoteService;
1517
import io.github.kaktushose.jdac.JDACommands;
1618
import io.github.kaktushose.jdac.annotations.interactions.CommandConfig;
1719
import io.github.kaktushose.jdac.annotations.interactions.CommandScope;
@@ -42,14 +44,16 @@ public class Bot implements ApplicationEventListener<ApplicationStartupEvent> {
4244
private final JamService jamService;
4345
private final SettingsService settingsService;
4446
private final TeamService teamService;
47+
private final VoteService voteService;
4548
private final String token;
4649

4750
private ShardManager shardManager;
4851

49-
public Bot(JamService jamService, SettingsService settingsService, TeamService teamService, @Value("${bot.token}") String token) {
52+
public Bot(JamService jamService, SettingsService settingsService, TeamService teamService, VoteService voteService, @Value("${bot.token}") String token) {
5053
this.jamService = jamService;
5154
this.settingsService = settingsService;
5255
this.teamService = teamService;
56+
this.voteService = voteService;
5357
this.token = token;
5458
}
5559

@@ -85,7 +89,8 @@ private void buildCommands() {
8589
var userContextProvider = new CommandContextProvider(
8690
jamService,
8791
settingsService,
88-
teamService
92+
teamService,
93+
voteService
8994
);
9095
var injector = Guice.createInjector(new BotModule(userContextProvider));
9196
JDACommands.builder(shardManager)
@@ -102,7 +107,7 @@ private void buildCommands() {
102107
}
103108
})
104109
.start();
105-
//shardManager.addEventListener(new InviteButtonListener(guilds));
110+
shardManager.addEventListener(new InviteButtonListener(teamService, settingsService, jamService));
106111
}
107112

108113
private void initBot() {

backend/src/main/java/de/chojo/pluginjam/bot/commands/CommandContextProvider.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
import de.chojo.pluginjam.service.JamService;
44
import de.chojo.pluginjam.service.SettingsService;
55
import de.chojo.pluginjam.service.TeamService;
6+
import de.chojo.pluginjam.service.VoteService;
67

78
public class CommandContextProvider {
89
private final JamService jamService;
910
private final SettingsService settingsService;
1011
private final TeamService teamService;
12+
private final VoteService voteService;
1113

12-
public CommandContextProvider(JamService jamService, SettingsService settingsService, TeamService teamService) {
14+
public CommandContextProvider(JamService jamService, SettingsService settingsService, TeamService teamService, VoteService voteService) {
1315
this.jamService = jamService;
1416
this.settingsService = settingsService;
1517
this.teamService = teamService;
18+
this.voteService = voteService;
1619
}
1720

1821
public JamService pluginJamService() {
@@ -26,4 +29,8 @@ public SettingsService settingsService() {
2629
public TeamService teamService() {
2730
return teamService;
2831
}
32+
33+
public VoteService voteService() {
34+
return voteService;
35+
}
2936
}

backend/src/main/java/de/chojo/pluginjam/bot/commands/team/TeamCreateCommand.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ public void onCommand(CommandEvent event, @Param("name") String teamName) {
102102
.getIdLong(), Collections.emptySet(), EnumSet.of(Permission.VIEW_CHANNEL))
103103
.complete();
104104

105-
//meta.token(Token.generate(40));
106-
107105
var team = commandContextProvider.teamService().createTeam(
108106
jam.id(),
109107
teamName,
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* SPDX-License-Identifier: AGPL-3.0-only
3+
*
4+
* Copyright (C) 2022 DevCord Team and Contributor
5+
*/
6+
7+
package de.chojo.pluginjam.bot.commands.team;
8+
9+
import com.google.inject.Inject;
10+
import de.chojo.pluginjam.bot.commands.CommandContextProvider;
11+
import io.github.kaktushose.jdac.annotations.i18n.Bundle;
12+
import io.github.kaktushose.jdac.annotations.interactions.Command;
13+
import io.github.kaktushose.jdac.annotations.interactions.Interaction;
14+
import io.github.kaktushose.jdac.annotations.interactions.Param;
15+
import io.github.kaktushose.jdac.dispatching.events.interactions.CommandEvent;
16+
import io.github.kaktushose.jdac.message.placeholder.Entry;
17+
import io.github.kaktushose.jdac.message.resolver.MessageResolver;
18+
import net.dv8tion.jda.api.EmbedBuilder;
19+
import net.dv8tion.jda.api.components.Component;
20+
import net.dv8tion.jda.api.components.actionrow.ActionRow;
21+
import net.dv8tion.jda.api.components.buttons.Button;
22+
import net.dv8tion.jda.api.components.container.Container;
23+
import net.dv8tion.jda.api.components.textdisplay.TextDisplay;
24+
import net.dv8tion.jda.api.entities.User;
25+
import net.dv8tion.jda.api.interactions.commands.OptionType;
26+
27+
@Bundle("locale")
28+
@Interaction
29+
public final class TeamInviteCommand {
30+
private final CommandContextProvider commandContextProvider;
31+
private final MessageResolver messageResolver;
32+
33+
@Inject
34+
public TeamInviteCommand(CommandContextProvider commandContextProvider, MessageResolver messageResolver) {
35+
this.commandContextProvider = commandContextProvider;
36+
this.messageResolver = messageResolver;
37+
}
38+
39+
@Command(value = "team invite")
40+
public void onCommand(CommandEvent event, @Param(value = "user", type = OptionType.USER) User user) {
41+
var guildId = event.getGuild().getIdLong();
42+
var optJam = commandContextProvider.pluginJamService().getCurrentOrUpcoming(guildId);
43+
44+
if (optJam.isEmpty()) {
45+
event.with().ephemeral(true).reply("error-nojamactive");
46+
return;
47+
}
48+
var jam = optJam.get();
49+
50+
if (jam.state().voting()) {
51+
event.with().ephemeral(true).reply("error-votingactive");
52+
return;
53+
}
54+
55+
var optTeam = commandContextProvider.teamService().getUserTeam(event.getUser().getIdLong());
56+
57+
if (optTeam.isEmpty()) {
58+
event.with().ephemeral(true).reply("error-noteam");
59+
return;
60+
}
61+
62+
var team = optTeam.get();
63+
64+
if (!team.isLeader(event.getUser())) {
65+
event.with().ephemeral(true).reply("command-team-invite-message-noleader");
66+
return;
67+
}
68+
69+
var members = team.members();
70+
var settings = commandContextProvider.settingsService().getSettings(guildId);
71+
72+
if (members.size() >= settings.getTeamSize()) {
73+
event.with().ephemeral(true).reply("error-maxteamsize");
74+
return;
75+
}
76+
77+
if (jam.registrations().stream().noneMatch(r -> r.userId().equals(user.getIdLong()))) {
78+
event.with().ephemeral(true).reply("command-team-invite-message-notRegistered");
79+
return;
80+
}
81+
82+
var currTeam = commandContextProvider.teamService().getUserTeam(user.getIdLong());
83+
84+
if (currTeam.isPresent()) {
85+
event.reply("command-team-invite-message-partofteam");
86+
return;
87+
}
88+
89+
var teamId = team.id();
90+
var buttonId = "invite-accept:" + guildId + ":" + teamId + ":" + user.getIdLong();
91+
92+
var components = Container.of(
93+
TextDisplay.of(messageResolver.resolve("command-team-invite-message-invitation", event.getGuildLocale(),
94+
Entry.entry("USER", user.getAsMention()),
95+
Entry.entry("TEAM", team.meta().getTeamName()))
96+
),
97+
ActionRow.of(Button.success(buttonId, messageResolver.resolve("command-team-invite-message-accept", event.getGuildLocale())))
98+
);
99+
100+
var embed = new EmbedBuilder()
101+
.setTitle("You have been invited to join a team on " + event.getGuild().getName())
102+
.setDescription(event.getUser().getAsMention() + " has invited you to join team **" + team.meta().getTeamName() + "**")
103+
.build();
104+
105+
user.openPrivateChannel().queue(channel ->
106+
channel.sendMessageComponents(components).useComponentsV2().queue()
107+
);
108+
109+
event.with().ephemeral(true).reply("command-team-invite-message-send");
110+
}
111+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* SPDX-License-Identifier: AGPL-3.0-only
3+
*
4+
* Copyright (C) 2022 DevCord Team and Contributor
5+
*/
6+
7+
package de.chojo.pluginjam.bot.commands.team;
8+
9+
import com.google.inject.Inject;
10+
import de.chojo.pluginjam.bot.commands.CommandContextProvider;
11+
import io.github.kaktushose.jdac.annotations.i18n.Bundle;
12+
import io.github.kaktushose.jdac.annotations.interactions.Command;
13+
import io.github.kaktushose.jdac.annotations.interactions.Interaction;
14+
import io.github.kaktushose.jdac.dispatching.events.interactions.CommandEvent;
15+
16+
@Bundle("locale")
17+
@Interaction
18+
public final class TeamLeaveCommand {
19+
private final CommandContextProvider commandContextProvider;
20+
21+
@Inject
22+
public TeamLeaveCommand(CommandContextProvider commandContextProvider) {
23+
this.commandContextProvider = commandContextProvider;
24+
}
25+
26+
@Command(value = "team leave")
27+
public void onCommand(CommandEvent event) {
28+
var guildId = event.getGuild().getIdLong();
29+
var optJam = commandContextProvider.pluginJamService().getCurrentOrUpcoming(guildId);
30+
31+
if (optJam.isEmpty()) {
32+
event.with().ephemeral(true).reply("error-nojamactive");
33+
return;
34+
}
35+
var jam = optJam.get();
36+
37+
if (jam.state().voting()) {
38+
event.with().ephemeral(true).reply("error-votingactive");
39+
return;
40+
}
41+
42+
var teamOpt = commandContextProvider.teamService().getUserTeam(event.getMember().getIdLong());
43+
teamOpt.ifPresentOrElse((team) -> {
44+
if (team.isLeader(event.getUser())) {
45+
event.with().ephemeral(true).reply("command-team-leave-message-leaderleave");
46+
return;
47+
}
48+
49+
commandContextProvider.teamService().leaveTeam(team, event.getMember(), event.getGuild());
50+
event.with().ephemeral(true).reply("command-team-leave-left");
51+
event.getGuild().getTextChannelById(team.meta().getTextChannelId()).sendMessage("command-team-leave-message-left").queue();
52+
}, () -> {
53+
event.with().ephemeral(true).reply("error-noteam");
54+
});
55+
}
56+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* SPDX-License-Identifier: AGPL-3.0-only
3+
*
4+
* Copyright (C) 2022 DevCord Team and Contributor
5+
*/
6+
7+
package de.chojo.pluginjam.bot.commands.team;
8+
9+
import com.google.inject.Inject;
10+
import de.chojo.pluginjam.bot.commands.CommandContextProvider;
11+
import de.chojo.pluginjam.bot.message.DefaultValues;
12+
import de.chojo.pluginjam.bot.util.MentionUtil;
13+
import de.chojo.pluginjam.database.entity.team.Team;
14+
import io.github.kaktushose.jdac.annotations.i18n.Bundle;
15+
import io.github.kaktushose.jdac.annotations.interactions.Button;
16+
import io.github.kaktushose.jdac.annotations.interactions.Command;
17+
import io.github.kaktushose.jdac.annotations.interactions.Interaction;
18+
import io.github.kaktushose.jdac.dispatching.events.interactions.CommandEvent;
19+
import io.github.kaktushose.jdac.dispatching.events.interactions.ComponentEvent;
20+
import io.github.kaktushose.jdac.dispatching.reply.Component;
21+
import io.github.kaktushose.jdac.message.resolver.MessageResolver;
22+
import net.dv8tion.jda.api.components.actionrow.ActionRow;
23+
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
24+
import net.dv8tion.jda.api.components.container.Container;
25+
import net.dv8tion.jda.api.components.container.ContainerChildComponent;
26+
import net.dv8tion.jda.api.components.textdisplay.TextDisplay;
27+
import net.dv8tion.jda.api.interactions.DiscordLocale;
28+
29+
import java.util.List;
30+
import java.util.UUID;
31+
import java.util.stream.Collectors;
32+
33+
@Bundle("locale")
34+
@Interaction
35+
public class TeamListCommand {
36+
private final CommandContextProvider commandContextProvider;
37+
private final MessageResolver messageResolver;
38+
39+
private List<Team> pageEntries;
40+
private int currentPage;
41+
42+
@Inject
43+
public TeamListCommand(CommandContextProvider commandContextProvider, MessageResolver messageResolver) {
44+
this.commandContextProvider = commandContextProvider;
45+
this.messageResolver = messageResolver;
46+
}
47+
48+
@Command(value = "team list")
49+
public void onCommand(CommandEvent event) {
50+
var guildId = event.getGuild().getIdLong();
51+
var optJam = commandContextProvider.pluginJamService().getCurrentOrUpcoming(guildId);
52+
53+
if (optJam.isEmpty()) {
54+
event.with().ephemeral(true).reply("error-nojamactive");
55+
return;
56+
}
57+
var jam = optJam.get();
58+
59+
pageEntries = commandContextProvider.teamService().getTeamsByJamId(jam.id());
60+
61+
currentPage = 0;
62+
event.with().ephemeral(true).reply(renderPage(0, pageEntries, event.getUserLocale()));
63+
}
64+
65+
private Container renderPage(int page, List<Team> entries, DiscordLocale locale) {
66+
int pageSize = 1;
67+
68+
boolean hasNext = page < (entries.size() - 1) / pageSize;
69+
boolean hasPrev = page > 0;
70+
71+
int start = page * pageSize;
72+
int end = Math.min(start + pageSize, entries.size());
73+
74+
List<ContainerChildComponent> teamContents = entries.subList(start, end).stream().map(team -> {
75+
var membersContent = team.members()
76+
.stream()
77+
.map(teamMember -> String.format("- %s", MentionUtil.user(teamMember.userId())))
78+
.collect(Collectors.joining(" \n"));
79+
80+
var contentString = """
81+
# %s (%s)
82+
### %s:
83+
*%s*
84+
### %s:
85+
*%s*
86+
### %s:
87+
*%s*
88+
""".formatted(
89+
team.meta().getTeamName(),
90+
MentionUtil.role(team.meta().getRoleId()),
91+
messageResolver.resolve("word-description", locale),
92+
DefaultValues.getTeamProjectDescription(team, messageResolver, locale),
93+
messageResolver.resolve("word-project-url", locale),
94+
DefaultValues.getTeamProjectUrl(team, messageResolver, locale),
95+
messageResolver.resolve("word-members", locale),
96+
membersContent);
97+
98+
return TextDisplay.of(contentString);
99+
}).collect(Collectors.toList());
100+
101+
int maxPages = (int) Math.ceil(entries.size() / (double) pageSize);
102+
103+
teamContents.add(
104+
ActionRow.of(
105+
hasPrev ? Component.enabled("onPrev") : Component.disabled("onPrev"),
106+
net.dv8tion.jda.api.components.buttons.Button.of(ButtonStyle.SECONDARY, UUID.randomUUID().toString(), "%d / %d".formatted(currentPage + 1, maxPages), null),
107+
hasNext ? Component.enabled("onNext") : Component.disabled("onNext")
108+
)
109+
);
110+
111+
return Container.of(teamContents);
112+
}
113+
114+
@Button(value = "◀", style = ButtonStyle.SECONDARY)
115+
public void onPrev(ComponentEvent event) {
116+
currentPage--;
117+
event.with().keepComponents(false).ephemeral(true).reply(renderPage(currentPage, pageEntries, event.getUserLocale()));
118+
}
119+
120+
@Button(value = "▶", style = ButtonStyle.SECONDARY)
121+
public void onNext(ComponentEvent event) {
122+
currentPage++;
123+
event.with().keepComponents(false).ephemeral(true).reply(renderPage(currentPage, pageEntries, event.getUserLocale()));
124+
}
125+
}

0 commit comments

Comments
 (0)