久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

對機器人命令使用不同的文件

Use different file for bot commands(對機器人命令使用不同的文件)
本文介紹了對機器人命令使用不同的文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想通過將所有較長的內(nèi)容放在另一個文件中來使我的代碼看起來干凈且易于理解.我有我的主文件(index.js):

I want to keep my code clean looking and easier to understand, by putting all the longer stuff in another file. I have my main file (index.js):

 const discord = require('discord.js');
 require('dotenv').config()

 const token = process.env.botToken;
 const prefix = "s!";

 const cmds = require("./commands.js");

 var client = new discord.Client();

 client.on('ready', function(message) {});

 client.on('message', function(message) {
    if(message.author.equals(client.user) || !message.content.startsWith(prefix)) return;

    var args = message.content.substring(prefix.length).split(" ");

    switch (args[0].toLowerCase()) {
        case "help":
            cmds.help;
            break;
    }
 });

 client.login(token)

和我的其他文件夾(commands.js):

and my other folder (commands.js):

const discord = require('discord.js');
var client = new discord.Client();

    module.exports = {
        help: function(message) {
            var embed = new discord.RichEmbed()
                .addField("spyBot Commands", "If you get issues, dont be afraid to join us: http://discord.gg/3k6zGNF");
            message.channel.send(embed);
        }
    }

我希望它發(fā)送嵌入,但是當(dāng)我輸入命令時,沒有任何反應(yīng),也沒有打印任何錯誤.

I would like it to send the embed, but when I put the command in, nothing happens and no errors are printed.

推薦答案

我看到需要修復(fù)的兩個主要問題:
1:commands.js中的客戶端
2:主文件中的命令函數(shù)

I see two major things that need to be fixed:
1: the client in commands.js
2: the command function in the main file

1 - 在 commands.js 中,您創(chuàng)建了一個新客戶端.如果你只有這個命令,它不會導(dǎo)致任何問題,因為 client 沒有在你的代碼中使用,但是當(dāng)你需要它時它不會起作用,因為它必須與在主文件中.您有兩種可能的解決方案:將您的客戶端設(shè)置為全局或需要主模塊.如果您的機器人不必在公共包中發(fā)布,那么您可以保存 global.client = client;,然后在每個其他文件中作為 client 訪問它.另一種方法是從主模塊 (module.exports = {client};) 導(dǎo)出客戶端,然后在 commands.js 中請求主文件 (var{client} = require("./index.js");).

1 - In commands.js, you created a new client. If you have only this command, it won't cause any problem since client is not used in your code, but when you'll need it that won't work since it has to be the same as in the main file. You have two possible solutions: setting your client as global or requiring the main module. If your bot doesn't have to be published in a public package then you can save global.client = client;, and then access it as client in every other file. The alternative is to export the client from the main module (module.exports = {client};) and then require the main file in commands.js (var {client} = require("./index.js");).

2 - 在 commands.js 中,您正在導(dǎo)出 help function,所以當(dāng)您調(diào)用它時index.js 你必須使用括號并將消息作為參數(shù)傳遞.試試這樣的:

2 - In commands.js you're exporting a help function, so when you call it index.js you have to use parenthesis and pass the message as an argument. Try something like this:

//in the switch statement
case "help":
  cmds.help(message);
  break;

希望對您有所幫助,如果您還有其他問題,請告訴我.

I hope this can help you, let me know if you have any further question.

這篇關(guān)于對機器人命令使用不同的文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發(fā)出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時的歡迎消息)
主站蜘蛛池模板: 日本公妇乱偷中文字幕 | 久久国产一区二区三区 | 国产精品伦一区二区三级视频 | 亚洲天堂一区 | 日本精品网站 | 毛片免费在线观看 | 久久最新网址 | 日韩精品免费看 | 欧美亚洲视频 | 色一情一乱一乱一区91av | 日本中文字幕在线观看 | 欧美超碰在线 | 国产又色又爽又黄又免费 | 99精品久久久久久中文字幕 | 国产精品国产成人国产三级 | 日韩网站在线观看 | 免费av大片 | 亚洲第一av网站 | 在线观看黄色片 | 国产精品hd | 九九精品视频在线观看 | 麻豆精品在线播放 | 草少妇 | 伊人久久影院 | 精品视频免费在线观看 | 亚洲国产成人精品女人久久久 | 天天射天天 | 国产精品自拍第一页 | 99视频网站 | a亚洲天堂 | 亚洲天堂一区二区三区 | 在线不欧美| 中文字幕免费观看视频 | 国产三级在线观看视频 | 午夜影院福利 | 中文字幕在线观 | 久久精品久久精品 | 亚洲精品aaa | 中文在线观看免费视频 | 毛片网站大全 | 新香蕉视频 |