最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - discord.js sending DM in response to user's slash commands - Stack Overflow

programmeradmin1浏览0评论

I'm trying to send a DM from our Discord bot to the user when they use one of the new Discord slash mands.

The code is below. The Discord docs say that `interaction.member should be a Discord GuildMember, however, the code below gives me the following error:

TypeError: interaction.member.send is not a function

I can call other local functions from the data field but have been unable to figure out how to DM the user back. I assume I'm doing something wrong (per the error), but I can't figure out the cookbook to DM the user from the slash mand callback.

client.ws.on("INTERACTION_CREATE", async (interaction) => {
    const mand = interaction.data.name.toLowerCase();
    const args = interaction.data.options;

    if (mand == "testing") {
        client.api.interactions(interaction.id, interaction.token).callback.post({
            data: {
                type: 2,
                data: interaction.member.send("hello").catch(console.error),
            },
        });
    }
});

EDIT: Final solution with help from Jakye. Note, I had to use "fetch" instead of "get", because get kept returning an undefined user.

if (mand == 'testing') {
  client.users.fetch(interaction.member.user.id)
    .then(user => user.send("hello").catch(console.error))
    .catch(console.error);

  client.api.interactions(interaction.id, interaction.token).callback.post({
    data: {
      type: 2,
    }
  });
}

I'm trying to send a DM from our Discord bot to the user when they use one of the new Discord slash mands.

The code is below. The Discord docs say that `interaction.member should be a Discord GuildMember, however, the code below gives me the following error:

TypeError: interaction.member.send is not a function

I can call other local functions from the data field but have been unable to figure out how to DM the user back. I assume I'm doing something wrong (per the error), but I can't figure out the cookbook to DM the user from the slash mand callback.

client.ws.on("INTERACTION_CREATE", async (interaction) => {
    const mand = interaction.data.name.toLowerCase();
    const args = interaction.data.options;

    if (mand == "testing") {
        client.api.interactions(interaction.id, interaction.token).callback.post({
            data: {
                type: 2,
                data: interaction.member.send("hello").catch(console.error),
            },
        });
    }
});

EDIT: Final solution with help from Jakye. Note, I had to use "fetch" instead of "get", because get kept returning an undefined user.

if (mand == 'testing') {
  client.users.fetch(interaction.member.user.id)
    .then(user => user.send("hello").catch(console.error))
    .catch(console.error);

  client.api.interactions(interaction.id, interaction.token).callback.post({
    data: {
      type: 2,
    }
  });
}

Share Improve this question edited Dec 26, 2020 at 21:28 Alchete asked Dec 26, 2020 at 17:48 AlcheteAlchete 1,6492 gold badges19 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The interaction data is ing directly from Discord's API, as a consequence interaction.member will be an Object.

member: {
    user: {
      username: 'Username',
      public_flags: 0,
      id: '0',
      discriminator: '0000',
      avatar: ''
    },
    roles: [],
    premium_since: null,
    permissions: '0',
    pending: false,
    nick: null,
    mute: false,
    joined_at: '2020-12-26T19:10:54.943000+00:00',
    is_pending: false,
    deaf: false
  }

You'll have to manually get the member, by either getting it from the cache or fetching it from the API.

const user = client.users.cache.get(interaction.member.user.id);
user.send("Hello").catch(console.error);

client.ws.on("INTERACTION_CREATE", async interaction => {
    const guild = client.guilds.cache.get(interaction.guild_id);
    const user = client.users.cache.get(interaction.member.user.id);

    user.send(`hello, you used the ${interaction.data.name.toLowerCase()} mand in ${guild.name}`).catch(console.error);
});
发布评论

评论列表(0)

  1. 暂无评论