javascript 尝试获取运行ping命令的用户的用户名并将其记录下来

nhhxz33t  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(123)
const TOKEN = "mybotstoken";
const CLIENT_ID = "mybotsclientid";

const { REST, Routes } = require('discord.js');

const commands = [
  {
    name: 'ping',
    description: 'Replies with Pong!',
  },
];

const rest = new REST({ version: '10' }).setToken(TOKEN);

(async () => {
  try {
    console.log('Started refreshing application (/) commands.');

    await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commands });

    console.log('Successfully reloaded application (/) commands.');
  } catch (error) {
    console.error(error);
  }
})();

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

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

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

  if (interaction.commandName === 'ping') {
    const user = interaction.author;

    const username = user.username;
    console.log(`Got a ping execute request from: ${username}`);

    await interaction.reply('Pong!');
    console.log(`Execute succesful`);
  }
});

client.login(TOKEN);

这个密码有什么问题吗?
我得到错误:第一个月

h9vpoimq

h9vpoimq1#

因为没有interaction.author,你可能以为它和message.author一样,有interaction.userinteraction.member,所以如果你想得到作者的用户名,可以用前者:

const username = interaction.user.username;

相关问题