首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >找不到引用错误问题的来源

找不到引用错误问题的来源
EN

Stack Overflow用户
提问于 2020-02-25 23:04:57
回答 1查看 32关注 0票数 0

我正在尝试从An Idiot's Guide进行教程,但我被here卡住了。在这一部分,我的机器人必须向每一个新成员问好……但当我尝试使用我的辅助账号时,我收到了来自Discord的消息,但不是来自我的机器人,而是他显示了这个错误:

代码语言:javascript
运行
复制
UnhandledPromiseRejectionWarning: ReferenceError: config is not defined

我知道,对于他来说,配置丢失了,但是当我尝试其他命令(比如!ping)时,它抛出了同样的错误!

代码语言:javascript
运行
复制
//There is code from:
//https://gist.github.com/eslachance/3349734a98d30011bb202f47342601d3

const Discord = require('discord.js');
const { prefix, token, state } = require('./config.json');
const client = new Discord.Client();
const newUsers = [];

//Say hello to every new user
client.on("guildMemberAdd", (member) => {
  const guild = member.guild;
  if (!newUsers[guild.id]) newUsers[guild.id] = new Discord.Collection();
  newUsers[guild.id].set(member.id, member.user);

  if (newUsers[guild.id].size > 10) {
    const userlist = newUsers[guild.id].map(u => u.toString()).join(" ");
    guild.channels.find(channel => channel.name === "general").send("Welcome our new users!\n" + userlist);
    newUsers[guild.id].clear();
  }
});

client.on("guildMemberRemove", (member) => {
  const guild = member.guild;
  if (newUsers[guild.id].has(member.id)) newUsers.delete(member.id);
});


//commands code
client.on("message", message => {
  if (message.author.bot) return;
  if (message.content.indexOf(config.prefix) !== 0) return;

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


  //if !beep, answer boop
  if (message.content === `${prefix}beep`) {
    message.channel.send('boop');
  }
//more commands
});

client.login(token);

感谢您的阅读。如果你看到任何可以改进的地方(即使是问题),请让我知道!同样的问题或评论,请让我知道!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-26 00:14:30

您正在使用未定义的对象config

代码语言:javascript
运行
复制
if (message.content.indexOf(config.prefix) !== 0) return;

还有这里

代码语言:javascript
运行
复制
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);

快速修复:添加一行:

代码语言:javascript
运行
复制
const config = require('./config.json');

或删除对配置对象的引用“

代码语言:javascript
运行
复制
if (message.content.indexOf(prefix) !== 0) return;

代码语言:javascript
运行
复制
const args = message.content.slice(prefix.length).trim().split(/ +/g);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60397596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档