2017-11-18 3 views
1

もう一度、私のディスカードボットを作成するのに助けが必要です。私はボットにコマンドを使って埋め込みを送信させようとしています。私のコードは、このメッセージを送信するにはあまりにも複雑です。なぜなら、私はそのコマンドにその効果があるので、コマンドをどのように表示するのかということだけです。ボットを持ってプレイヤーコマンドを使って埋め込みを送信する

/embed [title]; [description] 

とタイトルと説明は次のようになり前はそう埋め込みの著者が現れるでしょう

setAuthor(`${message.author.username}`, message.author.displayAvatarURL) 

だろう。どのようにこれを行う上の任意のアイデア?

+0

私の答えが役に立ったら教えてください! –

答えて

0

まず、ここでは、コマンドでテキストを解析するために正規表現を使用する方法である。

case /^\/embed \[[\w ]+\]; \[[\w ]+\]$/.test(command): 
    sendEmbed(message); 
    break; 

この次のように分けることができますRegular Expression/^\/embed \[[\w ]+\]; \[[\w ]+\]$/):

  • 始まり/^と終了$/の部分は、文字列/コマンド全体を一致させることを意味しています。
  • /embedは正確なテキスト"embed"
  • \[[\w ]+\]と一致するタイトルおよび説明のために使用され、テキストは文字(大文字または小文字)、数字、アンダースコア、または空間である角括弧内のテキスト"[]"に一致されます。 "!""-"などの文字が必要な場合は、それらを追加する方法を示します。
  • .test(command)は、正規表現がコマンドのテキストと一致することをテストしており、ブール値(true/false)を返します。ご質問があれば、私に教えてください

    // set message listener 
    client.on('message', message => { 
        let command = message.content; 
    
        // match commands like /embed [title]; [description] 
        // first \w+ is for the title, 2nd is for description 
        if (/^\/embed \[[\w ]+\]; \[[\w ]+\]$/.test(command)) 
         sendEmbed(message); 
    }); 
    
    function sendEmbed(message) { 
        let command = message.content; 
        let channel = message.channel; 
        let author = message.author; 
    
        // get title string coordinates in command 
        let titleStart = command.indexOf('['); 
        let titleEnd = command.indexOf(']'); 
        let title = command.substr(titleStart + 1, titleEnd - titleStart - 1); 
    
        // get description string coordinates in command 
        // -> (start after +1 so we don't count '[' or ']' twice) 
        let descStart = command.indexOf('[', titleStart + 1); 
        let descEnd = command.indexOf(']', titleEnd + 1); 
        let description = command.substr(descStart + 1, descEnd - descStart - 1); 
    
        // next create rich embed 
        let embed = new Discord.RichEmbed({ 
         title: title, 
         description: description 
        }); 
    
        // set author based of passed-in message 
        embed.setAuthor(author.username, author.displayAvatarURL); 
    
        // send embed to channel 
        channel.send(embed); 
    } 
    

は、だから今は、正規表現は、あなたのメッセージ/コマンドリスナーにコードをチェックしてから送信埋め込む機能(私はsendEmbedそれを命名)このように呼び出すことを置きます!

関連する問題