Search code examples
javascriptnode.jsdiscorddiscord.js

Expected token to be set for this request, but none was presen ERROR while DMing user


I was trying to make economy system. On end of proccess you shloud recieve DM about somone send you money. But when bot try to DM someone he cant.

Error:

/home/container/node_modules/@discordjs/rest/dist/index.js:787
        throw new Error("Expected token to be set for this request, but none was present");
              ^
Error: Expected token to be set for this request, but none was present
    at RequestManager.resolveRequest (/home/container/node_modules/@discordjs/rest/dist/index.js:787:15)
    at RequestManager.queueRequest (/home/container/node_modules/@discordjs/rest/dist/index.js:759:46)
    at REST.raw (/home/container/node_modules/@discordjs/rest/dist/index.js:914:32)
    at REST.request (/home/container/node_modules/@discordjs/rest/dist/index.js:910:33)
    at REST.post (/home/container/node_modules/@discordjs/rest/dist/index.js:901:17)
    at UserManager.createDM (/home/container/node_modules/discord.js/src/managers/UserManager.js:60:41)
    at UserManager.send (/home/container/node_modules/discord.js/src/managers/UserManager.js:112:24)
    at Query.<anonymous> (/home/container/commands/send.js:100:44)
    at Query.<anonymous> (/home/container/node_modules/mysql/lib/Connection.js:526:10)
    at Query._callback (/home/container/node_modules/mysql/lib/Connection.js:488:16)

My code:

//Importovani discord.js balicku do kodu
const { Client, GatewayIntentBits, Discord, Partials, EmbedBuilder, SlashCommandBuilder, PermissionFlagsBits, message } = require("discord.js");
const client = new Client({
    intents: [GatewayIntentBits.Guilds],
    partials: [Partials.Channel],
});

var db = require("../nastaveni/db.js");

