Added CommandContext, fixed commands compile errors

This commit is contained in:
RogueException
2016-10-04 09:54:44 -03:00
parent 8aa1050b93
commit 708f9fe514
32 changed files with 187 additions and 148 deletions

View File

@@ -163,19 +163,19 @@ namespace Discord.Rest
}
//Users
public static async Task<RestGuildUser> GetUserAsync(IGuildChannel channel, BaseDiscordClient client,
public static async Task<RestGuildUser> GetUserAsync(IGuildChannel channel, IGuild guild, BaseDiscordClient client,
ulong id)
{
var model = await client.ApiClient.GetGuildMemberAsync(channel.GuildId, id);
if (model == null)
return null;
var user = RestGuildUser.Create(client, model);
var user = RestGuildUser.Create(client, guild, model);
if (!user.GetPermissions(channel).ReadMessages)
return null;
return user;
}
public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuildChannel channel, BaseDiscordClient client,
public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuildChannel channel, IGuild guild, BaseDiscordClient client,
ulong? froUserId = null, uint? limit = DiscordConfig.MaxUsersPerBatch)
{
return new PagedAsyncEnumerable<RestGuildUser>(
@@ -188,8 +188,11 @@ namespace Discord.Rest
};
if (info.Position != null)
args.AfterUserId = info.Position.Value;
var models = await client.ApiClient.GetGuildMembersAsync(channel.GuildId, args);
return models.Select(x => RestGuildUser.Create(client, x)).ToImmutableArray(); ;
var models = await guild.Discord.ApiClient.GetGuildMembersAsync(guild.Id, args);
return models
.Select(x => RestGuildUser.Create(client, guild, x))
.Where(x => x.GetPermissions(channel).ReadMessages)
.ToImmutableArray();
},
nextPage: (info, lastPage) =>
{

View File

@@ -16,24 +16,25 @@ namespace Discord.Rest
public IReadOnlyCollection<Overwrite> PermissionOverwrites => _overwrites;
public ulong GuildId { get; }
internal IGuild Guild { get; }
public ulong GuildId => Guild.Id;
public string Name { get; private set; }
public int Position { get; private set; }
internal RestGuildChannel(BaseDiscordClient discord, ulong id, ulong guildId)
internal RestGuildChannel(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, id)
{
GuildId = guildId;
Guild = guild;
}
internal new static RestGuildChannel Create(BaseDiscordClient discord, Model model)
internal static RestGuildChannel Create(BaseDiscordClient discord, IGuild guild, Model model)
{
switch (model.Type)
{
case ChannelType.Text:
return RestTextChannel.Create(discord, model);
return RestTextChannel.Create(discord, guild, model);
case ChannelType.Voice:
return RestVoiceChannel.Create(discord, model);
return RestVoiceChannel.Create(discord, guild, model);
default:
throw new InvalidOperationException("Unknown guild channel type");
}

View File

@@ -17,13 +17,13 @@ namespace Discord.Rest
public string Mention => MentionUtils.MentionChannel(Id);
internal RestTextChannel(BaseDiscordClient discord, ulong id, ulong guildId)
: base(discord, id, guildId)
internal RestTextChannel(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, guild, id)
{
}
internal new static RestTextChannel Create(BaseDiscordClient discord, Model model)
internal new static RestTextChannel Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestTextChannel(discord, model.Id, model.GuildId.Value);
var entity = new RestTextChannel(discord, guild, model.Id);
entity.Update(model);
return entity;
}
@@ -39,9 +39,9 @@ namespace Discord.Rest
=> ChannelHelper.ModifyAsync(this, Discord, func);
public Task<RestGuildUser> GetUserAsync(ulong id)
=> ChannelHelper.GetUserAsync(this, Discord, id);
=> ChannelHelper.GetUserAsync(this, Guild, Discord, id);
public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync()
=> ChannelHelper.GetUsersAsync(this, Discord);
=> ChannelHelper.GetUsersAsync(this, Guild, Discord);
public Task<RestMessage> GetMessageAsync(ulong id)
=> ChannelHelper.GetMessageAsync(this, Discord, id);

View File

@@ -16,13 +16,13 @@ namespace Discord.Rest
public int Bitrate { get; private set; }
public int UserLimit { get; private set; }
internal RestVoiceChannel(BaseDiscordClient discord, ulong id, ulong guildId)
: base(discord, id, guildId)
internal RestVoiceChannel(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, guild, id)
{
}
internal new static RestVoiceChannel Create(BaseDiscordClient discord, Model model)
internal new static RestVoiceChannel Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestVoiceChannel(discord, model.Id, model.GuildId.Value);
var entity = new RestVoiceChannel(discord, guild, model.Id);
entity.Update(model);
return entity;
}

View File

