javascript - 如何在discord.js 中创建使状态发生变化的命令?

标签 javascript node.js discord discord.js

我正在尝试在discord.js 中为我的机器人发出命令。它应该做什么,每当服务器的管理员使用它时,它都会改变状态。我尝试制作它,但它不起作用。这是我的代码:

client.on('message', message => {
  if (message.content.startsWith('!status')) {
   const prefix = '!';
    const args = message.content.slice(prefix.length).trim().split(' ');
    if (member.hasPermission('ADMINISTRATOR')) {
    const activity = args.splice(2);
    client.user.setActivity(activity);
    }
    else {
      message.channel.send('You\'re not the admin of the server or bot!')
    }
  }
});

最佳答案

第一个问题是member未定义,如果你想让ti成为消息作者,可以使用message.member代替。

第二个问题是activity是一个数组,它needs to be a stringActivityOptions目的。您可以使用 args 的第一个元素作为状态,或者如果您想接受多个单词,请通过空格连接它们。

检查下面的代码,我也添加了一些注释:

const prefix = '!';

client.on('message', (message) => {
  // if the message doesn't start with the prefix or the author is a bot, exit early
  if (!message.content.startsWith(prefix) || message.author.bot) return;

  // create an args variable that slices off the prefix and splits it into an array
  const args = message.content.slice(prefix.length).split(/ +/);
  // create a command variable by taking the first element in the array
  // and removing it from args
  const command = args.shift().toLowerCase();

  if (command === 'status') {
    // if the message author has no permission, send a message and exit
    if (!message.member.hasPermission('ADMINISTRATOR'))
      return message.channel.send('Your not the Admin of the server or bot!');

    // if there are more than one words as the status, join them again
    const activity = args.join(' ');
    client.user.setActivity(activity);
  }
});

关于javascript - 如何在discord.js 中创建使状态发生变化的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67534879/

相关文章:

javascript - 如何返回公会的所有者名称

javascript - 在 svg 路径中​​动态绘制箭头不起作用

c# - 无法作为 webService 访问简单 Controller

node.js - 无法使用 create-react-app 生成项目

css - DiscordApp 的自定义 CSS 代码

python - Discord 机器人将嵌入消息发送到另一个 channel

javascript - 从右向左打印一个三 Angular 形

javascript - 在内存中创建的图像会阻碍页面渲染/性能吗

javascript - 如何根据属性的总和来组织对象数组?

javascript - Promise 拒绝给我一个 UnhandledPromiseRejectionWarning 错误