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

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

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

問題描述

我想通過將所有較長的內容放在另一個文件中來使我的代碼看起來干凈且易于理解.我有我的主文件(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);
        }
    }

我希望它發送嵌入,但是當我輸入命令時,沒有任何反應,也沒有打印任何錯誤.

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

推薦答案

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

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 中,您創建了一個新客戶端.如果你只有這個命令,它不會導致任何問題,因為 client 沒有在你的代碼中使用,但是當你需要它時它不會起作用,因為它必須與在主文件中.您有兩種可能的解決方案:將您的客戶端設置為全局或需要主模塊.如果您的機器人不必在公共包中發布,那么您可以保存 global.client = client;,然后在每個其他文件中作為 client 訪問它.另一種方法是從主模塊 (module.exports = {client};) 導出客戶端,然后在 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 中,您正在導出 help function,所以當您調用它時index.js 你必須使用括號并將消息作為參數傳遞.試試這樣的:

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.

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

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

相關文檔推薦

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(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 国产在线区| 国产麻豆乱码精品一区二区三区 | 日韩在线视频播放 | 午夜在线小视频 | 中文字幕日韩欧美 | 久久国产高清 | 国产成人免费 | 精品久久香蕉国产线看观看亚洲 | 国产精品免费一区二区三区四区 | 国产精品久久av | 免费观看黄a一级视频 | 在线中文字幕av | 亚洲一本 | 成人午夜精品 | 四虎最新| 乱码av午夜噜噜噜噜动漫 | 久久精品日产第一区二区三区 | 日韩欧美中文在线 | 欧美日韩亚洲视频 | 欧美日韩国产精品一区二区 | 成年人在线视频 | 国产精品亚洲精品 | 91福利网| 精品久久电影 | 99热热| 看一级毛片视频 | 精品一区在线看 | 亚洲成人一区二区 | 97国产精品视频人人做人人爱 | 亚洲一级在线 | 五月天婷婷丁香 | 中文字幕在线观看一区 | 日韩一及片 | 久久久毛片 | 一区二区三区视频在线观看 | 天天干视频 | 亚洲国产第一页 | 国产免费拔擦拔擦8x高清 | a级在线免费视频 | 中文字幕国产精品 | 亚洲欧美日韩激情 |