Kaneki__

Untitled

Jun 29th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using System.Threading.Tasks;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Discord;
  6. using Discord.Commands;
  7. using Discord.WebSocket;
  8. using System.IO;
  9. using System.Net;
  10. using System.Text;
  11.  
  12. class Program
  13. {
  14. private readonly DiscordSocketClient _client;
  15. // Keep the CommandService and IServiceCollection around for use with commands.
  16. private readonly IServiceCollection _map = new ServiceCollection();
  17. private readonly CommandService _commands = new CommandService();
  18. // Program entry point
  19. static void Main(string[] args)
  20. {
  21. // Call the Program constructor, followed by the
  22. // MainAsync method and wait until it finishes (which should be never).
  23. new Program().MainAsync().GetAwaiter().GetResult();
  24. }
  25.  
  26. private Program()
  27. {
  28. _client = new DiscordSocketClient(new DiscordSocketConfig
  29. {
  30. // How much logging do you want to see?
  31. LogLevel = LogSeverity.Info,
  32.  
  33. // If you or another service needs to do anything with messages
  34. // (eg. checking Reactions, checking the content of edited/deleted messages),
  35. // you must set the MessageCacheSize. You may adjust the number as needed.
  36. //MessageCacheSize = 50,
  37.  
  38. // If your platform doesn't have native websockets,
  39. // add Discord.Net.Providers.WS4Net from NuGet,
  40. // add the `using` at the top, and uncomment this line:
  41. //WebSocketProvider = WS4NetProvider.Instance
  42. });
  43. }
  44.  
  45. // Example of a logging handler. This can be re-used by addons
  46. // that ask for a Func<LogMessage, Task>.
  47. private static Task Logger(LogMessage message)
  48. {
  49. var cc = Console.ForegroundColor;
  50. switch (message.Severity)
  51. {
  52. case LogSeverity.Critical:
  53. case LogSeverity.Error:
  54. Console.ForegroundColor = ConsoleColor.Red;
  55. break;
  56. case LogSeverity.Warning:
  57. Console.ForegroundColor = ConsoleColor.Yellow;
  58. break;
  59. case LogSeverity.Info:
  60. Console.ForegroundColor = ConsoleColor.White;
  61. break;
  62. case LogSeverity.Verbose:
  63. case LogSeverity.Debug:
  64. Console.ForegroundColor = ConsoleColor.DarkGray;
  65. break;
  66. }
  67. Console.WriteLine($"{DateTime.Now,-19} [{message.Severity,8}] {message.Source}: {message.Message}");
  68. Console.ForegroundColor = cc;
  69.  
  70. // If you get an error saying 'CompletedTask' doesn't exist,
  71. // your project is targeting .NET 4.5.2 or lower. You'll need
  72. // to adjust your project's target framework to 4.6 or higher
  73. // (instructions for this are easily Googled).
  74. // If you *need* to run on .NET 4.5 for compat/other reasons,
  75. // the alternative is to 'return Task.Delay(0);' instead.
  76. return Task.CompletedTask;
  77. }
  78. private async Task MainAsync()
  79. {
  80. // Subscribe the logging handler.
  81. _client.Log += Logger;
  82.  
  83. // Centralize the logic for commands into a seperate method.
  84. await InitCommands();
  85.  
  86. // Login and connect.
  87. await _client.LoginAsync(TokenType.Bot, "MzI5MzIyNTExNzE0MjIyMDgz.DDQxAA.");
  88. await _client.StartAsync();
  89.  
  90. // Wait infinitely so your bot actually stays connected.
  91. await Task.Delay(-1);
  92. }
  93. private IServiceProvider _services;
  94.  
  95. private async Task InitCommands()
  96. {
  97. // Repeat this for all the service classes
  98. // and other dependencies that your commands might need.
  99. // When all your required services are in the collection, build the container.
  100. // Tip: There's an overload taking in a 'validateScopes' bool to make sure
  101. // you haven't made any mistakes in your dependency graph.
  102. _services = _map.BuildServiceProvider();
  103. // Either search the program and add all Module classes that can be found.
  104. // Module classes *must* be marked 'public' or they will be ignored.
  105. await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
  106. // Or add Modules manually if you prefer to be a little more explicit:
  107.  
  108. // Subscribe a handler to see if a message invokes a command.
  109. _client.MessageReceived += HandleCommandAsync;
  110. }
  111.  
  112. private async Task HandleCommandAsync(SocketMessage arg)
  113. {
  114. // Bail out if it's a System Message.
  115. var msg = arg as SocketUserMessage;
  116. if (msg == null) return;
  117.  
  118. // Create a number to track where the prefix ends and the command begins
  119. int pos = 0;
  120. // Replace the '!' with whatever character
  121. // you want to prefix your commands with.
  122. // Uncomment the second half if you also want
  123. // commands to be invoked by mentioning the bot instead.
  124. //if (msg.HasCharPrefix('', ref pos) /* || msg.HasMentionPrefix(_client.CurrentUser, ref pos) */)
  125. {
  126. // Create a Command Context.
  127. var context = new SocketCommandContext(_client, msg);
  128.  
  129. // Execute the command. (result does not indicate a return value,
  130. // rather an object stating if the command executed succesfully).
  131. var result = await _commands.ExecuteAsync(context, pos, _services);
  132.  
  133. // Uncomment the following lines if you want the bot
  134. // to send a message if it failed (not advised for most situations).
  135. //if (!result.IsSuccess && result.Error != CommandError.UnknownCommand)
  136. // await msg.Channel.SendMessageAsync(result.ErrorReason);
  137. }
  138. }
  139. }
  140. public class Commands
  141. {
  142. iw4xbot.Commands BOTcommands = new iw4xbot.Commands();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment