discord - Currency is not defined
问题描述
I decided to use this discord.js guide to create my economy discord bot. Apparently something went wrong as I was about to run the bot. It had this error message and it said :
Reflect.defineProperty(currency, 'add', {
^
ReferenceError: currency is not defined
I have no idea what was wrong with it. Here's my index.js:
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
Reflect.defineProperty(currency, 'add', {
/* eslint-disable-next-line func-name-matching */
value: async function add(id, amount) {
const user = currency.get(id);
if (user) {
user.balance += Number(amount);
return user.save();
}
const newUser = await Users.create({ user_id: id, balance: amount });
currency.set(id, newUser);
return newUser;
},
});
Reflect.defineProperty(currency, 'getBalance', {
/* eslint-disable-next-line func-name-matching */
value: function getBalance(id) {
const user = currency.get(id);
return user ? user.balance : 0;
},
});
client.once('ready', () => {
console.log('Ready!');
});
});
client.on('message', async message => {
if (message.author.bot) return;
currency.add(message.author.id, 1);
if (!message.content.startsWith(PREFIX)) return;
const input = message.content.slice(PREFIX.length).trim();
if (!input.length) return;
const [, command, commandArgs] = input.match(/(\w+)\s*([\s\S]*)/);
if (command === 'balance') {
const target = message.mentions.users.first() || message.author;
return message.channel.send(`${target.tag} has ${currency.getBalance(target.id)}`);
} else if (command === 'inventory') {
const target = message.mentions.users.first() || message.author;
const user = await Users.findOne({ where: { user_id: target.id } });
const items = await user.getItems();
if (!items.length) return message.channel.send(`${target.tag} has nothing!`);
return message.channel.send(`${target.tag} currently has ${items.map(t => `${t.amount} ${t.item.name}`).join(', ')}`);
} else if (command === 'transfer') {
const currentAmount = currency.getBalance(message.author.id);
const transferAmount = commandArgs.split(/ +/).find(arg => !/<@!?\d+>/.test(arg));
const transferTarget = message.mentions.users.first();
if (!transferAmount || isNaN(transferAmount)) return message.channel.send(`Sorry ${message.author}, that's an invalid amount`);
if (transferAmount > currentAmount) return message.channel.send(`Sorry ${message.author} you don't have that much.`);
if (transferAmount <= 0) return message.channel.send(`Please enter an amount greater than zero, ${message.author}`);
currency.add(message.author.id, -transferAmount);
currency.add(transferTarget.id, transferAmount);
return message.channel.send(`Successfully transferred ${transferAmount} to ${transferTarget.tag}. Your current balance is ${currency.getBalance(message.author.id)}`);
} else if (command === 'buy') {
const item = await CurrencyShop.findOne({ where: { name: { [Op.like]: commandArgs } } });
if (!item) return message.channel.send('That item doesn\'t exist.');
if (item.cost > currency.getBalance(message.author.id)) {
return message.channel.send(`You don't have enough currency, ${message.author}`);
}
const user = await Users.findOne({ where: { user_id: message.author.id } });
currency.add(message.author.id, -item.cost);
await user.addItem(item);
message.channel.send(`You've bought a ${item.name}`);
} else if (command === 'shop') {
const items = await CurrencyShop.findAll();
return message.channel.send(items.map(i => `${i.name}: ${i.cost}`).join('\n'), { code: true });
} else if (command === 'leaderboard') {
return message.channel.send(
currency.sort((a, b) => b.balance - a.balance)
.filter(user => client.users.cache.has(user.user_id))
.first(10)
.map((user, position) => `(${position + 1}) ${(client.users.cache.get(user.user_id).tag)}: ${user.balance}`)
.join('\n'),
{ code: true }
);
}
});
client.login(`[TOKEN]`);
I presume the only probelm of this code is Reflect.defineProperty(currency, 'add', { as it can't define what is currency. I hope someone can help...
解决方案
您正在尝试使用该currency
变量,但您尚未定义它,因此 JavaScript 理所当然地抱怨,因为它不知道是什么currency
。
您的代码中缺少指南中的以下行:
const currency = new Discord.Collection();
推荐阅读
- r - 在 R-markdown 中保持表格位置
- python - Pytorch 量化运行时错误:尝试创建具有负维度的张量
- gcc - 为什么 GCC 会自己编译 3 次?
- python - 在相等的列值上切片熊猫数据框
- flutter - 如何在 ListView 中将项目居中
- python - 从单元测试 Python 解析 XML 文件
- matlab - 如何克服这个错误?试图访问 U(2,0);索引必须是正整数或逻辑
- javascript - C# - 在 .Net Core 中重新应用对 JS 对象的操作
- delphi - 使用来自 Yubico 的 fido2.dll 进行 Webauthn 凭证验证
- java - 在字符串中编辑 Unicode