@@ -81,13 +81,13 @@ namespace Discord.Rest
{
var model = await client.ApiClient.GetChannelAsync(guild.Id, id).ConfigureAwait(false);
if (model != null)
return RestGuildChannel.Create(client, model);
return RestGuildChannel.Create(client, guild, model);
return null;
}
public static async Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync(IGuild guild, BaseDiscordClient client)
{
var models = await client.ApiClient.GetGuildChannelsAsync(guild.Id).ConfigureAwait(false);
return models.Select(x => RestGuildChannel.Create(client, x)).ToImmutableArray();
return models.Select(x => RestGuildChannel.Create(client, guild, x)).ToImmutableArray();
}
public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client,
string name)
@@ -96,7 +96,7 @@ namespace Discord.Rest
var args = new CreateGuildChannelParams(name, ChannelType.Text);
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args).ConfigureAwait(false);
return RestTextChannel.Create(client, model);
return RestTextChannel.Create(client, guild, model);
}
public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
string name)
@@ -105,7 +105,7 @@ namespace Discord.Rest
var args = new CreateGuildChannelParams(name, ChannelType.Voice);
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args).ConfigureAwait(false);
return RestVoiceChannel.Create(client, model);
return RestVoiceChannel.Create(client, guild, model);
}
//Integrations
@@ -155,7 +155,7 @@ namespace Discord.Rest
{
var model = await client.ApiClient.GetGuildMemberAsync(guild.Id, id).ConfigureAwait(false);
if (model != null)
return RestGuildUser.Create(client, model);
return RestGuildUser.Create(client, guild, model);
return null;
}
public static async Task<RestGuildUser> GetCurrentUserAsync(IGuild guild, BaseDiscordClient client)
@@ -166,7 +166,7 @@ namespace Discord.Rest
{
var args = new GetGuildMembersParams();
var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args).ConfigureAwait(false);
return models.Select(x => RestGuildUser.Create(client, x)).ToImmutableArray();
return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray();
}
public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client,
int days = 30, bool simulate = false)

View File

@@ -30,6 +30,7 @@ namespace Discord.Rest
public string VoiceRegionId { get; private set; }
public string IconId { get; private set; }
public string SplashId { get; private set; }
internal bool Available { get; private set; }
public ulong DefaultChannelId => Id;
public string IconUrl => API.CDN.GetGuildIconUrl(Id, IconId);
@@ -87,6 +88,8 @@ namespace Discord.Rest
roles[model.Roles[i].Id] = RestRole.Create(Discord, model.Roles[i]);
}
_roles = roles.ToImmutable();
Available = true;
}
//General
@@ -169,7 +172,7 @@ namespace Discord.Rest
=> GuildHelper.PruneUsersAsync(this, Discord, days, simulate);
//IGuild
bool IGuild.Available => true;
bool IGuild.Available => Available;
IAudioClient IGuild.AudioClient => null;
IRole IGuild.EveryoneRole => EveryoneRole;
IReadOnlyCollection<IRole> IGuild.Roles => Roles;

View File

@@ -15,19 +15,30 @@ namespace Discord.Rest
private ImmutableArray<ulong> _roleIds;
public string Nickname { get; private set; }
public ulong GuildId { get; private set; }
internal IGuild Guild { get; private set; }
public ulong GuildId => Guild.Id;
public GuildPermissions GuildPermissions
{
get
{
if (!Guild.Available)
throw new InvalidOperationException("Resolving permissions requires the parent guild to be downloaded.");
return new GuildPermissions(Permissions.ResolveGuild(Guild, this));
}
}
public IReadOnlyCollection<ulong> RoleIds => _roleIds;
public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks);
internal RestGuildUser(BaseDiscordClient discord, ulong id)
internal RestGuildUser(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, id)
{
Guild = guild;
}
internal static RestGuildUser Create(BaseDiscordClient discord, Model model)
internal static RestGuildUser Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestGuildUser(discord, model.User.Id);
var entity = new RestGuildUser(discord, guild, model.User.Id);
entity.Update(model);
return entity;
}
@@ -41,7 +52,7 @@ namespace Discord.Rest
private void UpdateRoles(ulong[] roleIds)
{
var roles = ImmutableArray.CreateBuilder<ulong>(roleIds.Length + 1);
roles.Add(GuildId);
roles.Add(Guild.Id);
for (int i = 0; i < roleIds.Length; i++)
roles.Add(roleIds[i]);
_roleIds = roles.ToImmutable();
@@ -56,12 +67,10 @@ namespace Discord.Rest
public ChannelPermissions GetPermissions(IGuildChannel channel)
{
throw new NotImplementedException(); //TODO: Impl
var guildPerms = GuildPermissions;
return new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, guildPerms.RawValue));
}
//IGuildUser
IReadOnlyCollection<ulong> IGuildUser.RoleIds => RoleIds;
//IVoiceState
bool IVoiceState.IsDeafened => false;
bool IVoiceState.IsMuted => false;