ReferenceError:未定义bot

时间:2018-01-23 02:01:57

标签: discord

这是const或var的东西。我认为除非我重新编码,否则它将无效。这是我的问题:

bot.on('ready', () => {
^

ReferenceError: bot is not defined
    at C:\Users\Dylan\Desktop\discord bot\app.js:7:1
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)

这是我的代码:

const Discord = require('discord');
const bot = new Discord.Bot();
const fs = require('fs')
const commandsList = fs.readFileSync('Storage/commands.txt','utf8');
});

bot.on('ready', () => {
  // This event will run if the bot starts, and logs in, successfully.
  console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels.`);
});

bot.on('message', message => {
    if (message.content === '!ping') {
    	message.channel.send('pong');
  	}
});

bot.on('message', message => {
    if (message.content === '!apply') {
    	message.channel.send('GOGLE STUFF');
  	}
});

bot.on('message', message => {
    if (message.content === '!server') {
    	message.channel.send('LA');
  	}
});

bot.on('message', message => {
    if (message.content === '!do you know dae wae') {
    	message.channel.send('Yes I know dae wae brother');
  	}
});

bot.on('message', message => {
    if (message.content === 'do you have a bot?') {
    	message.channel.send('no');
  	}
});

bot.on('message', message => {
    if (message.content === 'who is the owner') {
    	message.channel.send('FantasmicNerd, duh');
  	}
});

bot.on('message', message => {
    if (message.content === 'can i be staff' || message.content === 'can I be staff?' || message.content === 'can i be staff?') {
    	message.channel.send('Application - lalalalalala');
  	}
});

bot.on('message', message => {
    if (message.content === 'somebody touch my spaghet') {
    	message.channel.send('SOMEBODY TOUCH YOUR SPAGHET!');
  	}
});

bot.on('message', message => {
    if (message.content === 'so how about that airline food') {
    	message.channel.send('HAAHAHAHHAHAHAHHAHAHAHAHAHHAHAHHAHAHAHAHHAHAHAAHAHAHHAHAHAHHAHAHAHAHAHHAHAHHAHAHAHAHHAHA');
  	}
});

bot.on('ready',() => {
    console.log('Bot Launched...')

    bot.user.setStatus('Online')

    bot.user.setActivity('on The Magical')
});

bot.on('message', message => {
    if (message.content === '?help' || message.content === '?Help') {
    	message.channel.send('I have messaged you the commands and prefix.');
  	}
});

bot.on('message', message => {
    if (message.content === '?help' || message.content === '?Help') {
    	message.author.sendMessage(commandsList);
  	}
});

bot.on('ready', function() {
    bot.user.setUsername("The Magical");

// THIS  MUST  BE  THIS  WAY
bot.login('NDA0NjYzNzIwNDQzMzc5NzEy.DUapFw.zjDvPkG4QxZJ1rdxDYaPZEaVpiM');

我一直在寻找答案,但似乎没有任何效果。当我尝试运行它时,我在cmd提示符下结束了这个

2 个答案:

答案 0 :(得分:1)

如果您需要图书馆,则需要将其更改为discord.js才能访问图书馆 此外,该库不会公开名为“Bot”的类,因此您需要将其更改为"Client"

我猜第5行的)};应该关闭第88行的ready事件。
但我建议您坚持使用一个就绪事件,因此将状态和活动方法放在第一个就绪事件中并删除其他事件。此外,如果您将机器人用户名设置为您想要的,我将删除该方法,或检查用户名是否已设置为您想要的,如果是,则不要更改它。

与所有message事件相同,只需要一个message事件,并将该事件中的所有if语句链接起来。例如:

bot.on('message', message => {
    if (message.content === '!ping') {
        message.channel.send('pong');
    }
    else if (message.content === '!apply') {
        message.channel.send('GOGLE STUFF');
    }
    ...
});

此外,您还需要记住将变量定义为的内容。在第一个ready事件中,您使用的是client.users,而不是您定义的bot

答案 1 :(得分:1)

根据discord.js API文档,没有Bot类。 也许你需要的是

const client = new Discord.Client();

我建议您查看申请文件和示例。