Skip to content

Commit

Permalink
Merge pull request #7 from UncomplicatedCustomServer/dev-main
Browse files Browse the repository at this point in the history
Release v1.0.0
  • Loading branch information
FoxWorn3365 authored Oct 7, 2024
2 parents 1d359d1 + 9264ba6 commit 770473b
Show file tree
Hide file tree
Showing 48 changed files with 847 additions and 121 deletions.
10 changes: 8 additions & 2 deletions SimpleDiscord/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public class Client

public readonly Handler EventHandler;

public static Version Version { get; } = new(0, 9, 0);
public static Version Version { get; } = new(1, 0, 0);

public SocketUser Bot { get; internal set; }

public SocketUser CurrentUser => Bot;

public PartialApplication Application { get; internal set; }

public List<ApplicationCommand> Commands { get; } = [];
Expand Down Expand Up @@ -68,7 +70,7 @@ public Client(ClientConfig config = null)
Logger = new(Config);
ErrorHub = new(this);
GatewatEventHandler = new();
EventHandler = new();
EventHandler = new(this);
_discordClient = new(this);
RestHttp = new(_discordClient.httpClient, this);
}
Expand Down Expand Up @@ -102,6 +104,10 @@ public async void RegisterCommand(SocketSendApplicationCommand command)
public Guild? GetGuild(long id) => Guild.List.FirstOrDefault(guild => guild.Id == id);
#nullable disable

public Task SendWebhook(long id, string token, SocketSendMessage message, long? threadId = null) => RestHttp.SendWebhook(id, token, message, threadId);

public Task SendWebhook(string url, SocketSendMessage message) => RestHttp.SendWebhook(url, message);

public Task<HttpResponseMessage> ApiRequest(HttpRequestMessage message, HttpStatusCode expectedResult = HttpStatusCode.OK, bool disableResponseCheck = false) => RestHttp.Send(message, expectedResult, disableResponseCheck);

internal Guild GetSafeGuild(long id) => Guild.List.FirstOrDefault(safeguild => safeguild.Id == id);
Expand Down
12 changes: 11 additions & 1 deletion SimpleDiscord/Components/ApplicationCommandInteractionData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Newtonsoft.Json;
using SimpleDiscord.Components.Attributes;
using System.Linq;

namespace SimpleDiscord.Components
{
#pragma warning disable IDE0290
#nullable enable
[SocketInstance(typeof(SocketApplicationCommandInteractionData))]
public class ApplicationCommandInteractionData : InteractionData
Expand All @@ -15,7 +17,7 @@ public class ApplicationCommandInteractionData : InteractionData

public ResolvedData? Resolved { get; }

public ReplyCommandOption[]? Options { get; }
public ReplyCommandOption[]? Options { get; internal set; }

public long? TargetId { get; }

Expand All @@ -35,5 +37,13 @@ public ApplicationCommandInteractionData(ApplicationCommandInteractionData self)

public ApplicationCommandInteractionData(SocketApplicationCommandInteractionData socketInstance) : this(socketInstance.Id, socketInstance.Name, socketInstance.Type, socketInstance.Resolved is not null ? new(socketInstance.Resolved) : null, socketInstance.Options, socketInstance.TargetId)
{ }

public ReplyCommandOption? GetOption(string name) => Options?.FirstOrDefault(o => o.Name == name);

public bool TryGetOption(string name, out ReplyCommandOption? option)
{
option = GetOption(name);
return option is not null;
}
}
}
2 changes: 2 additions & 0 deletions SimpleDiscord/Components/Builders/ActivityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public ActivityBuilder SetAssets(ActivityAsset assets)

public ActivityBuilder AddButton(ActivityButton button)
{
activity.Buttons ??= [];

activity.Buttons.Append(button);
return this;
}
Expand Down
64 changes: 64 additions & 0 deletions SimpleDiscord/Components/Builders/ButtonBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using SimpleDiscord.Components.DiscordComponents;
using SimpleDiscord.Enums;
using System;
using System.Linq;

