Discord.js 的参数 [英] Arguments for Discord.js

查看:19
本文介绍了Discord.js 的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 discord.js 中读取 args?我正在尝试创建一个支持机器人,并且我想要一个 !help {topic} 命令.我怎么做?我当前的代码非常基础

how do I read args in discord.js? I am trying to create a support bot and I want to have an !help {topic} command. how do I do that? my current code is very basic

const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = ("!")
const token = ("removed")

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

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('pong');
  }
  if (msg.content === 'help') {
    msg.reply('type -new to create a support ticket');
  }

});

client.login(token);

推荐答案

你可以像这样使用前缀和参数...

You can make use of a prefix and arguments like so...

const prefix = '!'; // just an example, change to whatever you want

client.on('message', message => {
  if (!message.content.startsWith(prefix)) return;

  const args = message.content.trim().split(/ +/g);
  const cmd = args[0].slice(prefix.length).toLowerCase(); // case INsensitive, without prefix

  if (cmd === 'ping') message.reply('pong');

  if (cmd === 'help') {
    if (!args[1]) return message.reply('Please specify a topic.');
    if (args[2]) return message.reply('Too many arguments.');

    // command code
  }
});

这篇关于Discord.js 的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