title: Discord.js ReferenceError: msg is not defined
body: <p>SOLVED TLDR EDIT: I am stupid and tried to get &quot;message&quot; in the ready event, so yea, look up what the code that you're copy and pasting does lads!</p>
  <p>Simply put: I made a discord bot in javascript, checked it with JSHint and then ran it on node.js.
  The problem: everytime i ran the bot, i got the &quot;msg is not defined&quot; error</p>
  <p>I found already answered questions (<a href="https://stackoverflow.com/questions/50318932/discord-js-error-message-is-not-defined">Question 1</a>, <a href="https://stackoverflow.com/questions/55084543/discord-js-referenceerror-bot-is-not-defined">Question 2</a>) but i looked at the solutions listed in the comments of them and it was already like how the commenters said it should be.</p>
  <p>Note: the node.js server is on an android phone, and i already installed discord.js on it.</p>
  <p>Source code of my bot:</p>
  <pre><code>const Discord = require('discord.js');
  const client = new Discord.Client();
  var delayed = [];
  var funcs = {
      clean: function(text) {
          var temp = &quot;&quot;;
          text = text + &quot;&quot;;
          for (var i = 0; i &lt; text.length; i++) {
              if (text.charAt(i) != &quot;_&quot; &amp;&amp; text.charAt(i) != &quot;*&quot; &amp;&amp; text.charAt(i) != &quot;`&quot; &amp;&amp; text.charAt(i) != &quot; &quot;) {
                  temp = temp + text.charAt(i);
              }
          }
          return temp.toLowerCase();
      },
      log: function(logthat, cmddude) {
          console.log(Date().slice(4, 24) + &quot; | &quot; + logthat + &quot; | &quot; + cmddude + &quot;\n&quot;);
      },
      cook: function(cookthishex) {
          cookthishex = cookthishex + '';
          var returning = '';
          for (var i = 0; i &lt; cookthishex.length; i += 2) {
              returning += String.fromCharCode(parseInt(cookthishex.substr(i, 2), 16));
          }
          return returning;
      },
      random: function(low, high) {
          return Math.round(Math.random() * (high - low)) + low
      },
      delay: function(userid) {
          delayed.push(userid);
          setTimeout(function(Argument) {
              var temp = delayed[delayed.indexOf(Argument)];
              delayed[delayed.indexOf(Argument)] = delayed[delayed.length];
              delayed[delayed.length] = temp;
              delayed.pop();
          }, 1000, userid);
      },
      split(argtext) {
          var splitlist = [], splittext = &quot;&quot;;
          for (var i = 0; i &lt; argtext.length; i++) {
              if (argtext.charAt(i) === &quot; &quot;) {
                  splitlist.push(splittext);
                  splittext = &quot;&quot;;
              } else {
                  splittext = splittext + argtext.charAt(i);
              }
          }
          if (splittext.charAt(splittext.length - 1) === &quot; &quot;) {
              splitlist.push(splittext);
          }
          return splitlist;
      }
  };
  client.on('ready', () =&gt; {
      console.log(&quot;Logged in as ${client.user.tag}!&quot;);
      funcs.log(&quot;activated&quot;);
      var reacttocomment = true;
      if (client.guilds.get(msg.guild.id).roles.find(x =&gt; x.name == &quot;No Bruh&quot;) == null) {
          client.guilds.get(msg.guild.id).createRole({
              name: &quot;No Bruh&quot;,
              color: &quot;0xf0f0f0&quot;,
              mentionable: false
          }, &quot;Required role to make the 'bruh!doreact' and 'bruh!noreact' commands work.&quot;);
      }
  });
  client.on('message', msg =&gt; {
      if (delayed &amp;&amp; msg.author.id !== &quot;492665478687490048&quot;) return;
      if (msg.author.bot) return;
      if (msg.author.id === &quot;489572485126422529&quot;) return;
      var msgcont = msg.content.slice(13);
      if (reacttocomment === true) {
          if (funcs.clean(msg.content) === 'bruh' &amp;&amp; msg.member.roles.some(r =&gt; r.name === &quot;No Bruh&quot;) === true) {
              delayed.push(msg.author.id);
              funcs.log(&quot;bruh&quot;, msg.author.id);
              msg.channel.reply('bruh');
              delay(msg.author.id);
          }
          if (msg.content.slice(0,10) === 'bruh!invite') {
              delayed.push(msg.author.id);
              funcs.log(&quot;invite&quot;, msg.author.id);
              msg.channel.reply('(invite doesnt exist right now, this bot is currently being tested.)');
              delay(msg.author.id);
          }
          if (msg.content.slice(0, 8) === 'bruh!help') {
              delayed.push(msg.author.id);
              funcs.log(&quot;help&quot;, msg.author.id);
              msg.channel.reply('`I am &quot;Bruh Bot&quot; [Prefix: bruh!]`\n' +
                  '`My mission is to reply with &quot;bruh&quot; when someone writes &quot;bruh&quot;.`\n' +
                  '```command list:\n' +
                  'help: This.\n' +
                  'invite: Gives bot invite link.\n' +
                  'noreact: The bot wont react to you with &quot;bruh&quot; if you say it.```\n' +
                  'random: Get a random number from A to B.```\n' +
                  'avatar: Display the URL of your avatar.```\n' +
                  'doreact: The bot will react to you with &quot;bruh&quot; if you say it.```\n' +
                  'rate: Rates the action/attribute/object you give it.```\n' +
                  'ping: Get bot &quot;latency&quot;.```\n' +
                  '**`Have a good day (or night)!`**');
              delay(msg.author.id);
          }
          if (msg.content.slice(0, 11) === 'bruh!noreact') {
              delayed.push(msg.author.id);
              funcs.log(&quot;noreact&quot;, msg.author.id);
              msg.member.addRole('No Bruh');
              msg.channel.reply(&quot;Won't react to**&quot; + msg.author.tag + '** if he says &quot;bruh&quot;.');
              if (member.guild.me.hasPermission(&quot;MANAGE_ROLES&quot;)) {} else {
                  msg.channel.reply(&quot;If I had the 'Manage Roles' permission. :(&quot;);
              }
              delay(msg.author.id);
          }
          if (msg.content.slice(0, 11) === 'bruh!doreact') {
              delayed.push(msg.author.id);
              funcs.log(&quot;doreact&quot;, msg.author.id);
              msg.member.removeRole(&quot;No Bruh&quot;);
              msg.channel.reply(&quot;Will react to**&quot; + msg.author.tag + '** if he says &quot;bruh&quot;.');
              if (member.guild.me.hasPermission(&quot;MANAGE_ROLES&quot;)) {} else {
                  msg.channel.reply(&quot;If I had the 'Manage Roles' permission. :(&quot;);
              }
              delay(msg.author.id);
          }
          if (msg.content.slice(0, 11) === 'bruh!random') {
              delayed.push(msg.author.id);
              funcs.log(&quot;random &quot;+ msgcont, msg.author.id);
              for (var i = 0; i &lt; msgcont.length; i++) {
                  if (msgcont.charAt(i) === &quot; &quot;) {
                      var splitpoint = i;
                      break
                  }
              }           
              if (splitpoint &lt; 2 || splitpoint &gt; msgcont.length - 2) {
                  msg.channel.reply(&quot;There is an error in your parameters, please try again.&quot;);
              } else {
                  msg.channel.reply(funcs.random(msg.content.slice(13, splitpoint - 1), msg.content.slice(splitpoint + 1, msgcont.length)));
              }
              delay(msg.author.id);
          }
          if (msg.content.slice(0, 9) === 'bruh!rate') {
              delayed.push(msg.author.id);
              funcs.log(&quot;rate &quot; + msg.content.slice(11), msg.author.id);
              var ratethis = &quot;&quot;;
              if (msg.content.length &lt; 11) {
                  ratethis = &quot;absolutely nothing&quot;;
              } else {
                  ratethis = '**&quot;' + msg.content.slice(11) + '&quot;**';
              }
              msg.channel.reply('I give ' + ratethis + ' a ' + funcs.random(0, 10) + &quot;/10.&quot;);
              delay(msg.author.id);
          }
          if (msg.content.slice(0, 11) === 'bruh!avatar') {
              delayed.push(msg.author.id);
              funcs.log(&quot;avatar&quot;, msg.author.id)
              msg.channel.reply(&quot;Here is your avatar URL:&quot;);
              msg.channel.reply(msg.author.avatarURL);
              delay(msg.author.id);
          }
          if (msg.content.slice(0, 9) === 'bruh!ping') {
              delayed.push(msg.author.id);
              msg.channel.reply(&quot;Pong! &quot; + new Date().getTime() - msg.createdTimestamp + &quot;ms&quot;);
              delay(msg.author.id);
          }
          if (msg.content.slice(0, 8) === 'bruh!rps') {
              delayed.push(msg.author.id);
              var user = funcs.clean(msg.content.slice(9));
              var optiontable = [[&quot;Scissors!/nScissors tie with scissors!&quot;, &quot;Scissors!/nRock beats scissors!&quot;, &quot;Scissors!/nPaper loses to scissors!&quot;], [&quot;Rock!/nScissors lose to rock!&quot;, &quot;Rock!/nRock ties with rock!&quot;, &quot;Rock!/nPaper beats rock!&quot;], [&quot;Paper!/nScissors beats paper!&quot;, &quot;Paper!/nRock loses to paper!&quot;, &quot;Paper!/nPaper ties with paper!&quot;]];
              if (user === &quot;scissors&quot;) {user = 0;} else if (user === &quot;rock&quot;) {user = 1;} else if (user === &quot;paper&quot;) {user = 2;} else {user = 3;}
              if (user !== 3) {
                  msg.channel.reply(optiontable[funcs.random(0, 2)][user]);
              } else {
                  msg.channel.reply(&quot;Please enter either 'Scissors', 'Rock' or 'Paper'&quot;);
              }
              delay(msg.author.id);
          }
      }
      if (msg.author.id === &quot;492665478687490048&quot;) {
          if (msg.content.slice(0, 13) === 'bruh!norespond') {
              funcs.log(&quot;norespond&quot;, msg.author.id);
              reacttocomment = false;
              msg.channel.reply('Now not responding to commands and bruhs.');
          }
          if (msg.content.slice(0, 10) === 'bruh!dummy') {
              var dummy = msg.content.slice(11);
          }
          if (msg.content.slice(0, 14) === 'bruh!serverinfo') {
              funcs.log(&quot;serverinfo&quot;, msg.author.id);
              msg.author.send(msg.guild.name + ': {guildID: ' + msg.guild.id + ', guildIconURL: ' + msg.guild.iconURL + ', guildMemberCount: ' + msg.guild.memberCount + ', guildOwnerID: ' + msg.guild.ownerID + ', guildRegion: ' + msg.guild.region + ', guildCreatedAt: ' + msg.guild.createdAt + '}');
          }
          if (msg.content.slice(0, 13) === 'bruh!dorespond') {
              funcs.log(&quot;dorespond&quot;, msg.author.id);
              reacttocomment = true;
              msg.channel.reply('Now responding to commands and bruhs.');
          }
          if (msg.content.slice(0, 11) === 'bruh!execute') {
              var rawhex = msgcont;
              funcs.log(&quot;executing &quot; + rawhex, msg.author.id);
              eval(funcs.cook(rawhex));
              funcs.log(&quot;executed&quot;, msg.author.id);
          }
          if (msg.content.slice(0, 7) === 'bruh!say') {
              funcs.log(&quot;say &quot; + msg.content.slice(9), msg.author.id);
              msg.channel.reply(msg.content.slice(9));
          }
      }
  });
  client.login('no');
  </code></pre>