Update Documentation to be compatible with Beta2

this one took a while
This commit is contained in:
Christopher F
2016-10-15 17:28:36 -04:00
parent 8c8ac47887
commit 77342903bb
17 changed files with 205 additions and 146 deletions

View File

@@ -33,21 +33,21 @@ public class Program
// Discover all of the commands in this assembly and load them.
await commands.LoadAssembly(Assembly.GetEntryAssembly());
}
public async Task HandleCommand(IMessage paramMessage)
public async Task HandleCommand(SocketMessage messageParam)
{
// Cast paramMessage to an IUserMessage, return if the message was a System message.
var msg = paramMessage as IUserMessage;
if (msg == null) return;
// Internal integer, marks where the command begins
// 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;
// Get the current user (used for Mention parsing)
var currentUser = await client.GetCurrentUserAsync();
// Determine if the message is a command, based on if it starts with '!' or a mention prefix
if (msg.HasCharPrefix('!', ref argPos) || msg.HasMentionPrefix(currentUser, ref argPos))
if (message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))
{
// Execute the command. (result does not indicate a return value,
// 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(msg, argPos);
var result = await _commands.Execute(context, argPos);
if (!result.IsSuccess)
await msg.Channel.SendMessageAsync(result.ErrorReason);
}

View File

@@ -1,6 +1,7 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using foxboat.Services;
public class Commands
{
@@ -8,10 +9,16 @@ public class Commands
{
var commands = new CommandService();
var map = new DependencyMap();
map.Add<IDiscordClient>(client);
var self = await client.GetCurrentUserAsync();
map.Add<ISelfUser>(self);
map.Add(client);
map.Add(commands);
await commands.LoadAssembly(Assembly.GetCurrentAssembly(), map);
}
// In ConfigureServices, we will inject the Dependency Map with
// all of the services our client will use.
public Task ConfigureServices(IDependencyMap map)
{
map.Add(new NotificationService(map));
map.Add(new DatabaseService(map));
}
// ...
}

View File

@@ -1,5 +1,5 @@
public async Task ChangeAvatar()
{
var fileStream = new FileStream("./newAvatar.png", FileMode.Open);
await (await _client.GetCurrentUserAsync()).ModifyAsync(x => x.Avatar = fileStream);
await _client.CurrentUser.ModifyAsync(x => x.Avatar = fileStream);
}

View File

@@ -1,4 +0,0 @@
public async Task MessageReceived(IMessage msg)
{
var guild = (msg.Channel as IGuildChannel)?.Guild;
}

View File

@@ -1,6 +1,6 @@
public async Task SendMessageToChannel(ulong ChannelId)
{
var channel = await _client.GetChannelAsync(ChannelId) as IMessageChannel;
var channel = _client.GetChannel(ChannelId) as ISocketMessageChannel;
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,15 @@
[Module("admin")]
public class AdminModule
[Group("admin")]
public class AdminModule : ModuleBase
{
[Group("mod")]
public class ModerationGroup
public class ModerationGroup : ModuleBase
{
// ~admin mod ban foxbot#0282
[Command("ban")]
public async Task Ban(IUserMessage msg, IGuildUser user) { }
public async Task Ban(IGuildUser user) { }
}
// ~admin clean 100
[Command("clean")]
public async Task Clean(IUserMessage msg, int count = 100) { }
public async Task Clean(int count = 100) { }
}

View File

@@ -1,29 +1,29 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
// Create a module with no prefix
[Module]
public class Info
public class Info : ModuleBase
{
// ~say hello -> hello
[Command("say"), Summary("Echos a message.")]
public async Task Say(IUserMessage msg,
[Unparsed, Summary("The text to echo")] string echo)
public async Task Say([Unparsed, Summary("The text to echo")] string echo)
{
await msg.Channel.SendMessageAsync(echo);
// ReplyAsync is a method on ModuleBase
await ReplyAsync(echo);
}
}
// Create a module with the 'sample' prefix
[Module("sample")]
public class Sample
[Group("sample")]
public class Sample : ModuleBase
{
// ~sample square 20 ->
// ~sample square 20 -> 400
[Command("square"), Summary("Squares a number.")]
public async Task Square(IUserMessage msg,
[Summary("The number to square.")] int num)
public async Task Square([Summary("The number to square.")] int num)
{
await msg.Channel.SendMessageAsync($"{num}^2 = {Math.Pow(num, 2)}");
// We can also access the channel from the Command Context.
await Context.Channel.SendMessageAsync($"{num}^2 = {Math.Pow(num, 2)}");
}
// ~sample userinfo --> foxbot#0282
@@ -34,10 +34,9 @@ public class Sample
// ~sample whois 96642168176807936 --> Khionu#8708
[Command("userinfo"), Summary("Returns info about the current user, or the user parameter, if one passed.")]
[Alias("user", "whois")]
public async Task UserInfo(IUserMessage msg,
[Summary("The (optional) user to get info for")] IUser user = null)
public async Task UserInfo([Summary("The (optional) user to get info for")] IUser user = null)
{
var userInfo = user ?? await Program.Client.GetCurrentUserAsync();
await msg.Channel.SendMessageAsync($"{userInfo.Username}#{userInfo.Discriminator}");
var userInfo = user ?? Context.Client.CurrentUser;
await ReplyAsync($"{userInfo.Username}#{userInfo.Discriminator}");
}
}

View File

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

View File

@@ -1,16 +1,18 @@
// Defining the Precondition
// Specify that this precondition can target a Class (Module/Group) or Method (Command)
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
// Inherit from PreconditionAttribute
public class RequireOwnerAttribute : PreconditionAttribute
{
public readonly ulong OwnerId = 66078337084162048;
// Override the CheckPermissions method
public override Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance)
public override Task<PreconditionResult> CheckPermissions(CommandContext context, CommandInfo command, IDependencyMap map)
{
// If the author of the message is '66078337084162048', return success; otherwise fail.
return Task.FromResult(context.Author.Id == OwnerId ? PreconditionResult.FromSuccess() : PreconditionResult.FromError("You must be the owner of the bot."));
// Get the ID of the bot's owner
var ownerId = (await map.Get<DiscordSocketClient>().GetApplicationInfoAsync()).Owner.Id;
// If this command was executed by that user, return a success
if (context.User.Id == ownerId)
return PreconditionResult.FromSuccess();
// Since it wasn't, fail
else
return PreconditionResult.FromError("You must be the owner of the bot to run this command.");
}
}

View File

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

View File

@@ -3,7 +3,7 @@ using Discord.Commands;
public class BooleanTypeReader : TypeReader
{
public override Task<TypeReaderResult> Read(IUserMessage context, string input)
public override Task<TypeReaderResult> Read(CommandContext context, string input)
{
bool result;
if (bool.TryParse(input, out result))