Skip to content
This repository has been archived by the owner on Dec 18, 2021. It is now read-only.

Commit

Permalink
Episode 9
Browse files Browse the repository at this point in the history
  • Loading branch information
Directoire committed Nov 3, 2020
1 parent 1ab5b0c commit a6bc75b
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
bin/
obj/
[Ll]og/
[Ll]ogs/

Expand Down
Binary file modified .vs/Discord.NET-Template/v16/.suo
Binary file not shown.
77 changes: 77 additions & 0 deletions Infrastructure/Migrations/20201026124837_ThirdVersion.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions Infrastructure/Migrations/20201026124837_ThirdVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace Infrastructure.Migrations
{
public partial class ThirdVersion : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Background",
table: "Servers",
nullable: true);

migrationBuilder.AddColumn<ulong>(
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");
}
}
}
6 changes: 6 additions & 0 deletions Infrastructure/Migrations/TutorialContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.ValueGeneratedOnAdd()
.HasColumnType("bigint unsigned");

b.Property<string>("Background")
.HasColumnType("longtext CHARACTER SET utf8mb4");

b.Property<string>("Prefix")
.HasColumnType("longtext CHARACTER SET utf8mb4");

b.Property<ulong>("Welcome")
.HasColumnType("bigint unsigned");

b.HasKey("Id");

b.ToTable("Servers");
Expand Down
60 changes: 60 additions & 0 deletions Infrastructure/Servers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,65 @@ public async Task<string> 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<ulong> 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<string> GetBackgroundAsync(ulong id)
{
var server = await _context.Servers
.FindAsync(id);

return await Task.FromResult(server.Background);
}
}
}
2 changes: 2 additions & 0 deletions Infrastructure/TutorialContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions Template/Modules/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}
}
}
29 changes: 26 additions & 3 deletions Template/Services/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ 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;
_service = service;
_config = config;
_servers = servers;
_autoRolesHelper = autoRolesHelper;
_images = images;
}

public override async Task InitializeAsync(CancellationToken cancellationToken)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Template/Utilities/Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace Template.Utilities
{
public class Images
{
public async Task<string> CreateImageAsync(SocketGuildUser user)
public async Task<string> 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);
Expand Down
Binary file not shown.
Loading

0 comments on commit a6bc75b

Please sign in to comment.