module.exports = {
    //Data comanndu
    name: "send",
    description: "Zašle někomu peníze z tvého účtu.",
    data: new SlashCommandBuilder()
        .setName("send")
        .setDescription("Zašle někomu peníze z tvého účtu.")

        .addStringOption((option) => option.setName("pridat").setDescription("Kolik chceš poslat!").setRequired(true))
        //Kdo to je
        .addStringOption((option) => option.setName("cislouctu").setDescription("Číslo účtu pro poslání").setRequired(true))
        .addStringOption((option) => option.setName("poznamka").setDescription("Poznámka k transakci!"))
        .addStringOption((option) => option.setName("zuctu").setDescription("Pokud posíláte z vedlejšího účtu napište číslo vedlejšího účtu!")),

    //Spusteni scriptu pri interakci
    async execute(interaction) {
        var pridat = parseInt(interaction.options.getString("pridat"));
        const cislouctu = interaction.options.getString("cislouctu");
        const poznamka = interaction.options.getString("poznamka");
        const zuctu = interaction.options.getString("zuctu");
        const trchid = "967327241648373820";
        if (poznamka === null) {
            const poznamka = "*není*";
        }

        if (zuctu === null) {
            db.query("SELECT penize,cislouctu FROM charaktery WHERE discord = " + interaction.user.id, function (err, result) {
                if (err) {
                    console.log(err);
                    interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_1", emphemeral: true });
                    return;
                }
                const zuctu = result[0].cislouctu;
                var castka = parseInt(result[0].penize) - pridat;
                if (castka < 0) {
                    interaction.reply({ content: "Je nám líto ale nemáte dostatek financí pro provedení této transakce!", emphemeral: true });
                    return;
                }
                db.query("UPDATE charaktery SET penize = " + castka + " WHERE discord = " + interaction.user.id, function (err) {
                    if (err) {
                        console.log(err);
                        interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_2", emphemeral: true });
                        return;
                    }
                    db.query("SELECT * FROM charaktery WHERE cislouctu = " + cislouctu, function (err, result) {
                        if (err) {
                            console.log(err);
                            interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_3", emphemeral: true });
                            return;
                        }
                        if (result[0] === undefined) {
                            db.query("SELECT zustatek,discordpristup FROM ucty WHERE cislouctu = " + cislouctu, function (err, result2) {
                                if (err) {
                                    console.log(err);
                                    interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_11", emphemeral: true });
                                    return;
                                }
                                const discordid2 = result2[0].discordpristup;
                                var zustatek2 = parseInt(result2[0].zustatek);
                                var novyzustatek2 = zustatek2 + pridat;
                                db.query("UPDATE ucty SET zustatek = " + novyzustatek2 + " WHERE cislouctu = " + cislouctu, function (err) {
                                    if (err) {
                                        console.log(err);
                                        interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_4", emphemeral: true });
                                        return;
                                    }
                                    const newEmbed = new EmbedBuilder()
                                        .setColor("#4f4f4f")
                                        .setTitle("NOVÁ PLATBA PŘIJATA")
                                        .addFields({ name: "ČÁSTKA - " + pridat + "Kč", value: "**Z účtu: **" + zuctu + "\nPoznámka: " + poznamka + "\nČas: " + Date.now });
                                    try {
                                        client.users.send(discordid2, newEmbed);
                                    } catch (e) {
                                        interaction.guild.channels.cache.find((ch) => (ch.id = trchid));
                                    }
                                    interaction.reply({ content: "Transakce proběhla vpořádku! Váš aktuální zůstatek: **" + castka + "**Kč", emphemeral: true });
                                });
                            });
                        } else {
                            const discordid2 = result[0].discord;
                            var zustatek2 = parsteInt(result[0].penize);
                            var novyzustatek2 = zustatek2 + pridat;
                            db.query("UPDATE charaktery SET penize = " + novyzustatek2 + " WHERE discord = " + discordid2, function (err) {
                                if (err) {
                                    console.log(err);
                                    interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_10", emphemeral: true });
                                    return;
                                }
                                const newEmbed = new EmbedBuilder()
                                    .setColor("#4f4f4f")
                                    .setTitle("NOVÁ PLATBA PŘIJATA")
                                    .addFields({ name: "ČÁSTKA - " + pridat + "Kč", value: "**Z účtu: **" + zuctu + "\nPoznámka: " + poznamka + "\nČas: " + Date.now });
                                try {
                                    client.users.send(discordid2, newEmbed);
                                } catch (e) {
                                    interaction.guild.channels.cache.find((ch) => (ch.id = trchid));
                                }
                                interaction.reply({ content: "Transakce proběhla vpořádku! Váš aktuální zůstatek: **" + castka + "**Kč", emphemeral: true });
                            });
                        }
                    });
                });
            });
        } else {
            db.query("SELECT zustatek,discordpristup FROM ucty WHERE cislouctu = " + zuctu, function (err, result) {
                if (err) {
                    console.log(err);
                    interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_5", emphemeral: true });
                    return;
                }
                if (!result[0].discordpristup === interaction.user.id) {
                    interaction.reply({ content: "Tento účet vám nepatří!", emphemeral: true });
                    return;
                }
                var castka = parseInt(result[0].penize) - pridat;
                if (castka < 0) {
                    interaction.reply({ content: "Je nám líto ale nemáte dostatek financí pro provedení této transakce!", emphemeral: true });
                    return;
                }
                db.query("UPDATE ucty SET zustatek = " + castka + " WHERE cislouctu = " + zuctu, function (err) {
                    if (err) {
                        console.log(err);
                        interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_6", emphemeral: true });
                        return;
                    }
                    db.query("SELECT penize,discord FROM charaktery WHERE cislouctu = " + cislouctu, function (err, result) {
                        if (err) {
                            console.log(err);
                            interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_7", emphemeral: true });
                            return;
                        } else if (result === undefined) {
                            db.query("SELECT zustatek,discordpristup FROM ucty WHERE cislouctu = " + cislouctu, function (err, result) {
                                const discordid2 = result[0].discordpristup;
                                var zustatek2 = parsteInt(result[0].zustatek);
                                var novyzustatek2 = zustatek2 + pridat;
                                db.query("UPDATE ucty SET zustatek = " + novyzustatek2 + " WHERE cislouctu = " + cislouctu, function (err) {
                                    if (err) {
                                        console.log(err);
                                        interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_8", emphemeral: true });
                                        return;
                                    }
                                    const newEmbed = new EmbedBuilder()
                                        .setColor("#4f4f4f")
                                        .setTitle("NOVÁ PLATBA PŘIJATA")
                                        .addFields({ name: "ČÁSTKA - " + pridat + "Kč", value: "**Z účtu: **" + zuctu + "\nPoznámka: " + poznamka + "\nČas: " + Date.now });
                                    try {
                                        client.users.send(discordid2, newEmbed);
                                    } catch (e) {
                                        interaction.guild.channels.cache.find((ch) => (ch.id = trchid));
                                    }
                                    interaction.reply({ content: "Transakce proběhla vpořádku! Váš aktuální zůstatek: **" + castka + "**Kč", emphemeral: true });
                                    return;
                                });
                            });
                        } else {
                            const discordid2 = result[0].discord;
                            var zustatek2 = parsteInt(result[0].penize);
                            var novyzustatek2 = zustatek2 + pridat;
                            db.query("UPDATE charaktery SET penize = " + novyzustatek2 + "WHERE discord = " + discordid2, function (err) {
                                if (err) {
                                    console.log(err);
                                    interaction.reply({ content: "Něco se pokailo na naší straně! Kontaktujte podporu. ERR_DAT_9", emphemeral: true });
                                    return;
                                }
                                const newEmbed = new EmbedBuilder()
                                    .setColor("#4f4f4f")
                                    .setTitle("NOVÁ PLATBA PŘIJATA")
                                    .addFields({ name: "ČÁSTKA - " + pridat + "Kč", value: "**Z účtu: **" + zuctu + "\nPoznámka: " + poznamka + "\nČas: " + Date.now });
                                try {
                                    client.users.send(discordid2, newEmbed);
                                } catch (e) {
                                    interaction.guild.channels.cache.find((ch) => (ch.id = trchid));
                                }
                                interaction.reply({ content: "Transakce proběhla vpořádku! Váš aktuální zůstatek: **" + castka + "**Kč", emphemeral: true });
                            });
                        }
                    });
                });
            });
        }
    },
};

I tried to catch the error and send it to another channel but that dont work too. Best will be when it can do that DM. But if theres no way to do it I will be happy if i can catch the error and send message to another channel.


Solution

  • You don't need to initialise a new Client in each of your commands. Most of discord.js structures extend Base, which means that client is available under client property. In your case it's interaction.client.

    Here's a cleaned up variant of your imports:

    //Importovani discord.js balicku do kodu
    const { EmbedBuilder, SlashCommandBuilder } = require("discord.js");
    
    var db = require("../nastaveni/db.js");
    // ...
    

    After that, make sure to replace client to interaction.client everywhere in your code. Example:

    client.users.send(...);
    // becomes
    interaction.client.users.send(...);