2017-12-22 24 views
0

すべてのコマンドを自分のクラスの.csファイルに入れようとしています。起動とコマンドが同じファイルにあるので、ファイルが混乱します。他のほとんどの人がこれを最初から持っていたようですが、私はそれらのひとりではありませんでした。ここで私は今持っているものです。Discord.NET 1.0コマンドを新しい.csファイルに入れる方法

スタートアップ:

using System; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Reflection; 
using System.Diagnostics; 

using Discord; 
using Discord.WebSocket; 
using Discord.Commands; 

using Microsoft.Extensions.DependencyInjection; 

namespace LockBot__1._0_ 
{ 

    public class Program 
    { 
     private CommandService _commands; 
     private DiscordSocketClient _client; 
     private IServiceProvider _services; 

     public static void Main(string[] args) 
      => new Program().StartAsync().GetAwaiter().GetResult(); 

     public async Task StartAsync() 
     { 
      _client = new DiscordSocketClient(); 
      _commands = new CommandService(); 

      _services = new ServiceCollection() 
       .AddSingleton(_client) 
       .AddSingleton(_commands) 
       .BuildServiceProvider(); 

      await InstallCommandsAsync(); 

      string token = "SomeToken"; 
      await _client.LoginAsync(TokenType.Bot, token); 
      await _client.StartAsync(); 

      await Task.Delay(-1); 
     } 

     public async Task InstallCommandsAsync() 
     { 
      _client.Ready += SetGamePlayAsync; 
      _client.MessageReceived += HandleCommandAsync; 
      await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); 
     } 

     private async Task HandleCommandAsync(SocketMessage messageParam) 
     { 
      var message = messageParam as SocketUserMessage; 
      if (message == null) return; 

      int argPos = 0; 
      if (!(message.HasCharPrefix('~', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return; 

      var context = new SocketCommandContext(_client, message); 

      var result = await _commands.ExecuteAsync(context, argPos, _services); 
      if (!result.IsSuccess) 
       await context.Channel.SendMessageAsync(result.ErrorReason); 
     } 

     public async Task SetGamePlayAsync() 
     { 
      await _client.SetGameAsync("locksteel.me | ~help"); 
     } 
    } 

コマンド(短縮):

[Name("Miscellaneous")] 
    public class Misc : ModuleBase<SocketCommandContext> 
    { 
     Random rand = new Random(); 

     [Command("flip")] 
     [Name("flip")] 
     [Summary("Flips a coin.")] 
     [Alias("coin", "coinflip")] 
     public async Task FlipAsync() 
     { 
      string flipToSend; 
      int flip = rand.Next(1, 3); 
      if ((flip == 1) && !(flip == 2)) 
      { 
       flipToSend = "Heads."; 
      } 
      else if ((flip == 2) && !(flip == 1)) 
      { 
       flipToSend = "Tails."; 
      } 
      else 
      { 
       Debug.WriteLine("~flip error: error establishing random number"); 
       return; 
      } 
      await Context.Channel.SendMessageAsync(flipToSend); 
      Debug.WriteLine("~flip executed successfully in " + Context.Guild.Name); 
     } 
    } 

注:私はまだ確執ボット作りにさえmoresoコーディングとにかなり新しいですので、それを覚えておいてください。私のProgram.csで

答えて

0

私はあなたに似ています:次のように基づいている場合、私のコマンドファイルのそれぞれが作動しその後

using Discord; 
using Discord.Commands; 
using Discord.WebSocket; 
using Microsoft.Extensions.DependencyInjection; 
using System; 
using System.Reflection; 
using System.Threading.Tasks; 

namespace Discordbot_Techxas 
{ 
    class Program 
    { 
     static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult(); 

     private DiscordSocketClient _client; 
     private CommandService _commands; 
     private IServiceProvider _services; 

     public async Task RunBotAsync() 
     { 
      _client = new DiscordSocketClient(); 
      _commands = new CommandService(); 

      _services = new ServiceCollection() 
       .AddSingleton(_client) 
       .AddSingleton(_commands) 
       .BuildServiceProvider(); 
      //You need to add the Token for your Discord Bot to the code below. 
      string botToken = "YOUR CODE HERE"; 

      //event subscriptions 
      _client.Log += Log; 
      _client.UserJoined += AnnounceUserJoined; 

      await RegisterCommandsAsync(); 

      await _client.LoginAsync(TokenType.Bot, botToken); 

      await _client.StartAsync(); 

      await Task.Delay(-1); 
     } 

     private async Task AnnounceUserJoined(SocketGuildUser user) 
     { 
      var guild = user.Guild; 
      var channel = guild.DefaultChannel; 
      await channel.SendMessageAsync($"Welcome, {user.Mention}"); 
     } 

     private Task Log(LogMessage arg) 
     { 
      Console.WriteLine(arg); 

      return Task.CompletedTask; 
     } 

     public async Task RegisterCommandsAsync() 
     { 
      _client.MessageReceived += HandleCommandAsync; 

      await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); 
     } 

     private async Task HandleCommandAsync(SocketMessage arg) 
     { 
      var message = arg as SocketUserMessage; 

      if (message is null || message.Author.IsBot) return; 

      int argPos = 0; 

      if (message.HasStringPrefix("!", ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos)) 
      { 
       var context = new SocketCommandContext(_client, message); 

       var result = await _commands.ExecuteAsync(context, argPos, _services); 

       if (!result.IsSuccess) 
        Console.WriteLine(result.ErrorReason); 
      } 
     } 
    } 
} 

。ですから、!pingは次のようになります。

その後、調整する各コマンドのモジュールフォルダに新しいクラスアイテムを作成します。私は新しくて学びます。しかし、私が作成しているサンドボックスのボットと、そこにたどりついたリソースを見ることができます:https://github.com/Thanory/Discordbot_Techxas

関連する問題