[docs] rewrite commands, update samples

This commit is contained in:
Christopher F
2016-12-03 16:41:45 -05:00
parent 2cae0bda79
commit 779aaffbf4
14 changed files with 211 additions and 136 deletions

View File

@@ -8,6 +8,7 @@ public class Program
{
private CommandService commands;
private DiscordSocketClient client;
private DependencyMap map;
static void Main(string[] args) => new Program().Start().GetAwaiter().GetResult();
@@ -18,6 +19,8 @@ public class Program
string token = "bot token here";
map = new DependencyMap();
await InstallCommands();
await client.LoginAsync(TokenType.Bot, token);
@@ -25,13 +28,12 @@ public class Program
await Task.Delay(-1);
}
public async Task InstallCommands()
{
// Hook the MessageReceived Event into our Command Handler
client.MessageReceived += HandleCommand;
// Discover all of the commands in this assembly and load them.
await commands.LoadAssembly(Assembly.GetEntryAssembly());
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
}
public async Task HandleCommand(SocketMessage messageParam)
{
@@ -41,16 +43,14 @@ public class Program
// 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 if it starts with '!' or a mention prefix
if (message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))
{
// Create a Command Context
var context = new CommandContext(client, message);
// Execute the command. (result does not indicate a return value,
// rather an object stating if the command executed succesfully)
var result = await _commands.Execute(context, argPos);
if (!result.IsSuccess)
await msg.Channel.SendMessageAsync(result.ErrorReason);
}
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
// Create a Command Context
var context = new CommandContext(client, message);
// Execute the command. (result does not indicate a return value,
// rather an object stating if the command executed succesfully)
var result = await commands.ExecuteAsync(context, argPos, map);
if (!result.IsSuccess)
await msg.Channel.SendMessageAsync(result.ErrorReason);
}
}

View File

@@ -2,28 +2,30 @@ using Discord;
using Discord.Commands;
using Discord.WebSocket;
[Module]
public class ModuleA
public class ModuleA : ModuleBase
{
private DiscordSocketClient client;
private ISelfUser self;
private readonly DatabaseService _database;
public ModuleA(IDiscordClient c, ISelfUser s)
public ModuleA(DatabaseService database)
{
if (!(c is DiscordSocketClient)) throw new InvalidOperationException("This module requires a DiscordSocketClient");
client = c as DiscordSocketClient;
self = s;
_database = database;
}
public async Task ReadFromDb()
{
var x = _database.getX();
await ReplyAsync(x);
}
}
public class ModuleB
{
private IDiscordClient client;
private CommandService commands;
private CommandService _commands;
private NotificationService _notification;
public ModuleB(CommandService c, IDependencyMap m)
public ModuleB(CommandService commands, IDependencyMap map)
{
commands = c;
client = m.Get<IDiscordClient>();
_commands = commands;
_notification = map.Get<NotificationService>();
}
}

View File

@@ -0,0 +1,6 @@
using Discord.Commands;
public class InfoModule : ModuleBase
{
}

View File

@@ -1,6 +1,6 @@
public async Task SendMessageToChannel(ulong ChannelId)
{
var channel = _client.GetChannel(ChannelId) as ISocketMessageChannel;
var channel = _client.GetChannel(ChannelId) as SocketMessageChannel;
await channel?.SendMessageAsync("aaaaaaaaahhh!!!")
/* ^ This question mark is used to indicate that 'channel' may sometimes be null, and in cases that it is null, we will do nothing here. */
}

View File

@@ -1,15 +1,18 @@
[Group("admin")]
public class AdminModule : ModuleBase
{
[Group("mod")]
public class ModerationGroup : ModuleBase
[Group("clean")]
public class CleanModule : ModuleBase
{
// ~admin mod ban foxbot#0282
[Command("ban")]
public async Task Ban(IGuildUser user) { }
}
// ~admin clean 15
[Command]
public async Task Default(int count = 10) => Messages(count);
// ~admin clean 100
[Command("clean")]
public async Task Clean(int count = 100) { }
// ~admin clean messages 15
[Command("messages")]
public async Task Messages(int count = 10) { }
}
// ~admin ban foxbot#0282
[Command("ban")]
public async Task Ban(IGuildUser user) { }
}

View File

@@ -13,7 +13,11 @@ public class Program
LogLevel = LogSeverity.Info
});
_client.Log += (message) => Console.WriteLine($"{message.ToString()}");
_client.Log += (message) =>
{
Console.WriteLine($"{message.ToString()}");
return Task.CompletedTask;
};
await _client.LoginAsync(TokenType.Bot, "bot token");
}

View File

@@ -1,10 +0,0 @@
public class InfoModule : ModuleBase
{
// Constrain this command to Guilds
[RequireContext(ContextType.Guild)]
public async Task Whois(IGuildUser user) { }
// Constrain this command to either Guilds or DMs
[RequireContext(ContextType.Guild | ContextType.DM)]
public async Task Info() { }
}

View File

@@ -1,4 +1,4 @@
// Defining the Precondition
// (Note: This precondition is obsolete, it is recommended to use the RequireOwnerAttribute that is bundled with Discord.Commands)
// Inherit from PreconditionAttribute
public class RequireOwnerAttribute : PreconditionAttribute

View File

@@ -1,6 +0,0 @@
public class AdminModule : ModuleBase
{
[Command("ban")]
[RequirePermission(GuildPermission.BanMembers)]
public async Task Ban(IGuildUser target) { }
}

View File

@@ -1,3 +1,4 @@
// Note: This example is obsolete, a boolean type reader is bundled with Discord.Commands
using Discord;
using Discord.Commands;