Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix discord permissions #9

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Bot {

public Bot(Config config) throws IOException {
this.config = config;
this.permissionUtils = new PermissionUtils(config);

String token = config.getString("discord.token", "");
if (token.isEmpty()) throw new IOException("discord token missing");
Expand All @@ -44,8 +45,6 @@ public Bot(Config config) throws IOException {
this.jda = builder.build();

addCommands();

this.permissionUtils = new PermissionUtils(config);
}

private void applySettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ public AddListener(Config config, PermissionUtils permissionUtils) {

@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
Member eventMember = event.getMember();
if (eventMember == null) return;

if (permissionUtils.hasPermission(eventMember)) {
if (!permissionUtils.hasPermission(event)) {
String content = config
.getString(NO_PERMISSION, "You don't have permission to use it");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public ListListener(Config config, PermissionUtils permissionUtils) {

@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
Member eventMember = event.getMember();
if (eventMember == null) return;

if (permissionUtils.hasPermission(eventMember)) {
if (!permissionUtils.hasPermission(event)) {
String content = config
.getString(NO_PERMISSION, "You don't have permission to use it");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public RemoveByDiscordIdListener(Config config, PermissionUtils permissionUtils)

@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
Member member = event.getMember();
if (member == null) return;

if (permissionUtils.hasPermission(member)) {
if (!permissionUtils.hasPermission(event)) {
String content = config
.getString(NO_PERMISSION, "You don't have permission to use it");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public RemoveByNameListener(Config config, PermissionUtils permissionUtils) {

@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
Member eventMember = event.getMember();
if (eventMember == null) return;

if (permissionUtils.hasPermission(eventMember)) {
if (!permissionUtils.hasPermission(event)) {
String content = config
.getString(NO_PERMISSION, "You don't have permission to use it");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public SyncListener(Config config, PermissionUtils permissionUtils) {

@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
Member eventMember = event.getMember();
if (eventMember == null) return;

if (permissionUtils.hasPermission(eventMember)) {
if (!permissionUtils.hasPermission(event)) {
String content = config
.getString(NO_PERMISSION, "You don't have permission to use it");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.orewaee.discordauth.common.config.Config;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;

import java.util.List;

Expand All @@ -15,18 +16,19 @@ public PermissionUtils(Config config) {
this.config = config;
}

public boolean hasPermission(Member member) {
public boolean hasPermission(SlashCommandInteractionEvent event) {
List<String> ids = config.getList(DISCORD_IDS, List.of());

String userId = member.getUser().getId();
String userId = event.getUser().getId();
if (ids.contains(userId)) return true;

for (Role role : member.getRoles()) {
String roleId = role.getId();
Member eventMember = event.getMember();
if (eventMember != null)
for (Role role : eventMember.getRoles()) {
String roleId = role.getId();

if (ids.contains(roleId))
return true;
}
if (ids.contains(roleId)) return true;
}

return false;
}
Expand Down

This file was deleted.