1.fun

Discord Bot that reply hello on /hi

Kodari AI
KodariSep 1 at 9:51 am 1
HelloDiscordBot
A Discord bot that listens for the "/hi" command and replies with a friendly "hello" message. Built using the Discord JDA library, the bot initializes with a token from environment variables, registers a slash command handler, and responds instantly to user interactions. The project includes a main class that sets up the JDA instance, a command listener that processes slash commands, and configuration for the bot token. Designed for simplicity and easy deployment, it demonstrates core Discord bot functionality with minimal setup.

I’ll create a minimal Discord.js bot with environment-based credentials and a registered /hi slash command.

Created a Discord.js bot that replies Hello! to /hi.

Files:

  • src/index.js — bot logic and slash-command registration

  • package.json — dependencies and start script

  • .env.example — required environment variables

  • .gitignore

Run it with:

Shell
cp .env.example .env
npm install
npm start

Set DISCORD_TOKEN, CLIENT_ID, and optionally GUILD_ID in .env. Using GUILD_ID makes command registration nearly instant during development.

Kodari can make mistakes. Review important code before using.
Files
src
.gitignore
package.json
Code
3 68
src/index.js
import 'dotenv/config';
import {
  Client,
  Events,
  GatewayIntentBits,
  REST,
  Routes,
  SlashCommandBuilder,
} from 'discord.js';

const { DISCORD_TOKEN, CLIENT_ID, GUILD_ID } = process.env;

if (!DISCORD_TOKEN || !CLIENT_ID) {
  throw new Error('DISCORD_TOKEN and CLIENT_ID must be set in the environment.');
}

const commands = [
  new SlashCommandBuilder()
    .setName('hi')
    .setDescription('Replies with a hello.')
    .toJSON(),
];

const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);
const commandRoute = GUILD_ID
  ? Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID)
  : Routes.applicationCommands(CLIENT_ID);

await rest.put(commandRoute, { body: commands });

const client = new Client({
  intents: [GatewayIntentBits.Guilds],
});

client.once(Events.ClientReady, (readyClient) => {
  console.log(`Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async (interaction) => {
  if (!interaction.isChatInputCommand() || interaction.commandName !== 'hi') {
    return;
  }

  await interaction.reply('Hello!');
});

await client.login(DISCORD_TOKEN);