[Feature] add missing invite guild properties & welcome screen support (#2510)
* added models * working getter for welcome screen * <see langword="null"/> * more changes * modify welcome screen support * fix some typos & remove `using` added by VS * Working-ish state * Resolve some reviews * change access modifier * forgot to add docs * revert to InviteGuild & extend it * resolve some reviews * Apply suggestions from code review Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com> Co-authored-by: Cenk Ergen <57065323+Cenngo@users.noreply.github.com> Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>
This commit is contained in:
@@ -20,7 +20,8 @@ namespace Discord.Rest
|
||||
public static async Task<Model> ModifyAsync(IGuild guild, BaseDiscordClient client,
|
||||
Action<GuildProperties> func, RequestOptions options)
|
||||
{
|
||||
if (func == null) throw new ArgumentNullException(nameof(func));
|
||||
if (func == null)
|
||||
throw new ArgumentNullException(nameof(func));
|
||||
|
||||
var args = new GuildProperties();
|
||||
func(args);
|
||||
@@ -141,7 +142,7 @@ namespace Discord.Rest
|
||||
};
|
||||
|
||||
var mebibyte = Math.Pow(2, 20);
|
||||
return (ulong) (tierFactor * mebibyte);
|
||||
return (ulong)(tierFactor * mebibyte);
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -232,7 +233,8 @@ namespace Discord.Rest
|
||||
public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client,
|
||||
string name, RequestOptions options, Action<TextChannelProperties> func = null)
|
||||
{
|
||||
if (name == null) throw new ArgumentNullException(paramName: nameof(name));
|
||||
if (name == null)
|
||||
throw new ArgumentNullException(paramName: nameof(name));
|
||||
|
||||
var props = new TextChannelProperties();
|
||||
func?.Invoke(props);
|
||||
@@ -262,7 +264,8 @@ namespace Discord.Rest
|
||||
public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
|
||||
string name, RequestOptions options, Action<VoiceChannelProperties> func = null)
|
||||
{
|
||||
if (name == null) throw new ArgumentNullException(paramName: nameof(name));
|
||||
if (name == null)
|
||||
throw new ArgumentNullException(paramName: nameof(name));
|
||||
|
||||
var props = new VoiceChannelProperties();
|
||||
func?.Invoke(props);
|
||||
@@ -318,7 +321,8 @@ namespace Discord.Rest
|
||||
public static async Task<RestCategoryChannel> CreateCategoryChannelAsync(IGuild guild, BaseDiscordClient client,
|
||||
string name, RequestOptions options, Action<GuildChannelProperties> func = null)
|
||||
{
|
||||
if (name == null) throw new ArgumentNullException(paramName: nameof(name));
|
||||
if (name == null)
|
||||
throw new ArgumentNullException(paramName: nameof(name));
|
||||
|
||||
var props = new GuildChannelProperties();
|
||||
func?.Invoke(props);
|
||||
@@ -448,11 +452,13 @@ namespace Discord.Rest
|
||||
RequestOptions options)
|
||||
{
|
||||
var vanityModel = await client.ApiClient.GetVanityInviteAsync(guild.Id, options).ConfigureAwait(false);
|
||||
if (vanityModel == null) throw new InvalidOperationException("This guild does not have a vanity URL.");
|
||||
if (vanityModel == null)
|
||||
throw new InvalidOperationException("This guild does not have a vanity URL.");
|
||||
var inviteModel = await client.ApiClient.GetInviteAsync(vanityModel.Code, options).ConfigureAwait(false);
|
||||
inviteModel.Uses = vanityModel.Uses;
|
||||
return RestInviteMetadata.Create(client, guild, null, inviteModel);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Roles
|
||||
@@ -460,7 +466,8 @@ namespace Discord.Rest
|
||||
public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
|
||||
string name, GuildPermissions? permissions, Color? color, bool isHoisted, bool isMentionable, RequestOptions options)
|
||||
{
|
||||
if (name == null) throw new ArgumentNullException(paramName: nameof(name));
|
||||
if (name == null)
|
||||
throw new ArgumentNullException(paramName: nameof(name));
|
||||
|
||||
var createGuildRoleParams = new API.Rest.ModifyGuildRoleParams
|
||||
{
|
||||
@@ -676,7 +683,8 @@ namespace Discord.Rest
|
||||
public static async Task<GuildEmote> ModifyEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, Action<EmoteProperties> func,
|
||||
RequestOptions options)
|
||||
{
|
||||
if (func == null) throw new ArgumentNullException(paramName: nameof(func));
|
||||
if (func == null)
|
||||
throw new ArgumentNullException(paramName: nameof(func));
|
||||
|
||||
var props = new EmoteProperties();
|
||||
func(props);
|
||||
@@ -867,7 +875,7 @@ namespace Discord.Rest
|
||||
{
|
||||
switch (args.Status.Value)
|
||||
{
|
||||
case GuildScheduledEventStatus.Active when guildEvent.Status != GuildScheduledEventStatus.Scheduled:
|
||||
case GuildScheduledEventStatus.Active when guildEvent.Status != GuildScheduledEventStatus.Scheduled:
|
||||
case GuildScheduledEventStatus.Completed when guildEvent.Status != GuildScheduledEventStatus.Active:
|
||||
case GuildScheduledEventStatus.Cancelled when guildEvent.Status != GuildScheduledEventStatus.Scheduled:
|
||||
throw new ArgumentException($"Cannot set event to {args.Status.Value} when events status is {guildEvent.Status}");
|
||||
@@ -909,7 +917,7 @@ namespace Discord.Rest
|
||||
: Optional<ImageModel?>.Unspecified
|
||||
};
|
||||
|
||||
if(args.Location.IsSpecified)
|
||||
if (args.Location.IsSpecified)
|
||||
{
|
||||
apiArgs.EntityMetadata = new API.GuildScheduledEventEntityMetadata()
|
||||
{
|
||||
@@ -949,7 +957,7 @@ namespace Discord.Rest
|
||||
Image? bannerImage = null,
|
||||
RequestOptions options = null)
|
||||
{
|
||||
if(location != null)
|
||||
if (location != null)
|
||||
{
|
||||
Preconditions.AtMost(location.Length, 100, nameof(location));
|
||||
}
|
||||
@@ -985,7 +993,7 @@ namespace Discord.Rest
|
||||
Image = bannerImage.HasValue ? bannerImage.Value.ToModel() : Optional<ImageModel>.Unspecified
|
||||
};
|
||||
|
||||
if(location != null)
|
||||
if (location != null)
|
||||
{
|
||||
apiArgs.EntityMetadata = new API.GuildScheduledEventEntityMetadata()
|
||||
{
|
||||
@@ -1004,5 +1012,53 @@ namespace Discord.Rest
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Welcome Screen
|
||||
|
||||
public static async Task<WelcomeScreen> GetWelcomeScreenAsync(IGuild guild, BaseDiscordClient client, RequestOptions options)
|
||||
{
|
||||
var model = await client.ApiClient.GetGuildWelcomeScreenAsync(guild.Id, options);
|
||||
|
||||
if (model.WelcomeChannels.Length == 0)
|
||||
return null;
|
||||
|
||||
return new WelcomeScreen(model.Description.GetValueOrDefault(null), model.WelcomeChannels.Select(
|
||||
x => new WelcomeScreenChannel(
|
||||
x.ChannelId, x.Description,
|
||||
x.EmojiName.GetValueOrDefault(null),
|
||||
x.EmojiId.GetValueOrDefault(0))).ToList());
|
||||
}
|
||||
|
||||
public static async Task<WelcomeScreen> ModifyWelcomeScreenAsync(bool enabled, string description, WelcomeScreenChannelProperties[] channels, IGuild guild, BaseDiscordClient client, RequestOptions options)
|
||||
{
|
||||
if (!guild.Features.HasFeature(GuildFeature.Community))
|
||||
throw new InvalidOperationException("Cannot update welcome screen in a non-community guild.");
|
||||
|
||||
var args = new ModifyGuildWelcomeScreenParams
|
||||
{
|
||||
Enabled = enabled,
|
||||
Description = description,
|
||||
WelcomeChannels = channels?.Select(ch => new API.WelcomeScreenChannel
|
||||
{
|
||||
ChannelId = ch.Id,
|
||||
Description = ch.Description,
|
||||
EmojiName = ch.Emoji is Emoji emoj ? emoj.Name : Optional<string>.Unspecified,
|
||||
EmojiId = ch.Emoji is Emote emote ? emote.Id : Optional<ulong?>.Unspecified
|
||||
}).ToArray()
|
||||
};
|
||||
|
||||
var model = await client.ApiClient.ModifyGuildWelcomeScreenAsync(args, guild.Id, options);
|
||||
|
||||
if(model.WelcomeChannels.Length == 0)
|
||||
return null;
|
||||
|
||||
return new WelcomeScreen(model.Description.GetValueOrDefault(null), model.WelcomeChannels.Select(
|
||||
x => new WelcomeScreenChannel(
|
||||
x.ChannelId, x.Description,
|
||||
x.EmojiName.GetValueOrDefault(null),
|
||||
x.EmojiId.GetValueOrDefault(0))).ToList());
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user