From a6bc75b2422fe91522c39c0ebdc2e9c6a763502c Mon Sep 17 00:00:00 2001 From: Directoire Date: Tue, 3 Nov 2020 22:45:17 +0100 Subject: [PATCH] Episode 9 --- .gitignore | 4 +- .vs/Discord.NET-Template/v16/.suo | Bin 51712 -> 69120 bytes .../20201026124837_ThirdVersion.Designer.cs | 77 ++++++++++++++++++ .../Migrations/20201026124837_ThirdVersion.cs | 32 ++++++++ .../TutorialContextModelSnapshot.cs | 6 ++ Infrastructure/Servers.cs | 60 ++++++++++++++ Infrastructure/TutorialContext.cs | 2 + Template/Modules/Configuration.cs | 76 +++++++++++++++++ Template/Services/CommandHandler.cs | 29 ++++++- Template/Utilities/Images.cs | 4 +- .../Template.csprojAssemblyReference.cache | Bin 22330 -> 22330 bytes .../obj/Template.csproj.nuget.dgspec.json | 10 ++- Template/obj/project.assets.json | 5 +- Template/obj/project.nuget.cache | 2 +- 14 files changed, 293 insertions(+), 14 deletions(-) create mode 100644 Infrastructure/Migrations/20201026124837_ThirdVersion.Designer.cs create mode 100644 Infrastructure/Migrations/20201026124837_ThirdVersion.cs diff --git a/.gitignore b/.gitignore index 1ee5385..89c2b38 100644 --- a/.gitignore +++ b/.gitignore @@ -27,8 +27,8 @@ x86/ [Aa][Rr][Mm]/ [Aa][Rr][Mm]64/ bld/ -[Bb]in/ -[Oo]bj/ +bin/ +obj/ [Ll]og/ [Ll]ogs/ diff --git a/.vs/Discord.NET-Template/v16/.suo b/.vs/Discord.NET-Template/v16/.suo index 79f7bb91ef0064563abe36a52d5a1ff37f81c512..0b412e37dd9ef4e3876f9b1305d1eee907107517 100644 GIT binary patch delta 6805 zcmds53tW^{7XR+>mf;P8IN%@;c?>W!3{O!87*Rk41(PrbTFcB=)NjAqZ!L0YUu1*oV<{!$k%$6(UVqd5|_6O1xKS ztprHO4WCJXS{l-ch;&2-A`_uQj6q}}#v-y2Ba7`4gxT!1J~eEkSxIqEQ?V#U6UtV%LM~zA1?u z$TFiWmZ7PrIpHgGrqnFclCA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@rr0t{FK;55@}^Gu_Qa4r{s|m36W?+ z@kD~a0*PID(A49_{sdM2383pyK}}LPvS&O~)C-nO7~F)j7=lNPP8>Vt`t4VCd#PcD z;Gh55@imIKb~ku*P7X}EKTX*C<*BQ?MP3H&nh_7(yTax?RL94 zIRQ<U|0$)E0T@(N7C6JeXL40M7H zbX6+wUZmocQ!>-(I-`>_(_unRxK@>;vjE|i0D}K9nNN%D?)#}rmR{|)k;VP2V0g=k zpg+cTDz0RP}-db7OnJzQfN$v@OxouLxDf$Ri?*N&(yV z`hbwyBJ+AlY=?oV zKMEDCMxOmE6@{d(-0x6?FoPm!W(#K57_6Kf2(Ql`3TI}Yc93Rato|ui#LdSTUV?2i z$lP$;YypFLuODVdK)UfY__|O!w18WN6O0pS!9om|rMRd76Xkra7(%M2z^+a1jDu5~ z612iLl{u(3S3yXv4VqL>JtTKP<;ZRaZ5B@XgHyL@OC__ud-l6Ofrv@M?@I~Y>e*To6HrB zK2W4hV3&X$Y=E*;u`uV&UujiEItD>?&ahySJ^2d5p9QLo_DCj1<>}<%`E!mpz~9ru z)?oRRD2X0`wL__ACHn#%1#S@2(*CAU@KnTYf3S+?xJ_QRGth zN*{J#n+`qgN1f>px>)F6Q4B&&Rtv9v-Z7XKiX{;nltrprO|prD)Om-X=ZkUn^uIzZ z?~Cta&#AeSV8||&cGwj;gY(I*m~{n$ZnK^RA9^6L=L#WkUGJ%#aeF2+r_)vq oj|XK}5h(7Q2jQ`x748Xar|?&1;HNHM0Nq4E)1|9dnKRWt00AGFRR910 diff --git a/Infrastructure/Migrations/20201026124837_ThirdVersion.Designer.cs b/Infrastructure/Migrations/20201026124837_ThirdVersion.Designer.cs new file mode 100644 index 0000000..d89d356 --- /dev/null +++ b/Infrastructure/Migrations/20201026124837_ThirdVersion.Designer.cs @@ -0,0 +1,77 @@ +// +using Infrastructure; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace Infrastructure.Migrations +{ + [DbContext(typeof(TutorialContext))] + [Migration("20201026124837_ThirdVersion")] + partial class ThirdVersion + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.1.8") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("Infrastructure.AutoRole", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("RoleId") + .HasColumnType("bigint unsigned"); + + b.Property("ServerId") + .HasColumnType("bigint unsigned"); + + b.HasKey("Id"); + + b.ToTable("AutoRoles"); + }); + + modelBuilder.Entity("Infrastructure.Rank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("RoleId") + .HasColumnType("bigint unsigned"); + + b.Property("ServerId") + .HasColumnType("bigint unsigned"); + + b.HasKey("Id"); + + b.ToTable("Ranks"); + }); + + modelBuilder.Entity("Infrastructure.Server", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned"); + + b.Property("Background") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Prefix") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Welcome") + .HasColumnType("bigint unsigned"); + + b.HasKey("Id"); + + b.ToTable("Servers"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Infrastructure/Migrations/20201026124837_ThirdVersion.cs b/Infrastructure/Migrations/20201026124837_ThirdVersion.cs new file mode 100644 index 0000000..5baa366 --- /dev/null +++ b/Infrastructure/Migrations/20201026124837_ThirdVersion.cs @@ -0,0 +1,32 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace Infrastructure.Migrations +{ + public partial class ThirdVersion : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Background", + table: "Servers", + nullable: true); + + migrationBuilder.AddColumn( + name: "Welcome", + table: "Servers", + nullable: false, + defaultValue: 0ul); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Background", + table: "Servers"); + + migrationBuilder.DropColumn( + name: "Welcome", + table: "Servers"); + } + } +} diff --git a/Infrastructure/Migrations/TutorialContextModelSnapshot.cs b/Infrastructure/Migrations/TutorialContextModelSnapshot.cs index 1fb68f9..0ccb741 100644 --- a/Infrastructure/Migrations/TutorialContextModelSnapshot.cs +++ b/Infrastructure/Migrations/TutorialContextModelSnapshot.cs @@ -56,9 +56,15 @@ protected override void BuildModel(ModelBuilder modelBuilder) .ValueGeneratedOnAdd() .HasColumnType("bigint unsigned"); + b.Property("Background") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + b.Property("Prefix") .HasColumnType("longtext CHARACTER SET utf8mb4"); + b.Property("Welcome") + .HasColumnType("bigint unsigned"); + b.HasKey("Id"); b.ToTable("Servers"); diff --git a/Infrastructure/Servers.cs b/Infrastructure/Servers.cs index f1e4458..d3fba30 100644 --- a/Infrastructure/Servers.cs +++ b/Infrastructure/Servers.cs @@ -38,5 +38,65 @@ public async Task GetGuildPrefix(ulong id) return await Task.FromResult(prefix); } + + public async Task ModifyWelcomeAsync(ulong id, ulong channelId) + { + var server = await _context.Servers + .FindAsync(id); + + if (server == null) + _context.Add(new Server { Id = id, Welcome = channelId }); + else + server.Welcome = channelId; + + await _context.SaveChangesAsync(); + } + + public async Task ClearWelcomeAsync(ulong id) + { + var server = await _context.Servers + .FindAsync(id); + + server.Welcome = 0; + await _context.SaveChangesAsync(); + } + + public async Task GetWelcomeAsync(ulong id) + { + var server = await _context.Servers + .FindAsync(id); + + return await Task.FromResult(server.Welcome); + } + + public async Task ModifyBackgroundAsync(ulong id, string url) + { + var server = await _context.Servers + .FindAsync(id); + + if (server == null) + _context.Add(new Server { Id = id, Background = url }); + else + server.Background = url; + + await _context.SaveChangesAsync(); + } + + public async Task ClearBackgroundAsync(ulong id) + { + var server = await _context.Servers + .FindAsync(id); + + server.Background = null; + await _context.SaveChangesAsync(); + } + + public async Task GetBackgroundAsync(ulong id) + { + var server = await _context.Servers + .FindAsync(id); + + return await Task.FromResult(server.Background); + } } } diff --git a/Infrastructure/TutorialContext.cs b/Infrastructure/TutorialContext.cs index b369ca1..617f218 100644 --- a/Infrastructure/TutorialContext.cs +++ b/Infrastructure/TutorialContext.cs @@ -17,6 +17,8 @@ public class Server { public ulong Id { get; set; } public string Prefix { get; set; } + public ulong Welcome { get; set; } + public string Background { get; set; } } public class Rank diff --git a/Template/Modules/Configuration.cs b/Template/Modules/Configuration.cs index c69faed..4d40e5a 100644 --- a/Template/Modules/Configuration.cs +++ b/Template/Modules/Configuration.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Text; using System.Threading.Tasks; using Template.Utilities; @@ -203,5 +204,80 @@ public async Task DelAutoRole([Remainder] string name) await _autoRoles.RemoveAutoRoleAsync(Context.Guild.Id, role.Id); await ReplyAsync($"The role {role.Mention} has been removed from the autoroles!"); } + + [Command("welcome")] + [RequireUserPermission(GuildPermission.Administrator)] + public async Task Welcome(string option = null, string value = null) + { + if(option == null && value == null) + { + var fetchedChannelId = await _servers.GetWelcomeAsync(Context.Guild.Id); + if(fetchedChannelId == 0) + { + await ReplyAsync("There has not been set a welcome channel yet!"); + return; + } + + var fetchedChannel = Context.Guild.GetTextChannel(fetchedChannelId); + if(fetchedChannel == null) + { + await ReplyAsync("There has not been set a welcome channel yet!"); + await _servers.ClearWelcomeAsync(Context.Guild.Id); + return; + } + + var fetchedBackground = await _servers.GetBackgroundAsync(Context.Guild.Id); + + if (fetchedBackground != null) + await ReplyAsync($"The channel used for the welcome module is {fetchedChannel.Mention}.\nThe background is set to {fetchedBackground}."); + else + await ReplyAsync($"The channel used for the welcome module is {fetchedChannel.Mention}."); + + return; + } + + if(option == "channel" && value != null) + { + if(!MentionUtils.TryParseChannel(value, out ulong parsedId)) + { + await ReplyAsync("Please pass in a valid channel!"); + return; + } + + var parsedChannel = Context.Guild.GetTextChannel(parsedId); + if(parsedChannel == null) + { + await ReplyAsync("Please pass in a valid channel!"); + return; + } + + await _servers.ModifyWelcomeAsync(Context.Guild.Id, parsedId); + await ReplyAsync($"Successfully modified the welcome channel to {parsedChannel.Mention}."); + return; + } + + if (option == "background" && value != null) + { + if (value == "clear") + { + await _servers.ClearBackgroundAsync(Context.Guild.Id); + await ReplyAsync("Successfully cleared the background for this server."); + return; + } + + await _servers.ModifyBackgroundAsync(Context.Guild.Id, value); + await ReplyAsync($"Successfully modified the background to {value}."); + return; + } + + if(option == "clear" && value == null) + { + await _servers.ClearWelcomeAsync(Context.Guild.Id); + await ReplyAsync("Successfully cleared the welcome channel."); + return; + } + + await ReplyAsync("You did not use this command properly."); + } } } diff --git a/Template/Services/CommandHandler.cs b/Template/Services/CommandHandler.cs index 69f0848..d97ca6e 100644 --- a/Template/Services/CommandHandler.cs +++ b/Template/Services/CommandHandler.cs @@ -21,8 +21,9 @@ public class CommandHandler : InitializedService private readonly IConfiguration _config; private readonly Servers _servers; private readonly AutoRolesHelper _autoRolesHelper; + private readonly Images _images; - public CommandHandler(IServiceProvider provider, DiscordSocketClient client, CommandService service, IConfiguration config, Servers servers, AutoRolesHelper autoRolesHelper) + public CommandHandler(IServiceProvider provider, DiscordSocketClient client, CommandService service, IConfiguration config, Servers servers, AutoRolesHelper autoRolesHelper, Images images) { _provider = provider; _client = client; @@ -30,6 +31,7 @@ public CommandHandler(IServiceProvider provider, DiscordSocketClient client, Com _config = config; _servers = servers; _autoRolesHelper = autoRolesHelper; + _images = images; } public override async Task InitializeAsync(CancellationToken cancellationToken) @@ -42,12 +44,33 @@ public override async Task InitializeAsync(CancellationToken cancellationToken) } private async Task OnUserJoined(SocketGuildUser arg) + { + var newTask = new Task(async () => await HandleUserJoined(arg)); + newTask.Start(); + } + + private async Task HandleUserJoined(SocketGuildUser arg) { var roles = await _autoRolesHelper.GetAutoRolesAsync(arg.Guild); - if (roles.Count < 1) + if (roles.Count > 0) + await arg.AddRolesAsync(roles); + + var channelId = await _servers.GetWelcomeAsync(arg.Guild.Id); + if (channelId == 0) return; - await arg.AddRolesAsync(roles); + var channel = arg.Guild.GetTextChannel(channelId); + if(channel == null) + { + await _servers.ClearWelcomeAsync(arg.Guild.Id); + return; + } + + var background = await _servers.GetBackgroundAsync(arg.Guild.Id); + string path = await _images.CreateImageAsync(arg, background); + + await channel.SendFileAsync(path, null); + System.IO.File.Delete(path); } private async Task OnMessageReceived(SocketMessage arg) diff --git a/Template/Utilities/Images.cs b/Template/Utilities/Images.cs index 823c07d..0311e20 100644 --- a/Template/Utilities/Images.cs +++ b/Template/Utilities/Images.cs @@ -11,10 +11,10 @@ namespace Template.Utilities { public class Images { - public async Task CreateImageAsync(SocketGuildUser user) + public async Task CreateImageAsync(SocketGuildUser user, string url = "https://images.unsplash.com/photo-1602408959965-cbde35cfab50?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=933&q=80") { var avatar = await FetchImageAsync(user.GetAvatarUrl(size: 2048, format: Discord.ImageFormat.Png) ?? user.GetDefaultAvatarUrl()); - var background = await FetchImageAsync("https://images.unsplash.com/photo-1602408959965-cbde35cfab50?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=933&q=80"); + var background = await FetchImageAsync(url); background = CropToBanner(background); avatar = ClipImageToCircle(avatar); diff --git a/Template/obj/Debug/netcoreapp3.1/Template.csprojAssemblyReference.cache b/Template/obj/Debug/netcoreapp3.1/Template.csprojAssemblyReference.cache index e6da8dcee1c44accebe8da94a5626231be751189..9cc38c83341d6b2491eba97d2b3e0bda399078a3 100644 GIT binary patch delta 17 ZcmdnBj&avI#tr_AOz8rf0~ue20sucn29E#$ delta 17 ZcmdnBj&avI#tr_AOw)Nb2Qt141pq*O2D<