namespace SimpleDiscord.Components.Builders
{
public class ButtonBuilder
{
internal readonly Button button = new((int)ButtonStyle.Primary, null, null, RandomString(35), null);

public ButtonBuilder SetLabel(string label)
{
button.Label = label;
return this;
}

public ButtonBuilder SetStyle(ButtonStyle style)
{
button.Style = (int)style;
return this;
}

public ButtonBuilder SetEmoji(Emoji emoji)
{
button.Emoji = emoji;
return this;
}

public ButtonBuilder SetData(object data)
{
button.Data = data;
return this;
}

public ButtonBuilder SetCallback(Action<Interaction, object> callback)
{
button.Callback = callback;
return this;
}

public ButtonBuilder SetCustomId(string customId)
{
button.CustomId = customId;
return this;
}

public ButtonBuilder SetDisabled(bool disabled)
{
button.Disabled = disabled;
return this;
}

public static ButtonBuilder New() => new();

public static implicit operator Button(ButtonBuilder builder) => builder.button;

private static string RandomString(int length = 10)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz.,;_#@=()$?";
return new string(Enumerable.Repeat(chars, length).Select(s => s[Discord.Random.Next(s.Length)]).ToArray());
}
}
}
4 changes: 2 additions & 2 deletions SimpleDiscord/Components/Builders/CommandOptionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public class CommandOptionBuilder(string name, CommandOptionType type, string de
public CommandOptionBuilder AddChoice(CommandOptionChoice choice)
{
Option.Choiches ??= [];
Option.Choiches.Append(choice);
Option.Choiches.Add(choice);
return this;
}

public CommandOptionBuilder AddOption(SocketCommandOption option)
{
Option.Options ??= [];
Option.Options.Append(option);
Option.Options.Add(option);
return this;
}

Expand Down
10 changes: 6 additions & 4 deletions SimpleDiscord/Components/Builders/EmbedBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ public EmbedBuilder SetAuthor(string name, string? url = null, string? iconUrl =
return this;
}

public EmbedBuilder SetFooter(string text, string? iconUrl)
public EmbedBuilder SetFooter(string text, string? iconUrl = null)
{
Embed.Footer = new(text, iconUrl);
return this;
}

public EmbedBuilder SetThumbnail(string url, int? width, int? height)
public EmbedBuilder SetThumbnail(string url, int? width = null, int? height = null)
{
Embed.Thumbnail = new(url, null, height, width);
return this;
}

public EmbedBuilder SetImage(string url, int? width, int? height)
public EmbedBuilder SetImage(string url, int? width = null, int? height = null)
{
Embed.Image = new(url, null, height, width);
return this;
Expand All @@ -55,7 +55,9 @@ public EmbedBuilder SetTimestamp(DateTimeOffset? offset = null)

public EmbedBuilder AddField(string name, string value, bool? inline = false)
{
Embed.Fields.Append(new(name, value, inline));
Embed.Fields ??= [];

Embed.Fields.Add(new(name, value, inline));
return this;
}
#nullable disable
Expand Down
33 changes: 31 additions & 2 deletions SimpleDiscord/Components/Builders/MessageBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SimpleDiscord.Components.DiscordComponents;
using SimpleDiscord.Enums;
using System.Collections.Generic;
using System.Linq;

namespace SimpleDiscord.Components.Builders
Expand All @@ -19,21 +20,49 @@ public MessageBuilder AddEmbed(Embed embed)
if (Message.Embeds is null)
Message.Embeds = [embed];
else
Message.Embeds.Append(embed);
Message.Embeds.Add(embed);

return this;
}

public MessageBuilder SetEmbeds(List<Embed> embeds)
{
Message.Embeds = embeds;
return this;
}

public MessageBuilder AddComponent(ActionRow actionRow)
{
if (Message.Components is null)
Message.Components = [actionRow];
else
Message.Components.Append(actionRow);
Message.Components.Add(actionRow);

return this;
}

public MessageBuilder SetComponents(List<SocketActionRow> actionRows)
{
Message.Components = actionRows;
return this;
}

public MessageBuilder AddAttachment(Attachment attachment)
{
if (Message.Attachments is null)
Message.Attachments = [attachment];
else
Message.Attachments.Add(attachment);

return this;
}

public MessageBuilder SetAttachments(List<Attachment> attachments)
{
Message.Attachments = attachments;
return this;
}

public MessageBuilder SetEphemeral()
{
if (Message.Flags is null)
Expand Down
2 changes: 1 addition & 1 deletion SimpleDiscord/Components/ClientChild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace SimpleDiscord.Components
public class ClientChild() : DisposableElement
{
[JsonIgnore]
internal Client Client { get; private set; }
internal Client Client { get; set; }

internal void SetClient(Client client) => Client = client;
}
Expand Down
6 changes: 3 additions & 3 deletions SimpleDiscord/Components/DiscordComponents/ActionRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace SimpleDiscord.Components.DiscordComponents
[SocketInstance(typeof(SocketActionRow))]
public class ActionRow : SocketActionRow
{
public new List<GenericComponent> Components { get; } = [];
public new List<GenericComponent> Components { get; internal set; } = [];

public ActionRow() : base([]) => Components = [];
public ActionRow() : base((int)ComponentType.ActionRow, []) => Components = [];

public ActionRow(SocketActionRow actionRow) : base(actionRow)
{
Expand All @@ -22,7 +22,7 @@ public ActionRow(SocketActionRow actionRow) : base(actionRow)
Components.Add(Caster((ComponentType)Type, obj));
}

public SocketActionRow ToSocketInstance() => new([..Components]);
public SocketActionRow ToSocketInstance() => new(this);

public void BulkAdd(IEnumerable<GenericComponent> components)
{
Expand Down
18 changes: 12 additions & 6 deletions SimpleDiscord/Components/DiscordComponents/Button.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SimpleDiscord.Enums;
using System;

namespace SimpleDiscord.Components.DiscordComponents
{
Expand All @@ -7,18 +8,23 @@ public class Button(int style, string? label, Emoji? emoji, string? customId, st
{
public override int Type => (int)ComponentType.Button;

public int Style { get; } = style;
public int Style { get; internal set; } = style;

public string? Label { get; } = label;
public string? Label { get; internal set; } = label;

public Emoji? Emoji { get; } = emoji;
public Emoji? Emoji { get; internal set; } = emoji;

public string? CustomId { get; } = customId;
public string? CustomId { get; internal set; } = customId;

public string? Url { get; } = url;
public string? Url { get; internal set; } = url;

public bool? Disabled { get; } = disabled;
public bool? Disabled { get; internal set; } = disabled;

internal Action<Interaction, object>? Callback { get; set; } = null;

internal object? Data { get; set; } = null;

[Obsolete("You can't use anymore the Button::New function as it's not suitable anymore! Consider using the Components.Builders.ButtonBuilder class instead!", true)]
public static Button New(ButtonStyle style, string label, Emoji? emoji = null, string? customId = null, string? url = null, bool? disabled = false) => new((int)style, label, emoji, customId, url, disabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class ChannelSelectMenu : SelectMenu
{
public override int Type => (int)ComponentType.ChannelSelect;

public SelectDefaultValue[]? DefaultValues { get; }
public SelectDefaultValue[]? DefaultValues { get; internal set; }

public int[]? ChannelTypes { get; }
public int[]? ChannelTypes { get; internal set; }

[JsonConstructor]
public ChannelSelectMenu(SelectDefaultValue[]? defaultValues, int[]? channelTypes, string customId, string placeholder, int minValues, int maxValues, bool disabled = false) : base(customId, placeholder, minValues, maxValues, disabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class MentionableSelectMenu : SelectMenu
{
public override int Type => (int)ComponentType.MentionableSelect;

public SelectDefaultValue[]? DefaultValues { get; }
public SelectDefaultValue[]? DefaultValues { get; internal set; }

[JsonConstructor]
public MentionableSelectMenu(SelectDefaultValue[]? defaultValues, string customId, string placeholder, int minValues, int maxValues, bool disabled = false) : base(customId, placeholder, minValues, maxValues, disabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class RoleSelectMenu : SelectMenu
{
public override int Type => (int)ComponentType.RoleSelect;

public SelectDefaultValue[]? DefaultValues { get; }
public SelectDefaultValue[]? DefaultValues { get; internal set; }

[JsonConstructor]
public RoleSelectMenu(SelectDefaultValue[]? defaultValues, string customId, string placeholder, int minValues, int maxValues, bool disabled = false) : base(customId, placeholder, minValues, maxValues, disabled)
Expand Down
30 changes: 21 additions & 9 deletions SimpleDiscord/Components/DiscordComponents/SocketActionRow.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SimpleDiscord.Enums;
using System;
using System.Collections.Generic;

namespace SimpleDiscord.Components.DiscordComponents
{
#nullable enable
public class SocketActionRow : GenericComponent
{
public override int Type => (int)ComponentType.ActionRow;
public int? Id { get; }

public object[] Components { get; }
public override int Type { get; }

[JsonConstructor]
public SocketActionRow(object[] baseComponents)
{
Components = baseComponents;
}
public List<object>? Components { get; }

public SocketActionRow(SocketActionRow self) : this(self.Components)
public SocketActionRow(SocketActionRow self) : this((int)ComponentType.ActionRow, self.Components, self.Id)
{ }

public SocketActionRow(ActionRow child) : this([.. child.Components])
public SocketActionRow(ActionRow child) : this((int)ComponentType.ActionRow, [.. child.Components], child.Id)
{ }

[JsonConstructor]
public SocketActionRow(int type, List<object>? components, int? id = null)
{
Id = id;
Type = type;
Components = [];
if (components is not null)
foreach (object component in components)
if (component is JObject obj)
Components.Add(obj);
}
}
}
Loading

0 comments on commit 770473b

Please sign in to comment.