2017-01-19 15 views
0

私はDiscord Botを書きました。これはC#で開発されました。私のコマンドリストが満たされ、コマンド値がこのリストを受け取ります。しかし、コマンドはそれを呼び出すときにコードを実行しません。Discord Bot(C#を使用)はコマンドを実行しません

私のプレフィックスcharは '!'です。その後にコマンドが続きます。私の基本クラスは次のようになります:

public class Bot 
    { 
     string token = "#######################"; // my Token 

     CommandService command; // The commands holder 
     EventController eventController = new EventController(); // event class 
     CommandController commandController = new CommandController(); // commands class 

     public Bot() 
     { 
      var client = new DiscordClient(); // my client 

      client = new DiscordClient(input => 
      { 
       input.LogLevel = LogSeverity.Info; 
       input.LogHandler = Log; 
      }); 

      client.UsingCommands(input => 
      { 
       input.PrefixChar = '!';    // the prefix char to call commands 
       input.AllowMentionPrefix = true; 
      }); 

      eventController.HandleEvents(client); // reference to events 

      command = client.GetService<CommandService>(); 

      commandController.HandleCommands(command, client); // reference to commands 

      client.ExecuteAndWait(async() => 
      { 
       while (true) 
       { 
        await client.Connect(token, TokenType.Bot); 
        break; 
       } 
      }); 
     } 

     private void Log(object sender, LogMessageEventArgs e) 
     { 
      Console.WriteLine(e.Message); 
     } 

私はコードをeventControllerとcommandControllerという2つのクラスに分けました。 commandControllerは関連するものです。私は「TEST0」または「UseHelp()」のようなコマンドを呼び出すとき

private List<Tuple<string, string, string>> commandList = new List<Tuple<string, string, string>>(); // the List holding all commands 

     public void HandleCommands(CommandService command, DiscordClient client) 
     { 
      FillCommandList(); // Fill the List with the commands 

      foreach (Tuple<string, string, string> tuple in commandList) 
      { 
       command.CreateCommand('!' + tuple.Item1).Do(async (e) => 
       { 
        await e.Channel.SendMessage(tuple.Item2); // Create all commands from the List 
       }); 
      } 
     } 

     private void Add(string commandName, string textToReturn, string commandDescription) 
     { 
      commandList.Add(new Tuple<string, string, string>(commandName, textToReturn, commandDescription)); // Method to lower the mass of code 
     } 

     private void FillCommandList() 
     { 
      Add("test0", "success0", "info0"); // commands for testing 
      Add("test1", "success1", "info1"); 
      Add("test2", "success2", "info2"); 
      Add("test3", "success3", "info3"); 

      Add("help", UseHelp(), "List all Commands"); // call the help 
     } 

     private string UseHelp() 
     { 
      string commandItems = ""; 
      foreach (Tuple<string, string, string> tuple in commandList) 
      { 
       commandItems += "- !" + tuple.Item1 + " - " + tuple.Item3 + "\r\n"; // List all commands 
      } 
      return commandItems; 
     } 

だからコマンドは、文字列のコンテンツを受信します。

私のコマンドクラスは、この見えます。 5つのコマンドすべてがボットに一覧表示されます。しかし、Discordでコマンドを使うと、Botは応答しません。

それが接続されており、 "命令" データが満たされる...

答えて

0

まず、これを見て取る:

client.UsingCommands(input => 
      { 
       input.PrefixChar = '!';    // the prefix char to call commands 
       input.AllowMentionPrefix = true; 
      }); 

を、今、この: command.CreateCommand('!' + tuple.Item1)

不和で.net、PrefixCharをすでに作成している場合、PrefixCharは常にデフォルトではcommand.CreateCommand()の引数の内側に表示されます。したがって、別の場所に'!'を配置する必要はありません。その場合は、!!test0を使用してコマンドを呼び出す必要があります。単純に、システムが自動的に前に引数command.CreateCommand()の接頭辞を自動的に追加したので、それを扱います。

修正するには:command.CreateCommand('!' + tuple.Item1)の先頭の文字引数'!'を削除します。ボットを!test0などと呼んでテストしてください。うまくいくはずです。

関連する問題