Advertisement
Guest User

Program.cs

a guest
Apr 29th, 2021
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using Discord;
  2. using Discord.Commands;
  3. using Discord.WebSocket;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Threading.Tasks;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using SQLitePCL;
  11.  
  12. namespace PrayerBot
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args) => new Program().RunBot().GetAwaiter().GetResult();
  17.  
  18.         // Creating the necessary variables
  19.         public static DiscordSocketClient _client;
  20.         private CommandService _commands;
  21.         private IServiceProvider _services;
  22.  
  23.         private BotConfig config;
  24.  
  25.         // Runbot task
  26.         public async Task RunBot()
  27.         {
  28.             _client = new DiscordSocketClient(); // Define _client
  29.             _commands = new CommandService(); // Define _commands
  30.  
  31.             _services = new ServiceCollection() // Define _services
  32.                 .AddSingleton(_client)
  33.                 .AddSingleton(_commands)
  34.                 .BuildServiceProvider();
  35.  
  36.             config = JsonConvert.DeserializeObject<BotConfig>(File.ReadAllText("config.json"));
  37.  
  38.             string botToken = config.token; // Make a string for the token
  39.  
  40.             _client.Log += Log; // Logging
  41.  
  42.             await RegisterCommandsAsync(); // Call registercommands
  43.  
  44.             await _client.LoginAsync(TokenType.Bot, botToken); // Log into the bot user
  45.  
  46.             await _client.StartAsync(); // Start the bot user
  47.  
  48.             await _client.SetGameAsync(config.game); // Set the game the bot is playing
  49.  
  50.             await Task.Delay(-1); // Delay for -1 to keep the console window opened
  51.         }
  52.  
  53.         private async Task RegisterCommandsAsync()
  54.         {
  55.             _client.MessageReceived += HandleCommandAsync; // Messagerecieved
  56.  
  57.             await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), null); // Add module to _commands
  58.         }
  59.  
  60.         private Task Log(LogMessage arg) // Logging
  61.         {
  62.             Console.WriteLine(arg); // Print the log to Console
  63.             return Task.CompletedTask; // Return with completedtask
  64.         }
  65.  
  66.         private async Task HandleCommandAsync(SocketMessage arg)
  67.         {
  68.             string messageLower = arg.Content.ToLower(); // Convert the message to a Lower
  69.             var message = arg as SocketUserMessage; // Create a variable with the message as SocketUserMessage
  70.             if (message is null || message.Author.IsBot) return; // Checks if the message is empty or sent by a bot
  71.             int argumentPos = 0; // Sets the argpos to 0 (the start of the message)
  72.             if (message.HasStringPrefix(config.prefix, ref argumentPos) || message.HasMentionPrefix(_client.CurrentUser, ref argumentPos)) // If the message has the prefix at the start or starts with someone mentioning the bot
  73.             {
  74.                 var context = new SocketCommandContext(_client, message); // Create a variable called context
  75.                 var result = await _commands.ExecuteAsync(context, argumentPos, _services); // Create a veriable called result
  76.                 if (!result.IsSuccess) // If the result is unsuccessful
  77.                 {
  78.                     Console.WriteLine(result.ErrorReason); // Print the error to console
  79.                     await message.Channel.SendMessageAsync(result.ErrorReason); // Print the error to the channel where the error was caused (e.g "Unknown Command")
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     class BotConfig
  86.     {
  87.         public string token { get; set; }
  88.         public string prefix { get; set; }
  89.         public string game { get; set; }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement