Browse Source

modify welcome screen support

pull/2510/head
Misha133 2 years ago
parent
commit
94fe141ec4
7 changed files with 112 additions and 29 deletions
  1. +9
    -1
      src/Discord.Net.Core/Entities/Guilds/IGuild.cs
  2. +0
    -13
      src/Discord.Net.Core/Entities/Guilds/WelcomeScreenChannel.cs
  3. +50
    -0
      src/Discord.Net.Core/Entities/Guilds/WelcomeScreenChannelProperties.cs
  4. +1
    -1
      src/Discord.Net.Rest/API/Rest/ModifyGuildWelcomeScreenParams.cs
  5. +44
    -14
      src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
  6. +4
    -0
      src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
  7. +4
    -0
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs

+ 9
- 1
src/Discord.Net.Core/Entities/Guilds/IGuild.cs View File

@@ -1241,11 +1241,19 @@ namespace Discord
RequestOptions options = null);

/// <summary>
/// Gets the welcome screen of the guild.
/// Gets the welcome screen of the guild. Returns <see langword="null"/> is the welcome channel is not set.
/// </summary>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains a <see cref="WelcomeScreen"/>.
/// </returns>
Task<WelcomeScreen> GetWelcomeScreenAsync(RequestOptions options = null);

/// <summary>
/// Modifies the welcome screen of the guild. Returns <see langword="null"/> if welcome screen is removed.
/// </summary>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains a <see cref="WelcomeScreen"/>.
/// </returns>
Task<WelcomeScreen> ModifyWelcomeScreenAsync(bool enabled, WelcomeScreenChannelProperties[] channels, string description = null, RequestOptions options = null);
}
}

+ 0
- 13
src/Discord.Net.Core/Entities/Guilds/WelcomeScreenChannel.cs View File

@@ -40,17 +40,4 @@ public class WelcomeScreenChannel : ISnowflakeEntity
Emoji = null;

}

/// <summary>
/// Initializes a new instance of <see cref="WelcomeScreenChannel"/> to be used to modify one.
/// </summary>
/// <param name="id"></param>
/// <param name="description"></param>
/// <param name="emoji"></param>
public WelcomeScreenChannel(ulong id, string description, IEmote emoji)
{
Id = id;
Description = description;
Emoji = emoji;
}
}

+ 50
- 0
src/Discord.Net.Core/Entities/Guilds/WelcomeScreenChannelProperties.cs View File

@@ -0,0 +1,50 @@
using System;
using System.Xml.Linq;

namespace Discord;

public class WelcomeScreenChannelProperties : ISnowflakeEntity
{
/// <summary>
/// Gets or sets the channel's id.
/// </summary>
public ulong Id { get; set; }

/// <summary>
/// Gets or sets the description shown for the channel.
/// </summary>
public string Description { get; set; }

/// <summary>
/// Gets or sets the emoji for this channel. <see cref="Emoji"/> if it is unicode emoji, <see cref="Emote"/> if it is a custom one and <see langword="null"/> if none is set.
/// </summary>
/// <remarks>
/// If the emoji is <see cref="Emote"/> only the <see cref="Emote.Id"/> will be populated.
/// Use <see cref="IGuild.GetEmoteAsync"/> to get the emoji.
/// </remarks>
public IEmote Emoji { get; set; }

/// <inheritdoc/>
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);

/// <summary>
/// Initializes a new instance of <see cref="WelcomeScreenChannelProperties"/>.
/// </summary>
/// <param name="id">Id if a channel.</param>
/// <param name="description">Description for the channel in the welcome screen.</param>
/// <param name="emoji">The emoji for the channel in the welcome screen.</param>
public WelcomeScreenChannelProperties(ulong id, string description, IEmote emoji = null)
{
Id = id;
Description = description;
Emoji = emoji;
}

/// <summary>
/// Initializes a new instance of <see cref="WelcomeScreenChannelProperties"/>.
/// </summary>
/// <param name="channel">A welcome screen channel to modify.</param>
/// <returns>A new instance of <see cref="WelcomeScreenChannelProperties"/>.</returns>
public static WelcomeScreenChannelProperties FromWelcomeScreenChannel(WelcomeScreenChannel channel)
=> new (channel.Id, channel.Description, channel.Emoji);
}

