Skip to content

Commit

Permalink
Merge pull request #416 from fit-ctu-discord/release-2022-4-3
Browse files Browse the repository at this point in the history
Release 2022.4.3
  • Loading branch information
stepech authored Oct 17, 2022
2 parents cb16b97 + 635e0df commit 23f1247
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2022.4.2</Version>
<Version>2022.4.3</Version>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
Expand Down
22 changes: 11 additions & 11 deletions Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
<ItemGroup>
<PackageReference Update="Chronic.Core" Version="0.4.0" />
<PackageReference Update="Cronos" Version="0.7.1" />
<PackageReference Update="DSharpPlus" Version="4.3.0-nightly-01169" />
<PackageReference Update="DSharpPlus.Interactivity" Version="4.3.0-nightly-01169" />
<PackageReference Update="DSharpPlus.SlashCommands" Version="4.3.0-nightly-01169" />
<PackageReference Update="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Update="Microsoft.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Update="Microsoft.EntityFrameworkCore.Tools" Version="6.0.8" />
<PackageReference Update="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Update="DSharpPlus" Version="4.3.0-nightly-01173" />
<PackageReference Update="DSharpPlus.Interactivity" Version="4.3.0-nightly-01173" />
<PackageReference Update="DSharpPlus.SlashCommands" Version="4.3.0-nightly-01173" />
<PackageReference Update="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.10" />
<PackageReference Update="Microsoft.EntityFrameworkCore" Version="6.0.10" />
<PackageReference Update="Microsoft.EntityFrameworkCore.Tools" Version="6.0.10" />
<PackageReference Update="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Update="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Update="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Update="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Update="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Update="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Update="Microsoft.Extensions.Logging.Abstractions" Version="6.0.2" />
<PackageReference Update="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Update="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.6" />
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Update="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.7" />
<PackageReference Update="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Update="System.Collections.Immutable" Version="6.0.0" />
<PackageReference Update="coverlet.collector" Version="3.1.2" />
<PackageReference Update="xunit" Version="2.4.2" />
<PackageReference Update="xunit.runner.visualstudio" Version="2.4.5" />
<PackageReference Update="Shouldly" Version="4.1.0" />
<PackageReference Update="Html2Markdown" Version="5.0.2.561" />
<PackageReference Update="Html2Markdown" Version="5.1.0.703" />
</ItemGroup>
</Project>
47 changes: 27 additions & 20 deletions src/HonzaBotner.Discord.Services/Commands/FunCommands.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using HonzaBotner.Discord.Services.Extensions;

namespace HonzaBotner.Discord.Services.Commands;

Expand All @@ -21,35 +20,43 @@ public async Task ChooseCommandAsync(
string delimiter = ","
)
{
var answers = options.Split(delimiter, StringSplitOptions.TrimEntries & StringSplitOptions.RemoveEmptyEntries)
.Select(option => option.Trim().RemoveDiscordMentions(ctx.Guild))
var answers = options.Split(delimiter)
.Select(option => option.Trim())
.Where(option => option != "").ToArray();
if (answers.Length == 0)
{
await ctx.CreateResponseAsync("Nope");
await ctx.CreateResponseAsync("Nope", true);
return;
}
Random random = new();
var text = new StringBuilder("I picked: ");
var winNumber = random.Next(answers.Length);
var winner = answers[winNumber];
text.Append("`" + winner + "`");
if (answers.Length > 1)

var response = new DiscordEmbedBuilder()
.WithAuthor(name: ctx.Member.DisplayName, iconUrl: ctx.User.AvatarUrl)
.WithColor(DiscordColor.DarkGreen);

Random random = Random.Shared;

var text = new StringBuilder($"I picked: {answers[random.Next(answers.Length)]}");
if (ctx.User.Id is 470490558713036801 or 302127992258428929)
{
text.Append("\nOptions were:\n");
foreach (var option in answers)
{
text.Append("`" + option+ "`, ");
}
response
.WithFooter("uwu", "https://cdn.discordapp.com/emojis/945812225208234066.png")
.WithColor(DiscordColor.HotPink);
}

text.Remove(text.Length - 2, 2);
if (answers.Length > 1)
{
text.AppendLine("\n\nOptions were:");
text.AppendJoin("\n", answers);
response.WithDescription(text.ToString());
await ctx.CreateResponseAsync(response.Build());
}
else
{
text.Append("\nPlease seperate the options by `,`");
await ctx.CreateResponseAsync(response.WithDescription(text.ToString()).Build());
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder()
.WithContent("Please separate the options by `,`")
.AsEphemeral());
}

