如何为我的机器人编写带有原因功能的踢球

时间:2019-04-21 22:33:25

标签: javascript discord discord.js

我正试图让我的代码有理由踢人,而DM则说他们已被<server>踢给<reason>。目前没有发出任何理由。

看过在线教程,但没有运气。我也问过一些人,但他们也不知道。似乎没有多少人使用discord.js,任何帮助将不胜感激。 =)

Message Bot Sends Message I Sent

const Discord = require('discord.js');
const { token } = require('./config.json');
var { prefix, logchannel } = require('./config.json');
const client = new Discord.Client();

client.once('ready', () => {
  console.log('Bot loaded!');
  client.user.setActivity('SUPPORT: !info', {
    type: 'PLAYING'
  });
})


client.on('message', message => {

  if (message.content === `${prefix}ping`) {
    message.channel.send('Pong.');
    console.log(`COMMAND: ${message.member.displayName} issued ping command.`);
  } else if (message.content === `${prefix}info`) {
    message.channel.send(`This server's name is: ${message.guild.name}
It has ${message.guild.memberCount} total members.
It is running **WastefulBot** by **USER HIDDEN**
For support please join LINK HIDDEN`);
    console.log(`COMMAND: ${message.member.displayName} issued info command.`);
  } else if (message.content === `${prefix}help`) {
    console.log(`COMMAND: ${message.member.displayName} issued help command.`);
    message.delete();
    message.channel.send('Please check your dm\'s.');
    message.member.send(`__**WastefulBot Help**__
!ping | Pong.
!info | Returns information on the server and how to get support for the bot.
!donate | Get information on how to donate.
!help | Shows this information.
__**Administration Commands**__
(Please note these commands require corresponding permissions)
!kick <@user> | Kicks the specified user.
!ban <@user> | Bans the specified user.
__**Management Commands**__
(Please note these commands require corresponding permissions)
!staff | Grants a user staff. (WIP - May not work on most servers.)
`);
  }

  if (message.content === (`${prefix}setlog`)) {
    if (message.member.hasPermission("MANAGE_CHANNEL")) {
      message.channel.send('Sorry. This command is currently WIP.');
      console.log(`WARNING: ${message.member.displayName} issued setlog command, this command is WIP and does not work.`);
    }
  }

  if (message.content === (`${prefix}donate`)) {
    message.channel.send('You can donate at ``https://www.patreon.com/wasteful_development``');
  }

  //KICK COMMAND STARTS HERE
  else if (message.content.startsWith(`${prefix}kick`)) {

    if (message.member.hasPermission("KICK_MEMBERS")) {
      if (!message.mentions.users.size) {
        return message.reply('You must tag 1 user.');
      } else {
        let member = message.mentions.members.first();
        let reason = message.content.split(" ").slice(22);
        if (member.kickable == false) {
          message.channel.send("That user cannot be kicked!");
          return;
        } else {
          member.send(`You have been kicked from \`\`${message.guild.name}\`\` for the reason ${reason}.`).then((message) => {
            member.kick(reason).then((member) => {
              message.channel.send(" " + member.displayName + " has been kicked!");
              console.log(`KICK: ${message.member.displayName} successfully executed kick command.`);
            });
          });
        }
      }
    } else {
      message.channel.send("You do not have permission to use kick.");
      return;
    }
  }

  //End of kick mthd
  else if (message.content.startsWith(`${prefix}staff`)) {
    if (message.member.hasPermission("MANAGE_ROLES")) {
      if (!message.mentions.users.size) {
        return message.reply('You must tag 1 user.');
      } else {
        let member = message.mentions.members.first();
        member.addRole("569174153337503804");
        message.channel.send(`Granted ${member.displayName} staff!`);
        message.channel.send(`Warning this command will not have worked on most servers as it is WIP.`);
        console.log(`STAFF: ${message.member.displayName} successfully granted ${member.displayName} staff .`);
      }

    } else {
      message.channel.send("**You do not have permission to grant users staff.**");
      return;
    }
  }

});

client.login(token);

3 个答案:

答案 0 :(得分:2)

说明:

看看您的错误,这是问题的时间表。

  1. 所有这些信息的来源是,由于僵尸网络(here)中的隐私设置,该漫游器无法向其发送DM。这导致member.send(...)被拒绝。
  2. 由于未捕获到被拒绝的承诺,因此代码继续以.then()不返回任何内容,而不是预期的message参数。
  3. 当您尝试读取displayName的{​​{1}}属性时,由于从未返回您的参数message.member,它会引发错误。

解决方案:

实施message方法来处理被拒绝承诺的可能性

请记住,您必须将函数声明为异步才能使用.catch()

await

然后,在您的命令中:

client.on('message', async message => { // ES6

答案 1 :(得分:1)

您的漫游器可能由于其隐私设置而无法被DM用户踢出。因为用户被踢了,所以他们不再与bot共享服务器(除非它是一个非常普遍使用的bot,这将增加用户使用该bot在多台服务器中的几率),这意味着该bot是无法向该用户发送DM的可能性很高。

答案 2 :(得分:-2)

以下不是答案,用户和成员都具有.send()

您的代码中的错误是class App extends React.Component { constructor(props) { super(props); this.state = { data: [], library: null, }; } componentDidMount() { fetch("/json.bc", { method: "get" }) .then(response => response.text()) .then(text => { let Maindata = JSON.parse(text.replace(/\'/g, '"')); this.setState( state => ({ ...state, data: Maindata }), () => { this.reorganiseLibrary(); } ); }) .catch(error => console.error(error)); } reorganiseLibrary = () => { const { data } = this.state; let library = data; library = _.chunk(library); this.setState({ library, }); }; // handle per page handlePerPage = evt => this.setState( { perPage: evt.target.value }, () => this.reorganiseLibrary() ); renderMinTotal = () => { const { library } = this.state; if (!library || (library && library.length === 0)) { return ""; } return library.reduce((acc, lib) => { const libMin = Math.min(...lib.map(item => item.totalCom)); return acc === undefined ? libMin : libMin < acc ? libMin : acc; }, undefined); }; renderMaxTotal = () => { const { library } = this.state; if (!library || (library && library.length === 0)) { return ""; } return library.reduce((acc, lib) => { const libMax = Math.max(...lib.map(item => item.totalCom)); return libMax > acc ? libMax : acc; }, 0); }; render() { const { library } = this.state; return ( <div> <div> <input type="range" min={this.renderMinTotal()} max={this.renderMaxTotal()} defaultValue={this.renderMaxTotal()} step="1000" className="multirange" /> </div> </div> ); } } ReactDOM.render(<App />, document.getElementById('Result')) 。您会看到memberuser是两回事。 member.send()仅适用于user, so you should do member.user.send(),并且可以使用。
另外,您将需要兑现该承诺,因此,如果该用户禁用了DM,则由于漫游器无法禁用该用户,因此bot无法对其进行DM处理。像这样:

.send()

请记住,await member.user.send(`You have been kicked!`) .catch(() => message.channel.send("User seems to have DMs disabled!")); await member.kick("reason") .then(member => message.channel.send(`${member.displayName} has been kicked!`)) .catch(e => console.error(e.stack)); another way在异步函数中链接await的情况。