简体   繁体   中英

Javascript Discord Bot giving code reference errors when ran

I've been working on a discord bot lately, this is my first time coding in general, and I thought Javascript would be easier than other options I might have had. Right now, I'm struggling through reading error after error.

Anyways, lets get to the question at hand. Currently, the code is as follows:

const Discord = require("discord.js");
const client = new Discord.Client();
const commando = require('discord.js-commando');
const bot = new commando.Client();
const prefix="^";

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  let short = msg.content.toLowerCase()

  let GeneralChannel = server.channels.find("General", "Bot")
if (msg.content.startsWith( prefix + "suggest")) {
  var args = msg.content.substring(8)
  msg.guild.channels.get(GeneralChannel).send("http\n SUGGESTION:" + msg.author.username + " suggested the following: " + args + "")
  msg.delete();
  msg.channel.send("Thank you for your submission!")
  }
});

When I ran said code, it returned an error that (I think) basically told me that "server" in let GeneralChannel = server.channels.find("General", "Bot") was undefined. My problem is, I dont actually know how to define server. I'm assuming that when I define server to it, it will also tell me I need to define channel and find, though I'm not sure.

Thanks in advance :)

First of all, why are you using both let and var ? Anyways, as the error says, server is not defined. The client doesn't know what server you're referring to. That's where your msg object comes in, it has a property guild which is the server.

msg.guild;

Secondly, what are you trying to achieve with let GeneralChannel = server.channels.find("General", "Bot") ? the find method for arrays takes a function. Are you trying to look for the channel with the name "General" or something? If so, better to use the id of the channel that way, you can use a channel from any server the bot is in (in case you're trying to send all suggestions to a specific channel on a different server).

let generalChannel = client.channels.find(chan => {
    return chan.id === "channel_id"
})
//generalChannel will be undefined if there is no channel with the id

If you're trying to send

Going by that assumption, your code can then be re-written to:

const Discord = require("discord.js");
const client = new Discord.Client();
const commando = require('discord.js-commando');
const bot = new commando.Client();
const prefix="^";

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
    let short = msg.content.toLowerCase();

    if (msg.content.startsWith( prefix + "suggest")) {
        let generalChannel = client.channels.find(chan => {
            return chan.id === 'channel_id';
        });

        let args = msg.content.substring(8);

        generalChannel.send("http\n SUGGESTION: " + msg.author.username + " suggested the following: " + args + "");
        msg.delete();
        msg.channel.send("Thank you for your submission!")
    }
});

Not that scope is a concern in this instance but it's worth noting that 'let 'defines a local variable while 'var' defines a global variable. There is a difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM