0

I'm using Discord.js and am using NodeJS v22.14.0

I have a slash command called work which calls a remote PHP script and sends the userID value using POST. When the command is run in Discord I get the following : 'The application did not respond'

If I call the script directly in the browser it returns a successful response with data No errors in the PHP error_log file either

Any help with this would be great, its been stressing me out for days and I know its a stupid issue causing it

Here is the DiscordJS command

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isCommand()) return;

    const { commandName, options, channelId, user } = interaction;

  if (commandName === 'work') {
    const userid = interaction.user.id;
    const response  = await fetch('https://example.com/script.php',
            {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ userid }),
            });
            const result = await response.json();
            if (response.ok && result.success === true) 
            { 
                 return interaction.reply({content: `${result.message}`, ephemeral: true});
            } else {
                 interaction.reply('Failed');
            }
    }
});

If I access the PHP script via the browser it returns data successfully

PHP Script ( trimmed )

<?php
header('Content-Type: application/json');
$data = json_decode(file_get_contents("php://input"));
$userid = $data->userid;
$int = rand(2, 13);
$amount = ($int > 7) ? rand(360, 694) : rand(82, 340);

$earned = "**You earned $$amount**";
$made = "**You made $$amount**";
$work = [
    "You helped an old lady carry her groceries \n $earned",
    "Your boss was shocked to see you at work \n $earned",
    "After a $int hour shift as an Uber driver \n $earned",
    "Your wife pays you to tidy up the garage \n $earned",
    "You spent $int hours delivering food \n $earned",
    "You spot a bargain in a charity shop & sell it on ebay \n $made",
    "Insurance company pays out your claim \n $earned",
    "Made a cool profit trading the stock market \n $made",
    "Your crypto portfolio value shows a nice profit \n $made",
    "You sell your kidney on Craigslist \n $earned",
    "Garage sale was a success  \n $made"
];

$i = rand(0, 10);
$res = $work[$i];
echo json_encode(['success' => true, 'message' => "$res"]);
?>
{"success":true,"message":"Garage sale was a success  \n **You made $282**"}

1 Answer 1

0

discord requires a response within 3 seconds. maybe you have a solution: use interaction.deferReply() to save more time!!

example:

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === 'work') {
        await interaction.deferReply({ ephemeral: true });

        try {
            const userid = interaction.user.id;
            const response = await fetch('https://example.com/script.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ userid }),
            });

            const result = await response.json();
            if (response.ok && result.success) { 
                return interaction.editReply({ content: result.message });
            } else {
                return interaction.editReply('Failed');
            }
        } catch (error) {
            console.error(error);
            return interaction.editReply('error during command execution');
        }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Still exactly the same sadly. Also the script itself is processed in 0.00023317337036133 seconds average so there shouldn't even be a 3 second delay between executing the command and the JSON response
agree, you need to keep the deferReply there, esp. because of going to someplace else like a DB. Remember the 3 seconds is total time for discord, not just you. it could take 3s just to get to you depending on traffic at the moment. As for the rest of your error, I would find out the path it's taking to understand why it's not showing a reply as the answer suggests you do. (e.g. is command name not work, is the reply blank on success, etc)...put in some console logging or use the debugger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.