Need help with token, about Discord


#1

Hi ! Where do I need to put this in config.js ?


#2

You put it in the .env file in your Glitch project


#3

yes i did it but i have also to put i in config.js but where in config.js ? @Gareth


#4

You use it within the login function e.g. https://glitch.com/edit/#!/discordbot-example?path=server.js:1:0


#5

but i use the Eris library, this is my config.js code @Gareth :

const json5 = require('json5');
const fs = require('fs');
const path = require('path');

let userConfig;

// Try to find our config file from several options
const configFiles = [
  'config.json',
  'config.json5',
  'config.json.json',
  'config.json.txt',
  'config.js'
];

let foundConfigFile;

for (const configFile of configFiles) {
  try {
    fs.accessSync(__dirname + '/../' + configFile);
    foundConfigFile = configFile;
    break;
  } catch (e) {}
}

if (! foundConfigFile) {
  throw new Error(`Could not find config.json!`);
}

// Parse the config using JSON5
try {
  if (foundConfigFile.endsWith('.js')) {
    userConfig = require(`../${foundConfigFile}`);
  } else {
    const raw = fs.readFileSync(__dirname + '/../' + foundConfigFile);
    userConfig = json5.parse(raw);
  }
} catch (e) {
  throw new Error(`Error reading config file! The error given was: ${e.message}`);
}

const defaultConfig = {
  "token": null,
  "mailGuildId": null,
  "mainGuildId": null,
  "logChannelId": null,

  "prefix": "!",
  "snippetPrefix": "!!",

  "status": "Message me for help!",
  "responseMessage": "Thank you for your message! Our mod team will reply to you here as soon as possible.",
  "closeMessage": null,

  "newThreadCategoryId": null,
  "mentionRole": "here",
  "pingOnBotMention": true,

  "inboxServerPermission": null,
  "alwaysReply": false,
  "alwaysReplyAnon": false,
  "useNicknames": false,
  "ignoreAccidentalThreads": false,
  "threadTimestamps": false,
  "allowMove": false,
  "typingProxy": false,
  "typingProxyReverse": false,

  "enableGreeting": false,
  "greetingMessage": null,
  "greetingAttachment": null,

  "requiredAccountAge": null, // In hours
  "accountAgeDeniedMessage": "Your Discord account is not old enough to contact modmail.",

  "relaySmallAttachmentsAsAttachments": false,
  "smallAttachmentLimit": 1024 * 1024 * 2,

  "port": 8890,
  "url": null,

  "dbDir": path.join(__dirname, '..', 'db'),
  "knex": null,

  "logDir": path.join(__dirname, '..', 'logs'),
};

const required = ['token', 'mailGuildId', 'mainGuildId', 'logChannelId'];

const finalConfig = Object.assign({}, defaultConfig);

for (const [prop, value] of Object.entries(userConfig)) {
  if (! defaultConfig.hasOwnProperty(prop)) {
    throw new Error(`Invalid option: ${prop}`);
  }

  finalConfig[prop] = value;
}

// Default knex config
if (! finalConfig['knex']) {
  finalConfig['knex'] = {
    client: 'sqlite',
      connection: {
      filename: path.join(finalConfig.dbDir, 'data.sqlite'),
      "token":process.env.token,
    },
    useNullAsDefault: true
  };
}

// Make sure migration settings are always present in knex config
Object.assign(finalConfig['knex'], {
  migrations: {
    directory: path.join(finalConfig.dbDir, 'migrations')
  }
});

// Make sure all of the required config options are present
for (const opt of required) {
  if (! finalConfig[opt]) {
    console.error(`Missing required config.json value: ${opt}`);
    process.exit(1);
  }
}

if (finalConfig.smallAttachmentLimit > 1024 * 1024 * 8) {
  finalConfig.smallAttachmentLimit = 1024 * 1024 * 8;
  console.log('[WARN] smallAttachmentLimit capped at 8MB');
}

// Make sure mainGuildId is internally always an array
if (! Array.isArray(finalConfig['mainGuildId'])) {
  finalConfig['mainGuildId'] = [finalConfig['mainGuildId']];
}
module.exports = finalConfig;

#6

I can’t see where you’re using Eris in that code, but here’s an example that uses the token using Eris: https://glitch.com/edit/#!/discord-bot-example?path=server.js:1:0


#7

Hum but my code it isn’t like that @Gareth


#8

Let us know the project name and we’ll take a look. If your project is private, either make it public temporarily or DM me a join link so we can view the code. Thanks.


#9

@Gareth name: modmailclyde


#10

You shouldn’t share join links publicly as then anyone can join your project, which is why I requested it via DM. However, the project linked is a default node-app without any Discord bot code in it, so it looks like it was for a different project.


#11

I created it for host my discord bot (this website helped me : https://anidiots.guide/other-guides/hosting-on-glitch)


#12

The project didn’t contain any bot code, like you pasted above for config.js. It looks like you pasted a join link for the incorrect project. You’ll need to send me a join link for the correct project. Please send it via DM - click on my name and use the send message option. Otherwise, mail me it at gareth@glitch.com. Thanks.


#13

I’m sending an email


#14

The join link you shared is has been invalidated since you shared it in a public place. Please send a new join link to the correct project, thanks.