+ 1
- 1
src/Discord.Net.Rest/API/Rest/ModifyGuildWelcomeScreenParams.cs View File

@@ -10,7 +10,7 @@ internal class ModifyGuildWelcomeScreenParams
public Optional<bool> Enabled { get; set; }

[JsonProperty("welcome_channels")]
public Optional<WelcomeScreenChannel> Channels { get; set; }
public Optional<WelcomeScreenChannel[]> WelcomeChannels { get; set; }

[JsonProperty("description")]
public Optional<string> Description { get; set; }


+ 44
- 14
src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs View File

@@ -2,6 +2,7 @@ using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using WidgetModel = Discord.API.GuildWidget;
@@ -19,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);
@@ -140,7 +142,7 @@ namespace Discord.Rest
};

var mebibyte = Math.Pow(2, 20);
return (ulong) (tierFactor * mebibyte);
return (ulong)(tierFactor * mebibyte);
}
#endregion

@@ -231,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);
@@ -260,7 +263,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);
@@ -316,7 +320,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);
@@ -387,7 +392,8 @@ 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);
@@ -400,7 +406,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
{
@@ -616,7 +623,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);
@@ -807,7 +815,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}");
@@ -849,7 +857,7 @@ namespace Discord.Rest
: Optional<ImageModel?>.Unspecified
};

if(args.Location.IsSpecified)
if (args.Location.IsSpecified)
{
apiArgs.EntityMetadata = new API.GuildScheduledEventEntityMetadata()
{
@@ -889,7 +897,7 @@ namespace Discord.Rest
Image? bannerImage = null,
RequestOptions options = null)
{
if(location != null)
if (location != null)
{
Preconditions.AtMost(location.Length, 100, nameof(location));
}
@@ -925,7 +933,7 @@ namespace Discord.Rest
Image = bannerImage.HasValue ? bannerImage.Value.ToModel() : Optional<ImageModel>.Unspecified
};

if(location != null)
if (location != null)
{
apiArgs.EntityMetadata = new API.GuildScheduledEventEntityMetadata()
{
@@ -951,6 +959,9 @@ namespace Discord.Rest
{
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,
@@ -958,9 +969,28 @@ namespace Discord.Rest
x.EmojiId.GetValueOrDefault(0))).ToList());
}

public static async Task<WelcomeScreen> ModifyWelcomeScreenAsync(IGuild guild, BaseDiscordClient client, RequestOptions options)
public static async Task<WelcomeScreen> ModifyWelcomeScreenAsync(bool enabled, string description, WelcomeScreenChannelProperties[] channels, IGuild guild, BaseDiscordClient client, RequestOptions options)
{
var model = await client.ApiClient.ModifyGuildWelcomeScreenAsync(null, guild.Id, options);
if (!guild.Features.HasFeature(GuildFeature.Community))
throw new InvalidOperationException("Cannot update welcome screed 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(


+ 4
- 0
src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs View File

@@ -1523,6 +1523,10 @@ namespace Discord.Rest
public Task<WelcomeScreen> GetWelcomeScreenAsync(RequestOptions options = null)
=> GuildHelper.GetWelcomeScreenAsync(this, Discord, options);

/// <inheritdoc/>
public Task<WelcomeScreen> ModifyWelcomeScreenAsync(bool enabled, WelcomeScreenChannelProperties[] channels, string description = null, RequestOptions options = null)
=> GuildHelper.ModifyWelcomeScreenAsync(enabled, description, channels, this, Discord, options);

#endregion
}
}

+ 4
- 0
src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs View File

@@ -2015,6 +2015,10 @@ namespace Discord.WebSocket
public Task<WelcomeScreen> GetWelcomeScreenAsync(RequestOptions options = null)
=> GuildHelper.GetWelcomeScreenAsync(this, Discord, options);

/// <inheritdoc/>
public Task<WelcomeScreen> ModifyWelcomeScreenAsync(bool enabled, WelcomeScreenChannelProperties[] channels, string description = null, RequestOptions options = null)
=> GuildHelper.ModifyWelcomeScreenAsync(enabled, description, channels, this, Discord, options);

void IDisposable.Dispose()
{
DisconnectAudioAsync().GetAwaiter().GetResult();


Loading…
Cancel
Save