await ctx.CreateResponseAsync(text.ToString());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
Expand Down
47 changes: 21 additions & 26 deletions src/HonzaBotner.Discord.Services/Commands/MessageCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using DSharpPlus.Exceptions;
using DSharpPlus.Interactivity.Extensions;
using DSharpPlus.SlashCommands;
using HonzaBotner.Discord.Services.Extensions;
using HonzaBotner.Services.Contract;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -35,36 +34,34 @@ public async Task SendMessageCommandAsync(
{
DiscordMessage? messageToSend = await DiscordHelper.FindMessageFromLink(ctx.Guild, link);

if (messageToSend is not null)
if (messageToSend is null)
{
try
{
string valueToSend = messageToSend.Content;
if (!mention)
{
valueToSend = valueToSend.RemoveDiscordMentions(ctx.Guild);
}

await channel.SendMessageAsync(valueToSend);
await ctx.CreateResponseAsync("Message sent");
}
catch (Exception e)
{
_logger.LogWarning(e, "Error during sending bot message");
await ctx.CreateResponseAsync("Error occured during message send, see log for more information");
}
await ctx.CreateResponseAsync("Could not find linked message, does the bot have access to that channel?");
return;
}

await ctx.CreateResponseAsync("Could not find linked message, does the bot have access to that channel?");
try
{
var content = new DiscordMessageBuilder()
.WithContent(messageToSend.Content)
.WithAllowedMentions(mention ? Mentions.All : Mentions.None);

await channel.SendMessageAsync(content);
await ctx.CreateResponseAsync("Message sent");
}
catch (Exception e)
{
_logger.LogWarning(e, "Error during sending bot message");
await ctx.CreateResponseAsync("Error occured during message send, see log for more information");
}
}

[SlashCommand("edit", "Edit previously sent text message authored by this bot.")]
public async Task EditMessageCommandAsync(
InteractionContext ctx,
[Option("old-message", "Link to the message you want to edit")] string originalUrl,
[Option("new-message", "Link to a message with new content")] string newUrl,
[Option("mention", "Should all mentions be included?")] bool mention = false)
[Option("mention", "Should all mentions be included? Default: false")] bool mention = false)
{
DiscordMessage? oldMessage = await DiscordHelper.FindMessageFromLink(ctx.Guild, originalUrl);
DiscordMessage? newMessage = await DiscordHelper.FindMessageFromLink(ctx.Guild, newUrl);
Expand All @@ -81,15 +78,13 @@ public async Task EditMessageCommandAsync(
return;
}

string newText = newMessage.Content;
if (!mention)
{
newText = newText.RemoveDiscordMentions(ctx.Guild);
}
var content = new DiscordMessageBuilder()
.WithContent(newMessage.Content)
.WithAllowedMentions(mention ? Mentions.All : Mentions.None);

try
{
await oldMessage.ModifyAsync(newText);
await oldMessage.ModifyAsync(content);
await ctx.CreateResponseAsync("Message successfully edited.");
}
catch (Exception e)
Expand Down
11 changes: 5 additions & 6 deletions src/HonzaBotner.Discord.Services/Commands/ModerationCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task WarnMenuAsync(ContextMenuContext ctx)
var modalReason = await interactivity.WaitForModalAsync(modalId, TimeSpan.FromMinutes(10));
if (modalReason.TimedOut)
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent("Response timed out"));
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent("Response timed out").AsEphemeral());
return;
}

