【问题标题】:SetXp command does only work on the person who uses the commandSetXp 命令只对使用该命令的人有效
【发布时间】:2021-05-24 04:04:38
【问题描述】:

SetXp 命令只对使用该命令的人有效,如果我想给其他人 xp 它不起作用。我希望有人知道我需要做什么。 谢谢

const Discord = require('discord.js')
const bot = new Discord.Client();
const Levels = require('discord-xp')

Levels.setURL("")

bot.login('')

bot.on("ready!", bot => {
    console.log('Bot is Online!')
})

bot.on("message", async message => {
    if (!message.guild) return;
    if (message.author.bot) return;

    const prefix = '+';

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    var randomXp = Math.floor(Math.random() * 9) + 1; //Random amont of XP until the number you want + 1
    const hasLeveledUp = await Levels.appendXp(message.author.id, message.guild.id, randomXp);
    if (hasLeveledUp) {
        const user = await Levels.fetch(message.author.id, message.guild.id);
        message.channel.send(`You leveled up to ${user.level}! Keep it going!`);
    }
    
    if(command === "setxp" || command === "setXp") {
        const member = await message.mentions.members.first(args[0]) || message.guild.members.cache.get(args[0]); // setxp @user
        const amount = args[1]; // setxp @user 100
        Levels.setXp(message.member.id, message.guild.id, amount);
        
        
        message.channel.send(`here your xp my friend`)
    
}
})

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您的代码无法运行的原因有两个。

    1. 您没有正确获取member

    当您执行message.mentions.members.first() 时,您将访问message.mentions.members 的集合,因此在给定消息中所有提到的公会成员。这可以是一个,但也可以比first() 发挥作用的地方更多。正如您可能已经猜到的那样,它返回给定集合中的第一项。这也意味着您不需要向它传递任何参数。

    TL;DR:从message.mentions.members.first() 中删除参数。

    1. 您没有将 XP 提供给正确的人 注意:这是真正的问题

    在您的代码中,您将message.member.id 传递给setXp 函数。那总是发送消息的人。因此,要解决此问题,您需要将 member 从 1. 实际传递给函数。

    Levels.setXp(member.id, message.guild.id, amount);
    

    一些额外的想法:

    • 您可以删除|| command === "setXp,因为您在开始时将命令转换为小写。
    • 如果您计划拥有超过 1 个命令,您可能需要考虑使用命令处理程序。有关详细信息,请参阅here
    • here are your xp my friendhere're your xp my friend ?

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 1970-01-01
      • 2016-07-05
      • 2013-06-05
      • 2018-11-03
      • 2021-09-25
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多