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

Interactions, Slash-Commands, and Buttons #1501

Merged
merged 207 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 78 commits
Commits
Show all changes
207 commits
Select commit Hold shift + click to select a range
dd2a0d2
Initial pass on slash commands
MinnDevelopment Nov 9, 2020
7a18a51
Implement some new logic to allow callback-free flow
MinnDevelopment Mar 11, 2021
977ed68
Merge remote-tracking branch 'origin/development' into feature/slash-…
MinnDevelopment Mar 11, 2021
ec04370
Remove /messages from execute endpoint
MinnDevelopment Mar 11, 2021
f7b9423
Add overload to make ephemeral acknowledge easier
MinnDevelopment Mar 11, 2021
87ffbd4
Add CommandThread#setEphemeral
MinnDevelopment Mar 11, 2021
c4874bc
Add some convenience getters for options
MinnDevelopment Mar 11, 2021
c417d14
Add timeout and cascading failure for followup messages
MinnDevelopment Mar 11, 2021
5a6d1b2
Only log warning when followup messages were attempted
MinnDevelopment Mar 11, 2021
4c96e16
Add event methods for ListenerAdapter
MinnDevelopment Mar 11, 2021
4c3d260
Fix handling of choices
MinnDevelopment Mar 11, 2021
f720c45
Add JDA#createCommand and JDA#retrieveCommands
MinnDevelopment Mar 11, 2021
e8c8c97
Update copyright
MinnDevelopment Mar 12, 2021
4d82900
Support direct messages
MinnDevelopment Mar 12, 2021
2cbbe49
Support allowed mentions
MinnDevelopment Mar 12, 2021
aa45c5e
Add CommandUpdateAction and deleteCommand method
MinnDevelopment Mar 12, 2021
4e70bae
Files aren't supported for replies btw
MinnDevelopment Mar 12, 2021
213bee5
Webhook messages can use allowed mentions too
MinnDevelopment Mar 12, 2021
75f0bb5
Fix typo
MinnDevelopment Mar 12, 2021
182dec2
Rename CommandThread to CommandHook
MinnDevelopment Mar 12, 2021
a231c64
Rename SubcommandGroup#addOption to SubcommandGroup#addCommand
MinnDevelopment Mar 12, 2021
f4fda47
Add check for interaction type
MinnDevelopment Mar 12, 2021
e030112
Introduce new major parameter interaction_token
MinnDevelopment Mar 12, 2021
dca8215
Add SlashBotExample
MinnDevelopment Mar 13, 2021
410def2
Replace kick with say
MinnDevelopment Mar 13, 2021
0f513c2
Fix addFile in webhook action
MinnDevelopment Mar 13, 2021
edf11f1
Add missing events
MinnDevelopment Mar 13, 2021
2729980
Add missing follow-up overloads
MinnDevelopment Mar 14, 2021
2ca629b
Add CommandEditAction
MinnDevelopment Mar 14, 2021
3340f22
Better event abstractions
MinnDevelopment Mar 15, 2021
c55e9f2
Fix DELETE names and http method
MinnDevelopment Mar 15, 2021
d0d0c36
Fix ApplicationCommandCreateHandler
MinnDevelopment Mar 15, 2021
4c3955f
Rename createCommand to upsertCommand
MinnDevelopment Mar 15, 2021
5a2904a
Add slash commands to readme
MinnDevelopment Mar 16, 2021
8785713
Add rudimentary support for channels
MinnDevelopment Mar 16, 2021
d2f06d2
Add retrieveCommandById
MinnDevelopment Mar 16, 2021
d771f5e
Add AbstractChannel and OptionData#getChannel
MinnDevelopment Mar 19, 2021
5fd2542
Remove unnecessary override
MinnDevelopment Mar 19, 2021
79dd81c
Fix sendMessage overloads not using setEphemeral
MinnDevelopment Mar 19, 2021
7ec2d09
Add edit with attachment overloads
MinnDevelopment Mar 19, 2021
0363076
Improve API for subcommands in SlashCommandEvent
MinnDevelopment Mar 23, 2021
da3bc95
Add CommandData#load and CommandData#loadAll
MinnDevelopment Mar 23, 2021
79dc33e
Fix serialization of choices
MinnDevelopment Mar 23, 2021
638aaa3
Add SlashCommandEvent#getCommandPath
MinnDevelopment Mar 25, 2021
6bc9624
Fix handling of subcommand names
MinnDevelopment Mar 26, 2021
e98aa1f
Consistent naming
MinnDevelopment Mar 26, 2021
14b00bc
Rename OptionData#getChannel to OptionData#getAsChannel
MinnDevelopment Mar 26, 2021
6c3cac9
Update some incorrect routes
MinnDevelopment Mar 28, 2021
05da4cd
Fix OptionType.INTEGER for choices
MinnDevelopment Mar 29, 2021
bcf77d5
Add some getters and reduce code duplication
MinnDevelopment Mar 29, 2021
40f94ae
Add required scopes setters for invite url generation
MinnDevelopment Mar 29, 2021
e3d2bd7
Add check for double-ack on slash commands
MinnDevelopment Mar 30, 2021
6481b26
Make choices use insertion order
MinnDevelopment Mar 31, 2021
b9553aa
Merge branch 'development' into feature/slash-commands
MinnDevelopment Apr 2, 2021
d997e0d
Remove duplicate enum entries
MinnDevelopment Apr 2, 2021
eca3157
Merge remote-tracking branch 'origin/development' into feature/slash-…
MinnDevelopment Apr 6, 2021
0b3468f
Handle array schema errors better
MinnDevelopment Apr 6, 2021
ed6b880
Improve abstraction
MinnDevelopment Apr 6, 2021
038115b
Change gradle version
MinnDevelopment Apr 6, 2021
c3084e3
Improve webhook support
MinnDevelopment Apr 6, 2021
ae57f7e
Update src/main/java/net/dv8tion/jda/api/entities/Webhook.java
MinnDevelopment Apr 6, 2021
f2883ca
Update src/main/java/net/dv8tion/jda/api/entities/Webhook.java
MinnDevelopment Apr 6, 2021
e4c170f
Fix incorrect default implementations
MinnDevelopment Apr 6, 2021
fbc5cfd
Make MessageType.APPLICATION_COMMAND non-system
MinnDevelopment Apr 6, 2021
5b479c8
Merge branch 'development' into feature/slash-commands
MinnDevelopment Apr 7, 2021
02d0ec1
Handle cross-channel referenced
MinnDevelopment Apr 7, 2021
8cccfcc
Remove ack check from complete
MinnDevelopment Apr 8, 2021
5fa02cc
Add many checks
MinnDevelopment Apr 8, 2021
b84adde
Improve some old name checks
MinnDevelopment Apr 8, 2021
8b073dd
Improve checks for channel names
MinnDevelopment Apr 8, 2021
226febb
Rename Stream to Video in Permission.VOICE_STREAM
MinnDevelopment Apr 9, 2021
0c1d446
Add SlashCommandEvent#isAcknowledged
MinnDevelopment Apr 10, 2021
8d511ee
Add some docs and checks for options
MinnDevelopment Apr 10, 2021
1fce752
Merge remote-tracking branch 'origin/development' into feature/slash-…
MinnDevelopment Apr 12, 2021
7c2a506
Use Helpers.codePointLength
MinnDevelopment Apr 12, 2021
4e02960
Fix name pattern for unicode letters
MinnDevelopment Apr 21, 2021
7a66fb3
Refactor some packages for better future structure
MinnDevelopment Apr 26, 2021
5951675
Merge remote-tracking branch 'origin/development' into feature/slash-…
MinnDevelopment Apr 26, 2021
ebc51ed
Add basic button support
MinnDevelopment Apr 27, 2021
67f222f
Add emoji support and ButtonClickEvent
MinnDevelopment Apr 28, 2021
f961196
Fix some issues and handle ephemeral buttons
MinnDevelopment Apr 29, 2021
8c4a496
Properly update application id in createSelfUser
MinnDevelopment Apr 29, 2021
5607dc3
Add ButtonInteraction#getButton and improve some naming
MinnDevelopment May 3, 2021
5db116f
Use deferEdit instead of deferReply
MinnDevelopment May 3, 2021
efeb12a
Move inner classes into proper packages
MinnDevelopment May 3, 2021
6d26bb9
Add support for string coercion in DataObject and DataArray
MinnDevelopment May 3, 2021
5336c29
Add more getters and factories
MinnDevelopment May 3, 2021
c135201
Add some documentation to buttons and emoji
MinnDevelopment May 12, 2021
1ea4cb3
Add checks for lowercase only
MinnDevelopment May 12, 2021
3674e9d
Move InteractionHook to interactions package
MinnDevelopment May 12, 2021
9fa25c8
Use Checks.isLowercase instead
MinnDevelopment May 12, 2021
e50798d
More docs
MinnDevelopment May 12, 2021
28aeb6d
Fix some bugs and add length checks for buttons
MinnDevelopment May 12, 2021
2cc5036
Finish docs for Interaction interface
MinnDevelopment May 12, 2021
8846926
Add some more docs
MinnDevelopment May 13, 2021
f60c8e7
Fix lowercase names
MinnDevelopment May 13, 2021
a02ef93
Add buttons to SlashBotExample
MinnDevelopment May 13, 2021
de13d82
Fix typo higly -> highly
MinnDevelopment May 13, 2021
62178b5
Add docs for event requirements
MinnDevelopment May 15, 2021
66ba947
Add support for OptionType.MENTIONABLE
MinnDevelopment May 15, 2021
d572823
Use editMessageById instead of editOriginal for updateButton
MinnDevelopment May 15, 2021
d3cc143
Change message edit semantics for webhooks and simplify code
MinnDevelopment May 15, 2021
d8772c7
Replace applyMessage with setContent in updateButton
MinnDevelopment May 15, 2021
6e7837d
Remove redundancy
MinnDevelopment May 15, 2021
e5f6ea4
Support only editing buttons of a message
MinnDevelopment May 15, 2021
510d12a
Document behavior of updateButton
MinnDevelopment May 15, 2021
cef1725
Some more docs and renames
MinnDevelopment May 16, 2021
77cb567
Docs for editing messages
MinnDevelopment May 16, 2021
dd0cb3b
Add components to empty check
MinnDevelopment May 16, 2021
986179c
Remove some unnecessary links to editMessage
MinnDevelopment May 16, 2021
02ce358
Support component-only acknowledgements
MinnDevelopment May 16, 2021
0617202
Add docs for ReplyAction and document limitations of ephemeral messages
MinnDevelopment May 16, 2021
db92d7d
Add docs for MessageAction
MinnDevelopment May 16, 2021
efa9400
Make WebhookClient use generics for the message response
MinnDevelopment May 26, 2021
6d2684a
Add components to MessageBuilder
MinnDevelopment May 26, 2021
387d54f
Fix typo
Xirado May 26, 2021
92ed96e
Merge pull request #1612 from DV8FromTheWorld/feature/buttons
MinnDevelopment May 26, 2021
f96d800
Comment out replyComponents for now
MinnDevelopment May 26, 2021
fde34fc
Improve unicode checks
MinnDevelopment May 26, 2021
c114c34
Add convenience overloads for addOption
MinnDevelopment May 26, 2021
f957801
Allow buttons with only emoji
MinnDevelopment May 26, 2021
1785653
Improve copy constructors and fix unicode bug
MinnDevelopment May 27, 2021
bd3fa36
Cleanup
MinnDevelopment May 27, 2021
2868a9f
Fix check in UpdateActionImpl
MinnDevelopment May 27, 2021
8994ab4
Some more cleanup
MinnDevelopment May 27, 2021
725e217
Fix some inconsistencies with addFile and add docs to WebhookMessageU…
MinnDevelopment May 27, 2021
402ee63
Add docs to WebhookMessageAction
MinnDevelopment May 27, 2021
5885efc
Add some missing docs
MinnDevelopment May 27, 2021
e7c3619
Add lots of docs and make addOptions varargs
MinnDevelopment May 28, 2021
1b8f559
Add last missing docs
MinnDevelopment May 28, 2021
bacd237
Move classes into components package instead of buttons package
MinnDevelopment May 28, 2021
c021213
Add CommandPrivilege support
MinnDevelopment May 28, 2021
e4cb64a
Improve interface for Command options
MinnDevelopment May 28, 2021
52b70d2
Remove TODOs
MinnDevelopment May 28, 2021
667dac5
Fix incorrect default for setDefaultEnabled
MinnDevelopment May 28, 2021
5a187e2
Add some useful button setters
MinnDevelopment May 28, 2021
a2ad487
Fix Message#editMessage issues
MinnDevelopment May 28, 2021
0cb61fe
Fix typos
MinnDevelopment May 28, 2021
9c31ef1
Add constructor overload with parameter for isRequired
MinnDevelopment May 28, 2021
572de43
More varargs support
MinnDevelopment May 28, 2021
757de9b
Fix parsing of options in Command
MinnDevelopment May 28, 2021
b821609
Add docs for clearing commands
MinnDevelopment May 29, 2021
edf31d5
Add check for privilege limit
MinnDevelopment May 29, 2021
088ef71
Merge remote-tracking branch 'origin/development' into feature/slash-…
MinnDevelopment May 29, 2021
29f888d
Fix some javadoc errors
MinnDevelopment May 29, 2021
0feb089
Fix Command#delete
MinnDevelopment May 30, 2021
063d8b2
Add some docs for Choice constructors
MinnDevelopment May 30, 2021
421d9ba
Disallow adding subcommands and subcommand groups with addOptions met…
MinnDevelopment May 30, 2021
3846f8c
Support equals/toString and codepoint notation
MinnDevelopment May 30, 2021
b033f23
Add Interaction#isFromGuild
MinnDevelopment May 31, 2021
eca9fe0
Update SlashBotExample
MinnDevelopment May 31, 2021
8b1696b
Add see tags to JDA#upsertCommand
MinnDevelopment May 31, 2021
85a6658
Make SubcommandGroupData not extend OptionData and change factory nam…
MinnDevelopment May 31, 2021
035a9de
Rename Guild#retrieveCommandPrivileges to Guild#retrieveCommandPrivil…
MinnDevelopment May 31, 2021
35d899e
Add varargs version of Guild#updateCommandPrivileges and rename to Gu…
MinnDevelopment May 31, 2021
f787b12
Add nullability note for Interaction#getChannel
MinnDevelopment May 31, 2021
97ab686
Update some documentation for deferReply
MinnDevelopment May 31, 2021
4cae4a9
Add see also section to Command.Choice
MinnDevelopment May 31, 2021
ad46c48
Update comment about getMessage being null
MinnDevelopment May 31, 2021
479cb06
Add some clarification to ButtonStyle
MinnDevelopment May 31, 2021
974b73d
Update docs for ComponentInteraction
MinnDevelopment May 31, 2021
4441d85
Update src/main/java/net/dv8tion/jda/api/requests/restaction/WebhookM…
MinnDevelopment May 31, 2021
ab014e2
Use unordered list for InteractionHook docs
MinnDevelopment May 31, 2021
49a91a1
Update OptionMapping docs and cleanup
MinnDevelopment May 31, 2021
a25cb79
Update setters and checks in CommandData and similar
MinnDevelopment May 31, 2021
6135751
Don't use ordinal in CommandPrivilege
MinnDevelopment May 31, 2021
e2f86f7
Add check for type in ActionRow#fromData
MinnDevelopment May 31, 2021
ebfb046
Add length check in Button#withUrl
MinnDevelopment May 31, 2021
c2bb41f
Rename ButtonInteraction#updateButton to ButtonInteraction#editButton
MinnDevelopment May 31, 2021
ee61cff
Improve docs for ComponentLayout
MinnDevelopment May 31, 2021
4af9cab
Rename CommandUpdateAction to CommandListUpdateAction
MinnDevelopment May 31, 2021
800ab6d
Fix compiler error
MinnDevelopment May 31, 2021
9cf552e
Remove comment in MessageAction
MinnDevelopment May 31, 2021
a0eea4b
Remove Flag enum
MinnDevelopment May 31, 2021
88f59a8
Comment out setAvatarUrl and setUsername as its currently unusable
MinnDevelopment May 31, 2021
57e77ff
Use enum in InteractionCreateHandler
MinnDevelopment May 31, 2021
6224471
Cleanup CommandInteractionImpl
MinnDevelopment May 31, 2021
8f0ff19
Cleanup CommandEditActionImpl
MinnDevelopment May 31, 2021
6919e52
Rename getJSON to toData
MinnDevelopment May 31, 2021
d8f5225
Rename UpdateAction to UpdateInteractionAction
MinnDevelopment May 31, 2021
a065ae2
Add docs for AbstractChannel
MinnDevelopment May 31, 2021
0b4ace9
Add annotations in GenericInteractionCreateEvent
MinnDevelopment May 31, 2021
b454929
Cleanup CommandInteraction
MinnDevelopment May 31, 2021
72b149d
Add note about SelfUser#getApplicationId
MinnDevelopment May 31, 2021
dc395c4
Add docs for AllowedMentions
MinnDevelopment May 31, 2021
1fdcaa5
Revert unrelated voice changes
MinnDevelopment May 31, 2021
3ddf173
Add varargs version of Command#updatePrivileges
MinnDevelopment May 31, 2021
4433d7f
Update docs for application events
MinnDevelopment May 31, 2021
5d59909
Add check to prevent adding required options after non-required options
MinnDevelopment May 31, 2021
000c375
Add equals and hashCode to Command nested classes
MinnDevelopment May 31, 2021
1006961
Add CommandDataTest
MinnDevelopment May 31, 2021
f1d6174
Add missing newline
MinnDevelopment May 31, 2021
af603bf
Support removing attachments from messages
MinnDevelopment May 31, 2021
8e48cd0
Prepare implementation of retainFiles on UpdateInteractionAction
MinnDevelopment May 31, 2021
a20fc88
Properly set the RETAINED_FILES flag
MinnDevelopment May 31, 2021
3def451
Rename CallbackAction to InteractionCallbackAction
MinnDevelopment May 31, 2021
a6c1867
Add some convenience setters
MinnDevelopment Jun 1, 2021
4d20f1b
Add missing docs for guild parameter
MinnDevelopment Jun 1, 2021
f0422f4
Better variable name
MinnDevelopment Jun 1, 2021
6b61796
Improve parsing of CommandPrivilege
MinnDevelopment Jun 1, 2021
19258d0
Add example image and improve examples for Button
MinnDevelopment Jun 1, 2021
fe6dc24
Add linebreak
MinnDevelopment Jun 1, 2021
8eb37b2
Add missing parentheses
MinnDevelopment Jun 1, 2021
05d58f1
Include inputs for failed checks in exception message
MinnDevelopment Jun 1, 2021
3fff6a0
Rename AllowedMentionsUtil to AllowedMentionsImpl
MinnDevelopment Jun 2, 2021
bf71f22
Add support for addFile on callback actions
MinnDevelopment Jun 3, 2021
46da70f
Add missing return types
MinnDevelopment Jun 4, 2021
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
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,40 @@ public class Bot extends ListenerAdapter
}
```

**Slash-Commands**:

```java
public class Bot extends ListenerAdapter
{
public static void main(String[] args) throws LoginException
{
if (args.length < 1) {
System.out.println("You have to provide a token as first argument!");
System.exit(1);
}
// args[0] should be the token
// We don't need any intents for this bot. Slash commands work without any intents!
JDA jda = JDABuilder.createLight(args[0], Collections.emptyList())
.addEventListeners(new Bot())
.setActivity(Activity.playing("Type /ping"))
.build();

jda.upsertCommand("ping", "Calculate ping of the bot").queue(); // This can take up to 1 hour to show up in the client
}

@Override
public void onSlashCommand(SlashCommandEvent event)
{
if (!event.getName().equals("ping")) return; // make sure we handle the right command
long time = System.currentTimeMillis();
event.reply("Pong!").setEphemeral(true) // reply or acknowledge
.flatMap(v ->
event.getHook().editOriginalFormat("Pong: %d ms", System.currentTimeMillis() - time) // then edit original
).queue(); // Queue both reply and edit
}
}
```

### RestAction

Through [RestAction](https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/requests/RestAction.html) we provide request handling with
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
143 changes: 143 additions & 0 deletions src/examples/java/SlashBotExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.CommandHook;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.restaction.CommandUpdateAction;

import javax.security.auth.login.LoginException;
import java.util.EnumSet;

import static net.dv8tion.jda.api.interactions.commands.OptionType.*;

public class SlashBotExample extends ListenerAdapter
{
public static void main(String[] args) throws LoginException
{
JDA jda = JDABuilder.createLight("BOT_TOKEN_HERE", EnumSet.noneOf(GatewayIntent.class)) // slash commands don't need any intents
.addEventListeners(new SlashBotExample())
.build();

// These commands take up to an hour to be activated after creation/update/delete
CommandUpdateAction commands = jda.updateCommands();

// Moderation commands with required options
commands.addCommands(
new CommandData("ban", "Ban a user from this server. Requires permission to ban users.")
.addOption(new OptionData(USER, "user", "The user to ban") // USER type allows to include members of the server or other users by id
.setRequired(true)) // This command requires a parameter
.addOption(new OptionData(INTEGER, "delDays", "Delete messages from the past days.")) // This is optional
);

// Simple reply commands
commands.addCommands(
new CommandData("say", "Makes the bot say what you tell it to")
.addOption(new OptionData(STRING, "content", "What the bot should say")
.setRequired(true))
);

// Commands without any inputs
commands.addCommands(
new CommandData("leave", "Make the bot leave the server")
);

// Send the new set of commands to discord, this will override any existing global commands with the new set provided here
commands.queue();
}


@Override
public void onSlashCommand(SlashCommandEvent event)
{
// Only accept commands from guilds
if (event.getGuild() == null)
return;
switch (event.getName())
{
case "ban":
Member member = event.getOption("user").getAsMember(); // the "user" option is required so it doesn't need a null-check here
User user = event.getOption("user").getAsUser();
ban(event, user, member);
break;
case "say":
say(event, event.getOption("content").getAsString()); // content is required so no null-check here
break;
case "leave":
leave(event);
break;
default:
event.reply("I can't handle that command right now :(").setEphemeral(true).queue();
}
}

public void ban(SlashCommandEvent event, User user, Member member)
{
event.acknowledge(true).queue(); // Let the user know we received the command before doing anything else
CommandHook hook = event.getHook(); // This is a special webhook that allows you to send messages without having permissions in the channel and also allows ephemeral messages
hook.setEphemeral(true); // All messages here will now be ephemeral implicitly
if (!event.getMember().hasPermission(Permission.BAN_MEMBERS))
{
hook.sendMessage("You do not have the required permissions to ban users from this server.").queue();
return;
}

Member selfMember = event.getGuild().getSelfMember();
if (!selfMember.hasPermission(Permission.BAN_MEMBERS))
{
hook.sendMessage("I don't have the required permissions to ban users from this server.").queue();
return;
}

if (member != null && !selfMember.canInteract(member))
{
hook.sendMessage("This user is too powerful for me to ban.").queue();
return;
}

int delDays = 0;
SlashCommandEvent.OptionData option = event.getOption("delDays");
if (option != null) // null = not provided
delDays = (int) Math.max(0, Math.min(7, option.getAsLong()));
// Ban the user and send a success response
event.getGuild().ban(user, delDays)
.flatMap(v -> hook.sendMessage("Banned user " + user.getAsTag()))
.queue();
}

public void say(SlashCommandEvent event, String content)
{
event.reply(content).queue(); // This requires no permissions!
}

public void leave(SlashCommandEvent event)
{
if (!event.getMember().hasPermission(Permission.KICK_MEMBERS))
event.reply("You do not have permissions to kick me.").setEphemeral(true).queue();
else
event.reply("Leaving the server... :wave:") // Yep we received it
.flatMap(v -> event.getGuild().leave()) // Leave server after acknowledging the command
.queue();
}
}
93 changes: 87 additions & 6 deletions src/main/java/net/dv8tion/jda/api/JDA.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import net.dv8tion.jda.annotations.ReplaceWith;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.hooks.IEventManager;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.managers.AudioManager;
import net.dv8tion.jda.api.managers.DirectAudioController;
import net.dv8tion.jda.api.managers.Presence;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import net.dv8tion.jda.api.requests.restaction.GuildAction;
import net.dv8tion.jda.api.requests.restaction.*;
import net.dv8tion.jda.api.sharding.ShardManager;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
Expand All @@ -47,10 +48,8 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -492,6 +491,54 @@ default JDA awaitReady() throws InterruptedException
@Nonnull
List<Object> getRegisteredListeners();

// TODO: Docs
@Nonnull
@CheckReturnValue
RestAction<List<Command>> retrieveCommands();

@Nonnull
@CheckReturnValue
RestAction<Command> retrieveCommandById(@Nonnull String id);

@Nonnull
@CheckReturnValue
default RestAction<Command> retrieveCommandById(long id)
{
return retrieveCommandById(Long.toUnsignedString(id));
}

@Nonnull
@CheckReturnValue
CommandCreateAction upsertCommand(@Nonnull CommandData command);

@Nonnull
@CheckReturnValue
default CommandCreateAction upsertCommand(@Nonnull String name, @Nonnull String description)
{
return upsertCommand(new CommandData(name, description));
}

@Nonnull
@CheckReturnValue
CommandUpdateAction updateCommands();

@Nonnull
@CheckReturnValue
CommandEditAction editCommandById(@Nonnull String id);

@Nonnull
@CheckReturnValue
RestAction<Void> deleteCommandById(@Nonnull String commandId);

@Nonnull
@CheckReturnValue
default RestAction<Void> deleteCommandById(long commandId)
{
return deleteCommandById(Long.toUnsignedString(commandId));
}

// TODO: editing commands

/**
* Constructs a new {@link net.dv8tion.jda.api.entities.Guild Guild} with the specified name
* <br>Use the returned {@link GuildAction GuildAction} to provide
Expand Down Expand Up @@ -672,7 +719,7 @@ default User getUserByTag(@Nonnull String username, @Nonnull String discriminato
Checks.notNull(username, "Username");
Checks.notNull(discriminator, "Discriminator");
Checks.check(discriminator.length() == 4 && Helpers.isNumeric(discriminator), "Invalid format for discriminator!");
int codePointLength = username.codePointCount(0, username.length());
int codePointLength = Helpers.codePointLength(username);
Checks.check(codePointLength >= 2 && codePointLength <= 32, "Username must be between 2 and 32 codepoints in length!");
return getUserCache().applyStream(stream ->
stream.filter(it -> it.getDiscriminator().equals(discriminator))
Expand Down Expand Up @@ -1922,6 +1969,40 @@ default List<Emote> getEmotesByName(@Nonnull String name, boolean ignoreCase)
@CheckReturnValue
RestAction<ApplicationInfo> retrieveApplicationInfo();

/**
* Configures the required scopes applied to the {@link #getInviteUrl(Permission...)} and similar methods.
* <br>To use slash commands you must add {@code "applications.commands"} to these scopes. The scope {@code "bot"} is always applied.
*
* @param scopes
* The scopes to use with {@link #getInviteUrl(Permission...)} and the likes
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The current JDA instance
*/
@Nonnull
default JDA setRequiredScopes(@Nonnull String... scopes)
{
Checks.noneNull(scopes, "Scopes");
return setRequiredScopes(Arrays.asList(scopes));
}

/**
* Configures the required scopes applied to the {@link #getInviteUrl(Permission...)} and similar methods.
* <br>To use slash commands you must add {@code "applications.commands"} to these scopes. The scope {@code "bot"} is always applied.
*
* @param scopes
* The scopes to use with {@link #getInviteUrl(Permission...)} and the likes
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The current JDA instance
*/
@Nonnull
JDA setRequiredScopes(@Nonnull Collection<String> scopes);

/**
* Creates an authorization invite url for the currently logged in Bot-Account.
* <br>Example Format:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/dv8tion/jda/api/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public enum Permission
USE_SLASH_COMMANDS( 31, true, true, "Use Slash Commands"),

// Voice Permissions
VOICE_STREAM( 9, true, true, "Stream"),
VOICE_STREAM( 9, true, true, "Video"),
VOICE_CONNECT( 20, true, true, "Connect"),
VOICE_SPEAK( 21, true, true, "Speak"),
VOICE_MUTE_OTHERS(22, true, true, "Mute Members"),
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/AbstractChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.entities;

import net.dv8tion.jda.api.JDA;

import javax.annotation.Nonnull;

public interface AbstractChannel extends ISnowflake
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* The human readable name of this channel.
* <br>If no name has been set, this returns null.
*
* @return The name of this channel
*/
@Nonnull
String getName();

/**
* The {@link net.dv8tion.jda.api.entities.ChannelType ChannelType} for this channel
*
* @return The channel type
*/
@Nonnull
ChannelType getType();

/**
* Returns the {@link net.dv8tion.jda.api.JDA JDA} instance of this channel
*
* @return the corresponding JDA instance
*/
@Nonnull
JDA getJDA();
}
Loading