diff --git a/.env.template b/.env.template deleted file mode 100644 index 2a5ad63..0000000 --- a/.env.template +++ /dev/null @@ -1 +0,0 @@ -TOKEN="" diff --git a/.gitignore b/.gitignore index 1aa827d..3f7d5af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -.env +_config.json *.swp *.*~ diff --git a/Commands/Help.cs b/Commands/Help.cs new file mode 100644 index 0000000..037cf78 --- /dev/null +++ b/Commands/Help.cs @@ -0,0 +1,88 @@ +using Discord; +using Discord.Commands; +using Microsoft.Extensions.Configuration; +using System.Linq; +using System.Threading.Tasks; + +namespace Example.Modules +{ + public class HelpModule : ModuleBase + { + private readonly CommandService _service; + private readonly IConfigurationRoot _config; + + public HelpModule(CommandService service, IConfigurationRoot config) + { + _service = service; + _config = config; + } + + [Command("help")] + public async Task HelpAsync() + { + string prefix = _config["prefix"]; + var builder = new EmbedBuilder() + { + Color = new Color(114, 137, 218), + Description = "These are the commands you can use" + }; + + foreach (var module in _service.Modules) + { + string description = null; + foreach (var cmd in module.Commands) + { + var result = await cmd.CheckPreconditionsAsync(Context); + if (result.IsSuccess) + description += $"{prefix}{cmd.Aliases.First()}\n"; + } + + if (!string.IsNullOrWhiteSpace(description)) + { + builder.AddField(x => + { + x.Name = module.Name; + x.Value = description; + x.IsInline = false; + }); + } + } + + await ReplyAsync("", false, builder.Build()); + } + + [Command("help")] + public async Task HelpAsync(string command) + { + var result = _service.Search(Context, command); + + if (!result.IsSuccess) + { + await ReplyAsync($"Sorry, I couldn't find a command like **{command}**."); + return; + } + + string prefix = _config["prefix"]; + var builder = new EmbedBuilder() + { + Color = new Color(114, 137, 218), + Description = $"Here are some commands like **{command}**" + }; + + foreach (var match in result.Commands) + { + var cmd = match.Command; + + builder.AddField(x => + { + x.Name = string.Join(", ", cmd.Aliases); + x.Value = $"Parameters: {string.Join(", ", cmd.Parameters.Select(p => p.Name))}\n" + + $"Summary: {cmd.Summary}"; + x.IsInline = false; + }); + } + + await ReplyAsync("", false, builder.Build()); + } + } +} \ No newline at end of file diff --git a/Commands/Status.cs b/Commands/Status.cs index 0704e5f..695e788 100644 --- a/Commands/Status.cs +++ b/Commands/Status.cs @@ -22,7 +22,6 @@ namespace dotbot.Commands [Group("sample")] public class Sample : ModuleBase { - // ~sample square 20 -> 400 [Command("square")] [Summary("Squares a number.")] public async Task SquareAsync([Summary("The number to square.")] int num) @@ -31,12 +30,6 @@ namespace dotbot.Commands await Context.Channel.SendMessageAsync($"{num}^2 = {Math.Pow(num, 2)}"); } - // ~sample userinfo --> foxbot#0282 - // ~sample userinfo @Khionu --> Khionu#8708 - // ~sample userinfo Khionu#8708 --> Khionu#8708 - // ~sample userinfo Khionu --> Khionu#8708 - // ~sample userinfo 96642168176807936 --> Khionu#8708 - // ~sample whois 96642168176807936 --> Khionu#8708 [Command("userinfo")] [Summary("Returns info about the current user, or the user parameter, if one passed.")] [Alias("user", "whois")] diff --git a/Program.cs b/Program.cs index de4142e..0f93841 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,7 @@ using Discord; using Discord.Commands; using Discord.WebSocket; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.Reflection; @@ -12,27 +13,39 @@ namespace dotbot { private CommandService _commands; private DiscordSocketClient _client; + private IConfigurationRoot _config; private IServiceProvider _services; public static void Main(string[] args) - => new Program().MainAsync().GetAwaiter().GetResult(); + => new Program().StartAsync().GetAwaiter().GetResult(); - public async Task MainAsync() + public async Task StartAsync() { + var builder = new ConfigurationBuilder() + .SetBasePath(AppContext.BaseDirectory) + .AddJsonFile("_config.json"); + _config = builder.Build(); + // get token from _config.json file + // if your bot isn't connecting, rename _config.example.json to _config.json + // and then place your bot token between the quotes + string token = _config["tokens:discord"]; + Console.WriteLine(token); + _client = new DiscordSocketClient(); _commands = new CommandService(); _client.Log += Log; - DotNetEnv.Env.Load(); - string token = Environment.GetEnvironmentVariable("TOKEN"); - _services = new ServiceCollection() .AddSingleton(_client) .AddSingleton(_commands) + .AddSingleton(_config) + .AddSingleton() .BuildServiceProvider(); - await InstallCommandsAsync(); + // install all commands from the assembly + _client.MessageReceived += HandleCommandAsync; + await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); await _client.LoginAsync(TokenType.Bot, token); await _client.StartAsync(); @@ -40,12 +53,6 @@ namespace dotbot await Task.Delay(-1); } - public async Task InstallCommandsAsync() - { - _client.MessageReceived += HandleCommandAsync; - await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); - } - private async Task HandleCommandAsync(SocketMessage arg) { var message = arg as SocketUserMessage; @@ -53,7 +60,7 @@ namespace dotbot int argPos = 0; - if (!(message.HasCharPrefix(';', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return; + if (!(message.HasStringPrefix(_config["prefix"], ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return; var context = new SocketCommandContext(_client, message); diff --git a/_config.example.json b/_config.example.json new file mode 100644 index 0000000..e91a91f --- /dev/null +++ b/_config.example.json @@ -0,0 +1,6 @@ +{ + "prefix": "!", + "tokens": { + "discord": "" + } +} diff --git a/dotbot.csproj b/dotbot.csproj index 5b72b04..be3a8b4 100644 --- a/dotbot.csproj +++ b/dotbot.csproj @@ -5,6 +5,13 @@ - + + + + + + + Always + \ No newline at end of file