-
Notifications
You must be signed in to change notification settings - Fork 12
Manage commands #25
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
Draft
ILike2WatchMemes
wants to merge
5
commits into
SkyHanniStudios:master
Choose a base branch
from
ILike2WatchMemes:kick-ban-commands
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Manage commands #25
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
src/main/kotlin/at/hannibal2/skyhanni/discord/command/ManageCommands.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package at.hannibal2.skyhanni.discord.command | ||
|
|
||
|
|
||
| import at.hannibal2.skyhanni.discord.BOT | ||
| import at.hannibal2.skyhanni.discord.BIG_X | ||
| import at.hannibal2.skyhanni.discord.Option | ||
| import at.hannibal2.skyhanni.discord.PLEADING_FACE | ||
| import at.hannibal2.skyhanni.discord.SimpleTimeMark | ||
| import at.hannibal2.skyhanni.discord.Utils.embed | ||
| import at.hannibal2.skyhanni.discord.Utils.embedSend | ||
| import at.hannibal2.skyhanni.discord.Utils.getId | ||
| import at.hannibal2.skyhanni.discord.Utils.messageSend | ||
| import at.hannibal2.skyhanni.discord.Utils.reply | ||
| import at.hannibal2.skyhanni.discord.Utils.userError | ||
| import at.hannibal2.skyhanni.discord.command.ManageCommands.createModerationEmbed | ||
| import at.hannibal2.skyhanni.discord.command.ManageCommands.handleError | ||
| import at.hannibal2.skyhanni.discord.command.ManageCommands.sendDM | ||
| import at.hannibal2.skyhanni.discord.command.PullRequestCommand.passedSince | ||
| import net.dv8tion.jda.api.EmbedBuilder | ||
| import net.dv8tion.jda.api.Permission | ||
| import net.dv8tion.jda.api.entities.Member | ||
| import net.dv8tion.jda.api.entities.MessageEmbed | ||
| import net.dv8tion.jda.api.entities.User | ||
| import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel | ||
| import net.dv8tion.jda.api.events.message.MessageReceivedEvent | ||
| import net.dv8tion.jda.api.exceptions.ErrorResponseException | ||
| import net.dv8tion.jda.api.requests.ErrorResponse | ||
| import java.awt.Color | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| object ManageCommands { | ||
| fun createModerationEmbed( | ||
| action: String, | ||
| target: User, | ||
| moderator: Member, | ||
| reason: String, | ||
| color: Color = Color.RED | ||
| ): MessageEmbed { | ||
| return EmbedBuilder() | ||
| .setAuthor("${target.name} was $action", null, target.avatarUrl) | ||
| .addField("Mention", target.asMention, false) | ||
| .addField("Moderator", moderator.asMention, false) | ||
| .addField("Reason", reason, false) | ||
| .addField("Time", passedSince(SimpleTimeMark.now().toString()), false) | ||
| .setColor(color) | ||
| .build() | ||
| } | ||
|
|
||
| fun handleError(error: Throwable): String { | ||
| return when (error) { | ||
| is ErrorResponseException -> { | ||
| when (error.errorResponse) { | ||
| ErrorResponse.UNKNOWN_MEMBER -> | ||
| "User not in guild!" | ||
|
|
||
| ErrorResponse.UNKNOWN_USER -> | ||
| "User doesn't exist!" | ||
|
|
||
| ErrorResponse.CANNOT_SEND_TO_USER -> | ||
| "Couldn't DM user!" | ||
|
|
||
| else -> "Discord API error: ${error.errorResponse}" | ||
| } | ||
| } | ||
|
|
||
| else -> "${error.message}" | ||
| } | ||
| } | ||
|
|
||
| fun User.sendDM(channel: MessageChannel, action: String, reason: String, color: Color = Color.RED) { | ||
| openPrivateChannel().queue { dm -> | ||
| val embed = embed("You were $action!", "**Reason:** $reason", color) | ||
|
|
||
| dm.sendMessageEmbeds(embed).queue( | ||
| { channel.messageSend("Sent DM to ${name}.") }, | ||
| { error -> channel.messageSend(handleError(error)) } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Suppress("unused") | ||
| class KickCommand : BaseCommand() { | ||
| override val name: String = "kick" | ||
| override val description: String = "Kicks a member from the server." | ||
| override val options: List<Option> = listOf( | ||
| Option("user", "The @ or id of the member you want to kick."), | ||
| Option("reason", "The reason why this member is being kicked.") | ||
| ) | ||
|
|
||
| override fun MessageReceivedEvent.execute(args: List<String>) { | ||
| val mod = member ?: return reply("Internal error getting moderator.") | ||
| if (!mod.hasPermission(Permission.KICK_MEMBERS) && !mod.hasPermission(Permission.ADMINISTRATOR)) | ||
| return reply("No perms $PLEADING_FACE") | ||
|
|
||
| val targetId = args[0].getId() ?: return userError("Invalid user! Did you enter the correct id?") | ||
| val reason = args.drop(1).joinToString(" ") | ||
|
|
||
| guild.retrieveMemberById(targetId).queue( | ||
| { target -> | ||
| val modChannel = guild.getTextChannelById(BOT.config.moderationChannelId) ?: channel | ||
|
|
||
| target.user.sendDM(modChannel, "kicked", reason) | ||
|
|
||
| target.kick().reason(reason).queue { | ||
| modChannel.embedSend(createModerationEmbed("kicked", target.user, mod, reason)) | ||
| } | ||
| }, | ||
| { error -> reply("$BIG_X ${handleError(error)}") } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| @Suppress("unused") | ||
| class BanCommand : BaseCommand() { | ||
| override val name: String = "ban" | ||
| override val description: String = "Bans a member from the server." | ||
| override val options: List<Option> = listOf( | ||
| Option("user", "The @ or id of the member you want to ban."), | ||
| Option("timeframe", "The timeframe (in days, max 7) where messages should be deleted in."), | ||
| Option("reason", "The reason why this member is being banned.") | ||
| ) | ||
|
|
||
| override fun MessageReceivedEvent.execute(args: List<String>) { | ||
| val mod = member ?: return reply("Internal error getting moderator.") | ||
| if (!mod.hasPermission(Permission.BAN_MEMBERS) && !mod.hasPermission(Permission.ADMINISTRATOR)) | ||
| return reply("No perms $PLEADING_FACE") | ||
|
|
||
| val targetId = args[0].getId() ?: return userError("Invalid user! Did you enter the correct id?") | ||
ILike2WatchMemes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val timeframe = args[1].toIntOrNull().takeIf { it != null && it <= 7 } | ||
| ?: return userError("Invalid timeframe! Did you enter a number <= 7?") | ||
| val reason = args.drop(2).joinToString(" ") | ||
|
|
||
| guild.retrieveMemberById(targetId).queue( | ||
| { target -> | ||
| val modChannel = guild.getTextChannelById(BOT.config.moderationChannelId) ?: channel | ||
|
|
||
| target.user.sendDM(modChannel, "banned", reason) | ||
|
|
||
| target.ban(timeframe, TimeUnit.DAYS).reason(reason).queue { | ||
| modChannel.embedSend(createModerationEmbed("banned", target.user, mod, reason)) | ||
| } | ||
| }, | ||
| { error -> reply("$BIG_X ${handleError(error)}") } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| @Suppress("unused") | ||
| class UnbanCommand : BaseCommand() { | ||
| override val name: String = "unban" | ||
| override val description: String = "Unbans a member from the server." | ||
| override val options: List<Option> = listOf( | ||
| Option("user", "The @ or id of the member you want to unban."), | ||
| Option("reason", "The reason why this member is being unbanned.") | ||
| ) | ||
|
|
||
| override fun MessageReceivedEvent.execute(args: List<String>) { | ||
| val mod = member ?: return reply("Internal error getting moderator.") | ||
| if (!mod.hasPermission(Permission.BAN_MEMBERS) && !mod.hasPermission(Permission.ADMINISTRATOR)) | ||
| return reply("No perms $PLEADING_FACE") | ||
|
|
||
| val targetId = args[0].getId() ?: return userError("Invalid user! Did you enter the correct id?") | ||
| val reason = args.drop(1).joinToString(" ") | ||
|
|
||
| jda.retrieveUserById(targetId).queue( | ||
| { target -> | ||
| val modChannel = guild.getTextChannelById(BOT.config.moderationChannelId) ?: channel | ||
|
|
||
| target.sendDM(modChannel, "unbanned", reason) | ||
|
|
||
| guild.unban(target).reason(reason).queue { | ||
| modChannel.embedSend( | ||
| createModerationEmbed("unbanned", target, mod, reason, color = Color.GREEN) | ||
| ) | ||
| } | ||
| }, | ||
| { error -> reply("$BIG_X ${handleError(error)}") } | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.