Skip to content

Commit

Permalink
discordban command + related tags (#50)
Browse files Browse the repository at this point in the history
* Add `discordban` command + related tags.

Tags added: '<DiscordGroupTag.banned_members>' & '<DiscordUserTag.is_banned[<group>]>'.

* Add `<entry[saveName].user>` + meta fixes.

* Fix maximum arguments number.

* Set syntax to be correct and re-fix max args.

* Remove unused import.

* Various fixes + cleanup
  • Loading branch information
BreadcrumbIsTaken committed Jan 4, 2023
1 parent 4904fc8 commit 7a75888
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void onEnable() {
}
try {
// Commands
DenizenCore.commandRegistry.registerCommand(DiscordBanCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordCommandCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordConnectCommand.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.denizenscript.ddiscordbot.commands;

import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.objects.DiscordBotTag;
import com.denizenscript.ddiscordbot.objects.DiscordGroupTag;
import com.denizenscript.ddiscordbot.objects.DiscordUserTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.commands.generator.*;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.UserSnowflake;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import org.bukkit.Bukkit;

import java.util.concurrent.TimeUnit;

public class DiscordBanCommand extends AbstractCommand implements Holdable {

public DiscordBanCommand() {
setName("discordban");
setSyntax("discordban [id:<id>] ({add}/remove) [user:<user>] [group:<group>] (reason:<reason>) (deletion_timeframe:<time>/{0s})");
setRequiredArguments(3, 6);
isProcedural = false;
autoCompile();
}

// <--[command]
// @Name discordban
// @Syntax discordban [id:<id>] ({add}/remove) [user:<user>] [group:<group>] (reason:<reason>) (deletion_timeframe:<time>/{0s})
// @Required 3
// @Maximum 6
// @Short Bans or unbans a member from a group.
// @Plugin dDiscordBot
// @Guide https://guide.denizenscript.com/guides/expanding/ddiscordbot.html
// @Group external
//
// @Description
// Bans or unbans a member from a group.
//
// To ban a user, use the "add" argument. To unban a user, use the "remove" argument.
// The group is required for both "add" and "remove" arguments, but "reason" can only be used with "add".
// Reasons show up in the group's Audit Logs.
//
// The "deletion_timeframe" argument will, if set, delete all messages sent by the user being banned within the timeframe given.
// The timeframe defaults to 0 seconds, which will not delete any messages. The timeframe cannot be greater than 7 days.
// This argument can only be used when adding a ban using the "add" argument, although it is not required.
//
// The command should usually be ~waited for. See <@link language ~waitable>.
//
// @Tags
// <DiscordUserTag.is_banned[<group>]> returns if the user is banned from a certain group.
// <DiscordGroupTag.banned_members> returns a list of all banned members in a group.
//
// @Usage
// # Bans a user with a reason and deletes all messages sent by the user in the past 2 hours.
// - ~discordban id:mybot add user:<[user]> group:<[group]> "reason:Was being mean!" deletion_timeframe:2h
//
// @Usage
// # Bans a user with but does not delete any messages sent and does not have a reason.
// - ~discordban id:mybot add user:<[user]> group:<[group]>
//
// @Usage
// # Unbans a user.
// - ~discordban id:mybot remove user:<[user]> group:<[group]>
// -->

public enum DiscordBanInstruction { ADD, REMOVE }

public static void autoExecute(ScriptEntry scriptEntry,
@ArgPrefixed @ArgName("id") DiscordBotTag bot,
@ArgName("instruction") DiscordBanInstruction instruction,
@ArgPrefixed @ArgName("user") DiscordUserTag user,
@ArgPrefixed @ArgName("group") DiscordGroupTag rawGroup,
@ArgPrefixed @ArgDefaultNull @ArgName("reason") String reason,
@ArgPrefixed @ArgDefaultText("0s") @ArgName("deletion_timeframe") DurationTag deletionTimeframe) {
if (rawGroup != null && rawGroup.bot == null) {
rawGroup = new DiscordGroupTag(bot.bot, rawGroup.guild_id);
}
final DiscordGroupTag group = rawGroup;
Guild guild = group.getGuild();
UserSnowflake userObj = UserSnowflake.fromId(user.user_id);
Runnable runnable = () -> {
try {
switch (instruction) {
case ADD: {
AuditableRestAction<Void> banAction = guild.ban(userObj, deletionTimeframe.getSecondsAsInt(), TimeUnit.SECONDS);
if (reason != null) {
banAction.reason(reason);
}
banAction.queue();
break;
}
case REMOVE: {
guild.unban(userObj).queue();
break;
}
}
}
catch (Exception ex) {
Debug.echoError(scriptEntry, ex);
}
};
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
runnable.run();
scriptEntry.setFinished(true);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ public static void register() {
return list;
});

// <--[tag]
// @attribute <DiscordGroupTag.banned_members>
// @returns ListTag(DiscordUserTag)
// @plugin dDiscordBot
// @description
// Returns a list of all banned users in the group.
// -->
tagProcessor.registerTag(ListTag.class, "banned_members", (attribute, object) -> {
ListTag list = new ListTag();
for (Guild.Ban ban : object.getGuild().retrieveBanList().complete()) {
list.addObject(new DiscordUserTag(object.bot, ban.getUser()));
}
return list;
});

// <--[tag]
// @attribute <DiscordGroupTag.roles>
// @returns ListTag(DiscordRoleTag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.requests.ErrorResponse;

import java.util.List;

Expand Down Expand Up @@ -420,6 +422,28 @@ public static void register() {
}
return list;
});

// <--[tag]
// @attribute <DiscordUserTag.is_banned[<group>]>
// @returns ElementTag(boolean)
// @plugin dDiscordBot
// @description
// Returns whether the user is banned from a certain group.
// -->
tagProcessor.registerTag(ElementTag.class, DiscordGroupTag.class, "is_banned", (attribute, object, group) -> {
UserSnowflake user = UserSnowflake.fromId(object.user_id);
try {
group.getGuild().retrieveBan(user).complete();
}
catch (ErrorResponseException ex) {
if (ex.getErrorResponse() == ErrorResponse.UNKNOWN_BAN) {
return new ElementTag(false);
}
Debug.echoError(ex);
return null;
}
return new ElementTag(true);
});
}

public static ObjectTagProcessor<DiscordUserTag> tagProcessor = new ObjectTagProcessor<>();
Expand Down

0 comments on commit 7a75888

Please sign in to comment.