Expand All @@ -72,7 +72,7 @@ await ctx.FollowUpAsync(
"\n\n" +
$"This is your **{numberOfWarnings + 1}. warning**, #beGood.";

await ctx.TargetMember.SendMessageAsync(messageForUser.RemoveDiscordMentions(ctx.Guild));
await ctx.TargetMember.SendMessageAsync(messageForUser);
}
catch (Exception e)
{
Expand All @@ -95,8 +95,8 @@ await modalReason.Result.Interaction.CreateResponseAsync(
await modalReason.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithContent("Announced"));
await buttonResponse.Result.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder()
.WithContent($"{ctx.TargetMember.Mention}" + $" was warned for \"{modalReason.Result.Values[reasonId]}\""
.RemoveDiscordMentions(ctx.Guild))
.WithContent($"{ctx.TargetMember.Mention}" + $" was warned for \"{modalReason.Result.Values[reasonId]}\"")
.AddMention(new UserMention(ctx.TargetMember.Id))
.AsEphemeral(false));
}
}
Expand All @@ -114,8 +114,7 @@ public async Task ShowCommandAsync(
}
else
{
await ctx.CreateResponseAsync($"**Warning {warning.Id}** for user <@{warning.UserId}>:\n" +
$"{warning.Reason.RemoveDiscordMentions(ctx.Guild)}");
await ctx.CreateResponseAsync($"**Warning {warning.Id}** for user <@{warning.UserId}>:\n" + $"{warning.Reason}");
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/HonzaBotner.Discord.Services/Commands/PollCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using HonzaBotner.Discord.Services.Commands.Polls;
using HonzaBotner.Discord.Services.Extensions;
using HonzaBotner.Discord.Services.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -51,10 +50,10 @@ public async Task AbcPollCommandAsync(
string delimiter = ","
)
{
await CreateDefaultPollAsync(ctx, question,
answers.Split(delimiter)
.Select(answer => answer.Trim().RemoveDiscordMentions(ctx.Guild))
.Where(answer => answer != "").ToList());
await CreateDefaultPollAsync(ctx, question, answers.Split(delimiter)
.Select(answer => answer.Trim())
.Where(answer => answer != "")
.ToList());
}

private async Task CreateDefaultPollAsync(InteractionContext ctx, string question, List<string>? answers = null)
Expand Down Expand Up @@ -92,7 +91,7 @@ public async Task AddPollOptionCommandAsync(
{
DiscordMessage? originalMessage = await DiscordHelper.FindMessageFromLink(ctx.Guild, link);
List<string> options = answers.Split(delimiter)
.Select(answer => answer.Trim().RemoveDiscordMentions(ctx.Guild))
.Select(answer => answer.Trim())
.Where(answer => answer != "").ToList();

if (originalMessage is null
Expand Down
8 changes: 4 additions & 4 deletions src/HonzaBotner.Discord.Services/Commands/Polls/AbcPoll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using HonzaBotner.Discord.Services.Extensions;

namespace HonzaBotner.Discord.Services.Commands.Polls;

Expand Down Expand Up @@ -74,12 +73,13 @@ public async Task AddOptionsAsync(DiscordClient client, IEnumerable<string> newO
throw new PollException($"Total number of reactions on a message can't be greater than {reactionCap}");
}
await ExistingPollMessage
.ModifyAsync(Modify(client, ExistingPollMessage.Channel.Guild, ExistingPollMessage.Embeds[0], emojisToAdd));
.ModifyAsync(new DiscordMessageBuilder()
.AddEmbed(Modify(client, ExistingPollMessage.Embeds[0], emojisToAdd)));

Task _ = Task.Run(async () => { await AddReactionsAsync(client, ExistingPollMessage, emojisToAdd); });
}

private DiscordEmbed Modify(DiscordClient client, DiscordGuild guild, DiscordEmbed original, IEnumerable<string> emojisToAdd)
private DiscordEmbed Modify(DiscordClient client, DiscordEmbed original, IEnumerable<string> emojisToAdd)
{
DiscordEmbedBuilder builder = new (original);

Expand All @@ -89,7 +89,7 @@ private DiscordEmbed Modify(DiscordClient client, DiscordGuild guild, DiscordEmb

builder.AddField(
DiscordEmoji.FromName(client, emojiName).ToString(),
answer.RemoveDiscordMentions(guild),
answer,
true);
});

Expand Down
10 changes: 5 additions & 5 deletions src/HonzaBotner.Discord.Services/Commands/Polls/Poll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using HonzaBotner.Discord.Services.Extensions;

namespace HonzaBotner.Discord.Services.Commands.Polls;

Expand Down Expand Up @@ -45,7 +44,8 @@ protected Poll(DiscordMessage originalMessage)

public async Task PostAsync(DiscordClient client, DiscordChannel channel)
{
DiscordMessage pollMessage = await client.SendMessageAsync(channel, Build(client, channel.Guild));
DiscordMessage pollMessage = await channel.SendMessageAsync(new DiscordMessageBuilder()
.AddEmbed(Build(client)));

Task _ = Task.Run(async () => { await AddReactionsAsync(client, pollMessage); });
}
Expand All @@ -58,7 +58,7 @@ protected async Task AddReactionsAsync(DiscordClient client, DiscordMessage mess
}
}

private DiscordEmbed Build(DiscordClient client, DiscordGuild guild)
private DiscordEmbed Build(DiscordClient client)
{
if (NewChoices.Count > OptionsEmoji.Count)
{
Expand All @@ -67,7 +67,7 @@ private DiscordEmbed Build(DiscordClient client, DiscordGuild guild)

DiscordEmbedBuilder builder = new()
{
Title = Question.RemoveDiscordMentions(guild),
Title = Question,
Description = "By: " + AuthorMention // Author needs to stay as the last argument
};

Expand All @@ -77,7 +77,7 @@ private DiscordEmbed Build(DiscordClient client, DiscordGuild guild)

builder.AddField(
DiscordEmoji.FromName(client, emojiName).ToString(),
answer.RemoveDiscordMentions(guild),
answer,
true);
});

Expand Down
17 changes: 9 additions & 8 deletions src/HonzaBotner.Discord.Services/Commands/ReminderCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using HonzaBotner.Discord.Managers;
using HonzaBotner.Discord.Services.Extensions;
using HonzaBotner.Discord.Services.Options;
using HonzaBotner.Services.Contract;
using HonzaBotner.Services.Contract.Dto;
Expand Down Expand Up @@ -60,19 +59,21 @@ await ctx.CreateResponseAsync(
return;
}

DiscordMessage followup = await ctx.Channel.SendMessageAsync("❤");
DiscordMessage reminderMessage = await ctx.Channel.SendMessageAsync("❤");

Reminder reminder = await _service.CreateReminderAsync(
ctx.User.Id,
followup.Id,
followup.ChannelId,
reminderMessage.Id,
reminderMessage.ChannelId,
datetime.Value.ToUniversalTime(), // This is safe, as the nullability is validated above
content.RemoveDiscordMentions(ctx.Guild)
content
);

followup = await followup.ModifyAsync("",await _reminderManager.CreateReminderEmbedAsync(reminder));
await followup.CreateReactionAsync(DiscordEmoji.FromUnicode(_options.CancelEmojiName));
await followup.CreateReactionAsync(DiscordEmoji.FromUnicode(_options.JoinEmojiName));
reminderMessage = await reminderMessage.ModifyAsync(new DiscordMessageBuilder()
.WithContent("")
.AddEmbed(await _reminderManager.CreateReminderEmbedAsync(reminder)));
await reminderMessage.CreateReactionAsync(DiscordEmoji.FromUnicode(_options.CancelEmojiName));
await reminderMessage.CreateReactionAsync(DiscordEmoji.FromUnicode(_options.JoinEmojiName));
await ctx.CreateResponseAsync("Reminder created", true);
}

Expand Down
Loading

0 comments on commit 23f1247

Please sign in to comment.