Skip to content

Commit 431ec8d

Browse files
committed
message builders
1 parent 1c242d7 commit 431ec8d

4 files changed

Lines changed: 203 additions & 29 deletions

File tree

src/Disc.NET.Commands/Message.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Disc.NET.Client.SDK.Messages;
33
using Disc.NET.Client.SDK.Messages.Components;
44
using Disc.NET.Client.SDK.Messages.Components.Enums;
5+
using Disc.NET.Client.SDK.Messages.Embeds;
56
using Disc.NET.Commands.Contexts;
67
using Disc.NET.Commands.MessageBuilders;
78
using Disc.NET.Shared.Constraints;
@@ -11,10 +12,27 @@ namespace Disc.NET.Commands
1112
{
1213
public class Message : ApiMessage
1314
{
14-
1515
public List<MessageFlag>? MessageFlags { get; set; }
1616

1717
public List<IActionRowBuilder> ActionRows { get; set; } = [];
18+
19+
public Message AddEmbed(Embed embed)
20+
{
21+
Embeds.Add(embed);
22+
return this;
23+
}
24+
25+
public Message AddEmbed(EmbedBuilder builder)
26+
{
27+
return AddEmbed(builder.Build());
28+
}
29+
30+
public Message AddEmbed(Action<EmbedBuilder> configure)
31+
{
32+
var builder = new EmbedBuilder();
33+
configure(builder);
34+
return AddEmbed(builder);
35+
}
1836

1937
public ApiMessage Build()
2038
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Disc.NET.Client.SDK.Messages.Embeds;
2+
using Disc.NET.Commands.MessageBuilders;
3+
4+
namespace Disc.NET.Commands
5+
{
6+
public class MessageBuilder
7+
{
8+
private readonly Message _message = new();
9+
10+
public MessageBuilder WithContent(string content)
11+
{
12+
_message.Content = content;
13+
return this;
14+
}
15+
16+
public MessageBuilder WithEmbed(Embed embed)
17+
{
18+
_message.AddEmbed(embed);
19+
return this;
20+
}
21+
22+
public MessageBuilder WithEmbed(EmbedBuilder embedBuilder)
23+
{
24+
_message.AddEmbed(embedBuilder);
25+
return this;
26+
}
27+
28+
public MessageBuilder WithEmbed(Action<EmbedBuilder> configure)
29+
{
30+
_message.AddEmbed(configure);
31+
return this;
32+
}
33+
34+
public MessageBuilder WithActionRow(IActionRowBuilder actionRowBuilder)
35+
{
36+
_message.ActionRows.Add(actionRowBuilder);
37+
return this;
38+
}
39+
40+
public Message Build()
41+
{
42+
return _message;
43+
}
44+
}
45+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using Disc.NET.Client.SDK.Messages.Embeds;
2+
3+
namespace Disc.NET.Commands.MessageBuilders
4+
{
5+
public sealed class EmbedBuilder
6+
{
7+
private readonly Embed _embed = new();
8+
9+
public EmbedBuilder SetTitle(string? title)
10+
{
11+
_embed.Title = title;
12+
return this;
13+
}
14+
15+
public EmbedBuilder SetDescription(string? description)
16+
{
17+
_embed.Description = description;
18+
return this;
19+
}
20+
21+
public EmbedBuilder SetType(string? type)
22+
{
23+
_embed.Type = type;
24+
return this;
25+
}
26+
27+
public EmbedBuilder SetUrl(string? url)
28+
{
29+
_embed.Url = url;
30+
return this;
31+
}
32+
33+
public EmbedBuilder SetTimestamp(DateTimeOffset timestamp)
34+
{
35+
_embed.Timestamp = timestamp.ToString("O");
36+
return this;
37+
}
38+
39+
public EmbedBuilder SetTimestamp(string? timestamp)
40+
{
41+
_embed.Timestamp = timestamp;
42+
return this;
43+
}
44+
45+
public EmbedBuilder SetColor(int? color)
46+
{
47+
_embed.Color = color;
48+
return this;
49+
}
50+
51+
public EmbedBuilder SetFooter(string text, string? iconUrl = null, string? proxyIconUrl = null)
52+
{
53+
_embed.Footer = new EmbedFooter
54+
{
55+
Text = text,
56+
IconUrl = iconUrl,
57+
ProxyIconUrl = proxyIconUrl,
58+
};
59+
60+
return this;
61+
}
62+
63+
public EmbedBuilder SetImage(string url)
64+
{
65+
_embed.Image = new EmbedImage
66+
{
67+
Url = url,
68+
};
69+
70+
return this;
71+
}
72+
73+
public EmbedBuilder SetThumbnail(string url)
74+
{
75+
_embed.Thumbnail = new EmbedImage
76+
{
77+
Url = url,
78+
};
79+
80+
return this;
81+
}
82+
83+
public EmbedBuilder SetAuthor(string name, string? url = null, string? iconUrl = null, string? proxyIconUrl = null)
84+
{
85+
_embed.Author = new EmbedAuthor
86+
{
87+
Name = name,
88+
Url = url,
89+
IconUrl = iconUrl,
90+
ProxyIconUrl = proxyIconUrl,
91+
};
92+
93+
return this;
94+
}
95+
96+
public EmbedBuilder AddField(string name, string value, bool inline = false)
97+
{
98+
_embed.Fields ??= [];
99+
_embed.Fields.Add(new EmbedField
100+
{
101+
Name = name,
102+
Value = value,
103+
Inline = inline,
104+
});
105+
106+
return this;
107+
}
108+
109+
public EmbedBuilder ClearFields()
110+
{
111+
_embed.Fields?.Clear();
112+
return this;
113+
}
114+
115+
public Embed Build()
116+
{
117+
return _embed;
118+
}
119+
}
120+
}

tests/GenericBot/TestCommand.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,35 @@
99
namespace GenericBot
1010
{
1111

12-
[SlashCommand("test", InteractionType.SubCommand, "Ver as informações de tempo da sua cidade")]
12+
[SlashCommand("testo", InteractionType.SubCommand, "Ver as informações de tempo da sua cidade")]
1313
public class TestCommand : ISlashCommand
1414
{
1515
public async Task RunAsync(InteractionContext context, CancellationToken cancellation = default)
1616
{
17-
18-
var message = new Message()
19-
{
20-
Content = "Teste",
21-
ActionRows = new List<IActionRowBuilder>
22-
{
23-
new ActionRowBuilder()
24-
.AddSelectMenu("test_select", new List<StringSelectOption>()
25-
{
26-
new StringSelectOption()
27-
{
28-
Label = "Opção 1",
29-
Value = "option_1"
30-
},
31-
32-
}, context: context, callback: TestCallback3Async),
33-
34-
35-
new ActionRowBuilder()
36-
.AddButton("Testar", "test_button", ButtonStyle.Primary, context, TestCallbackAsync)
37-
.AddLinkButton("Testar 2", "https://www.linkedin.com/in/dridev/")
38-
}
39-
};
17+
var message = new MessageBuilder()
18+
.WithContent("teste")
19+
.WithEmbed(embed => embed
20+
.SetTitle("Embed de teste")
21+
.SetDescription("Mensagem gerada com MessageBuilder + EmbedBuilder")
22+
.SetColor(0x5865F2)
23+
.AddField("Comando", "/test", inline: true))
24+
.WithActionRow(new ActionRowBuilder()
25+
.AddButton("Testar", "test_button", ButtonStyle.Primary, context, TestCallbackAsync)
26+
.AddLinkButton("Testar 2", "https://www.linkedin.com/in/dridev/")
27+
).Build();
4028

4129
await context.Response.SendMessageAsync(message, cancellation);
4230
}
4331

4432
private async Task TestCallbackAsync(InteractionContext context)
4533
{
46-
await context.Response.SendMessageAsync(new Message()
47-
{
48-
Content = "Teste",
49-
});
34+
await context.Response.SendMessageAsync(new MessageBuilder()
35+
.WithContent("Teste")
36+
.WithEmbed(embed => embed
37+
.SetTitle("Callback executado")
38+
.SetDescription("Resposta criada via MessageBuilder")
39+
.SetColor(0x57F287))
40+
.Build());
5041
}
5142

5243
private async Task TestCallback3Async(InteractionContext context)

0 commit comments

Comments
 (0)