Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Mar 29, 2024
1 parent bda4d50 commit f6765f8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/examples/java/SlashBotExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.IntegrationType;
import net.dv8tion.jda.api.interactions.InteractionContextType;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
Expand Down Expand Up @@ -64,6 +65,8 @@ public static void main(String[] args)
// Simple reply commands
commands.addCommands(
Commands.slash("say", "Makes the bot say what you tell it to")
.setContexts(InteractionContextType.ALL) // Allow the command to be used anywhere (Bot DMs, Guild, Friend DMs, Group DMs)
.setIntegrationTypes(IntegrationType.ALL) // Allow the command to be installed anywhere (Guilds, Users)
.addOption(STRING, "content", "What the bot should say", true) // you can add required options like this too
);

Expand All @@ -89,8 +92,11 @@ public static void main(String[] args)
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event)
{
// Only accept commands from guilds
if (event.getGuild() == null)
// Only accept commands from guilds.
// As this also supports commands installed on user accounts,
// we need to make sure not to use the full guild object (getGuild()),
// as it may not be available (for example, when using /say in a guild the bot isn't in).
if (!event.isFromGuild())
return;
switch (event.getName())
{
Expand Down

0 comments on commit f6765f8

Please sign in to comment.