diff --git a/Tools/BiblioTech/BiblioTech.csproj b/Tools/BiblioTech/BiblioTech.csproj index f02677b..ab4ef7e 100644 --- a/Tools/BiblioTech/BiblioTech.csproj +++ b/Tools/BiblioTech/BiblioTech.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/Tools/BiblioTech/CommandHandler.cs b/Tools/BiblioTech/CommandHandler.cs new file mode 100644 index 0000000..ef8330a --- /dev/null +++ b/Tools/BiblioTech/CommandHandler.cs @@ -0,0 +1,62 @@ +using Discord.Commands; +using Discord.WebSocket; +using System.Reflection; + +namespace BiblioTech +{ + public class CommandHandler + { + private readonly DiscordSocketClient client; + private readonly CommandService commands; + + // Retrieve client and CommandService instance via ctor + public CommandHandler(DiscordSocketClient client, CommandService commands) + { + this.commands = commands; + this.client = client; + } + + public async Task InstallCommandsAsync() + { + // Hook the MessageReceived event into our command handler + client.MessageReceived += HandleCommandAsync; + + // Here we discover all of the command modules in the entry + // assembly and load them. Starting from Discord.NET 2.0, a + // service provider is required to be passed into the + // module registration method to inject the + // required dependencies. + // + // If you do not use Dependency Injection, pass null. + // See Dependency Injection guide for more information. + await commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), + services: null); + } + + private async Task HandleCommandAsync(SocketMessage messageParam) + { + // Don't process the command if it was a system message + var message = messageParam as SocketUserMessage; + if (message == null) return; + + // Create a number to track where the prefix ends and the command begins + int argPos = 0; + + // Determine if the message is a command based on the prefix and make sure no bots trigger commands + if (!(message.HasCharPrefix('!', ref argPos) || + message.HasMentionPrefix(client.CurrentUser, ref argPos)) || + message.Author.IsBot) + return; + + // Create a WebSocket-based command context based on the message + var context = new SocketCommandContext(client, message); + + // Execute the command with the command context we just + // created, along with the service provider for precondition checks. + await commands.ExecuteAsync( + context: context, + argPos: argPos, + services: null); + } + } +} diff --git a/Tools/BiblioTech/HelloWorldModule.cs b/Tools/BiblioTech/HelloWorldModule.cs new file mode 100644 index 0000000..f0e711b --- /dev/null +++ b/Tools/BiblioTech/HelloWorldModule.cs @@ -0,0 +1,14 @@ +using Discord.Commands; + +namespace BiblioTech +{ + public class HelloWorldModule : ModuleBase + { + [Command("say")] + [Summary("Echoes a message.")] + public Task SayAsync([Remainder][Summary("The text to echo")] string echo) + { + return ReplyAsync("I say: " + echo); + } + } +} diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index 240f9c7..455618a 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -1,8 +1,29 @@ -public class Program -{ - public static void Main(string[] args) - { - Console.WriteLine("Starting Codex Discord bot BiblioTech..."); +using Discord; +using Discord.WebSocket; +public class Program +{ + public static Task Main(string[] args) => new Program().MainAsync(); + + private DiscordSocketClient client; + + public async Task MainAsync() + { + client = new DiscordSocketClient(); + + client.Log += Log; + + // You can assign your bot token to a string, and pass that in to connect. + // This is, however, insecure, particularly if you plan to have your code hosted in a public repository. + var token = "token"; + + await client.LoginAsync(TokenType.Bot, token); + await client.StartAsync(); + await Task.Delay(-1); + } + private Task Log(LogMessage msg) + { + Console.WriteLine(msg.ToString()); + return Task.CompletedTask; } } \ No newline at end of file