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 79f7bb9..0b412e3 100644 Binary files a/.vs/Discord.NET-Template/v16/.suo and b/.vs/Discord.NET-Template/v16/.suo differ 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 e6da8dc..9cc38c8 100644 Binary files a/Template/obj/Debug/netcoreapp3.1/Template.csprojAssemblyReference.cache and b/Template/obj/Debug/netcoreapp3.1/Template.csprojAssemblyReference.cache differ diff --git a/Template/obj/Template.csproj.nuget.dgspec.json b/Template/obj/Template.csproj.nuget.dgspec.json index 3ed87a1..7df9206 100644 --- a/Template/obj/Template.csproj.nuget.dgspec.json +++ b/Template/obj/Template.csproj.nuget.dgspec.json @@ -22,7 +22,8 @@ ], "sources": { "C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {}, - "https://api.nuget.org/v3/index.json": {} + "https://api.nuget.org/v3/index.json": {}, + "https://www.myget.org/F/discord-net/api/v3/index.json": {} }, "frameworks": { "netcoreapp3.1": { @@ -68,7 +69,7 @@ "privateAssets": "all" } }, - "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.1.402\\RuntimeIdentifierGraph.json" + "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.1.403\\RuntimeIdentifierGraph.json" } } }, @@ -90,7 +91,8 @@ ], "sources": { "C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {}, - "https://api.nuget.org/v3/index.json": {} + "https://api.nuget.org/v3/index.json": {}, + "https://www.myget.org/F/discord-net/api/v3/index.json": {} }, "frameworks": { "netcoreapp3.1": { @@ -170,7 +172,7 @@ "privateAssets": "all" } }, - "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.1.402\\RuntimeIdentifierGraph.json" + "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.1.403\\RuntimeIdentifierGraph.json" } } } diff --git a/Template/obj/project.assets.json b/Template/obj/project.assets.json index d768274..e53b329 100644 --- a/Template/obj/project.assets.json +++ b/Template/obj/project.assets.json @@ -2307,7 +2307,8 @@ ], "sources": { "C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {}, - "https://api.nuget.org/v3/index.json": {} + "https://api.nuget.org/v3/index.json": {}, + "https://www.myget.org/F/discord-net/api/v3/index.json": {} }, "frameworks": { "netcoreapp3.1": { @@ -2387,7 +2388,7 @@ "privateAssets": "all" } }, - "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.1.402\\RuntimeIdentifierGraph.json" + "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.1.403\\RuntimeIdentifierGraph.json" } } } diff --git a/Template/obj/project.nuget.cache b/Template/obj/project.nuget.cache index 8827b5a..6bf27be 100644 --- a/Template/obj/project.nuget.cache +++ b/Template/obj/project.nuget.cache @@ -1,6 +1,6 @@ { "version": 2, - "dgSpecHash": "Nu560BapYOFCG2cyipplJ4/Essc+oF/CYX0K0QnO/JikOadRUUvDwgrwVVy7uqyxBxTMBOrGDknqy6NOCZaT8A==", + "dgSpecHash": "9EBiecKNovxTb2f5hTRQTe7IlwWYi1e7WXHQ0bqL4m0L7gzBtQJ0L/MKgoxWJBrT4zZXgBw7k/prhewSMTKABQ==", "success": true, "projectFilePath": "C:\\Users\\Efehan\\Documents\\Projects\\Bot Development Series\\Template\\Template.csproj", "expectedPackageFiles": [