Skip to content

Commit e8c87d4

Browse files
committed
Simplify permission setting for ticket.
1 parent debe757 commit e8c87d4

File tree

1 file changed

+18
-40
lines changed

1 file changed

+18
-40
lines changed

src/main/java/com/eternalcode/discordapp/ticket/TicketChannelService.java

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public CompletableFuture<Optional<TextChannel>> createTicket(long userId, String
5959
return CompletableFuture.supplyAsync(() -> {
6060
try {
6161
long ticketId = Instant.now().toEpochMilli() + ThreadLocalRandom.current().nextInt(1000, 9999);
62-
TextChannel channel = this.createChannel(userId, category, ticketId);
62+
TextChannel channel = this.createChannel(userId, category);
6363

6464
TicketWrapper ticket = new TicketWrapper(userId, channel.getIdLong(), categoryId);
6565
this.ticketService.saveTicket(ticket).join();
@@ -176,56 +176,34 @@ public MessageEmbed createEmbed(String title, String description) {
176176
return builder.build();
177177
}
178178

179-
private TextChannel createChannel(long userId, TicketConfig.TicketCategoryConfig category, long ticketId) {
179+
private TextChannel createChannel(long userId, TicketConfig.TicketCategoryConfig category) {
180180
Category cat = this.jda.getCategoryById(this.config.categoryId);
181181
if (cat == null) {
182182
throw new IllegalStateException("Category not found: " + this.config.categoryId);
183183
}
184184

185+
Guild guild = cat.getGuild();
185186
User user = this.jda.getUserById(userId);
186187
String userName = user != null ? user.getName() : "user" + userId;
187188
long count = this.ticketService.countTicketsByUser(userId).join();
188189
String channelName = count == 0 ? "ticket-" + userName : "ticket-" + userName + "-" + (count + 1);
189190

190-
TextChannel channel = cat.createTextChannel(channelName)
191-
.setTopic("Ticket " + category.displayName)
192-
.complete();
193-
194-
this.setupPermissions(channel, userId);
195-
return channel;
196-
}
197-
198-
private void setupPermissions(TextChannel channel, long userId) {
199-
Guild guild = channel.getGuild();
200-
201-
channel.getManager()
202-
.putPermissionOverride(guild.getPublicRole(), null, List.of(Permission.VIEW_CHANNEL))
203-
.queue();
204-
205191
Member member = guild.getMemberById(userId);
206-
if (member != null) {
207-
channel.getManager()
208-
.putPermissionOverride(
209-
member,
210-
List.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_SEND, Permission.MESSAGE_HISTORY),
211-
null)
212-
.queue();
213-
}
192+
Role staffRole = this.config.staffRoleId != 0L ? guild.getRoleById(this.config.staffRoleId) : null;
214193

215-
if (this.config.staffRoleId != 0L) {
216-
Role staffRole = guild.getRoleById(this.config.staffRoleId);
217-
if (staffRole != null) {
218-
channel.getManager()
219-
.putPermissionOverride(
220-
staffRole,
221-
List.of(
222-
Permission.VIEW_CHANNEL,
223-
Permission.MESSAGE_SEND,
224-
Permission.MESSAGE_HISTORY,
225-
Permission.MESSAGE_MANAGE),
226-
null)
227-
.queue();
228-
}
229-
}
194+
return cat.createTextChannel(channelName)
195+
.setTopic("Ticket " + category.displayName)
196+
.addPermissionOverride(guild.getPublicRole(), null, List.of(Permission.VIEW_CHANNEL))
197+
.addPermissionOverride(member,
198+
List.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_SEND, Permission.MESSAGE_HISTORY),
199+
null)
200+
.addPermissionOverride(staffRole,
201+
List.of(
202+
Permission.VIEW_CHANNEL,
203+
Permission.MESSAGE_SEND,
204+
Permission.MESSAGE_HISTORY,
205+
Permission.MESSAGE_MANAGE),
206+
null)
207+
.complete();
230208
}
231209
}

0 commit comments

Comments
 (0)