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

Commit a6bc75b

Browse files
author
Directoire
committed
Episode 9
1 parent 1ab5b0c commit a6bc75b

File tree

14 files changed

+293
-14
lines changed

14 files changed

+293
-14
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ x86/
2727
[Aa][Rr][Mm]/
2828
[Aa][Rr][Mm]64/
2929
bld/
30-
[Bb]in/
31-
[Oo]bj/
30+
bin/
31+
obj/
3232
[Ll]og/
3333
[Ll]ogs/
3434

.vs/Discord.NET-Template/v16/.suo

17 KB
Binary file not shown.

Infrastructure/Migrations/20201026124837_ThirdVersion.Designer.cs

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
namespace Infrastructure.Migrations
4+
{
5+
public partial class ThirdVersion : Migration
6+
{
7+
protected override void Up(MigrationBuilder migrationBuilder)
8+
{
9+
migrationBuilder.AddColumn<string>(
10+
name: "Background",
11+
table: "Servers",
12+
nullable: true);
13+
14+
migrationBuilder.AddColumn<ulong>(
15+
name: "Welcome",
16+
table: "Servers",
17+
nullable: false,
18+
defaultValue: 0ul);
19+
}
20+
21+
protected override void Down(MigrationBuilder migrationBuilder)
22+
{
23+
migrationBuilder.DropColumn(
24+
name: "Background",
25+
table: "Servers");
26+
27+
migrationBuilder.DropColumn(
28+
name: "Welcome",
29+
table: "Servers");
30+
}
31+
}
32+
}

Infrastructure/Migrations/TutorialContextModelSnapshot.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
5656
.ValueGeneratedOnAdd()
5757
.HasColumnType("bigint unsigned");
5858

59+
b.Property<string>("Background")
60+
.HasColumnType("longtext CHARACTER SET utf8mb4");
61+
5962
b.Property<string>("Prefix")
6063
.HasColumnType("longtext CHARACTER SET utf8mb4");
6164

65+
b.Property<ulong>("Welcome")
66+
.HasColumnType("bigint unsigned");
67+
6268
b.HasKey("Id");
6369

6470
b.ToTable("Servers");

Infrastructure/Servers.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,65 @@ public async Task<string> GetGuildPrefix(ulong id)
3838

3939
return await Task.FromResult(prefix);
4040
}
41+
42+
public async Task ModifyWelcomeAsync(ulong id, ulong channelId)
43+
{
44+
var server = await _context.Servers
45+
.FindAsync(id);
46+
47+
if (server == null)
48+
_context.Add(new Server { Id = id, Welcome = channelId });
49+
else
50+
server.Welcome = channelId;
51+
52+
await _context.SaveChangesAsync();
53+
}
54+
55+
public async Task ClearWelcomeAsync(ulong id)
56+
{
57+
var server = await _context.Servers
58+
.FindAsync(id);
59+
60+
server.Welcome = 0;
61+
await _context.SaveChangesAsync();
62+
}
63+
64+
public async Task<ulong> GetWelcomeAsync(ulong id)
65+
{
66+
var server = await _context.Servers
67+
.FindAsync(id);
68+
69+
return await Task.FromResult(server.Welcome);
70+
}
71+
72+
public async Task ModifyBackgroundAsync(ulong id, string url)
73+
{
74+
var server = await _context.Servers
75+
.FindAsync(id);
76+
77+
if (server == null)
78+
_context.Add(new Server { Id = id, Background = url });
79+
else
80+
server.Background = url;
81+
82+
await _context.SaveChangesAsync();
83+
}
84+
85+
public async Task ClearBackgroundAsync(ulong id)
86+
{
87+
var server = await _context.Servers
88+
.FindAsync(id);
89+
90+
server.Background = null;
91+
await _context.SaveChangesAsync();
92+
}
93+
94+
public async Task<string> GetBackgroundAsync(ulong id)
95+
{
96+
var server = await _context.Servers
97+
.FindAsync(id);
98+
99+
return await Task.FromResult(server.Background);
100+
}
41101
}
42102
}

Infrastructure/TutorialContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Server
1717
{
1818
public ulong Id { get; set; }
1919
public string Prefix { get; set; }
20+
public ulong Welcome { get; set; }
21+
public string Background { get; set; }
2022
}
2123

2224
public class Rank

Template/Modules/Configuration.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Net.Http;
78
using System.Text;
89
using System.Threading.Tasks;
910
using Template.Utilities;
@@ -203,5 +204,80 @@ public async Task DelAutoRole([Remainder] string name)
203204
await _autoRoles.RemoveAutoRoleAsync(Context.Guild.Id, role.Id);
204205
await ReplyAsync($"The role {role.Mention} has been removed from the autoroles!");
205206
}
207+
208+
[Command("welcome")]
209+
[RequireUserPermission(GuildPermission.Administrator)]
210+
public async Task Welcome(string option = null, string value = null)
211+
{
212+
if(option == null && value == null)
213+
{
214+
var fetchedChannelId = await _servers.GetWelcomeAsync(Context.Guild.Id);
215+
if(fetchedChannelId == 0)
216+
{
217+
await ReplyAsync("There has not been set a welcome channel yet!");
218+
return;
219+
}
220+
221+
var fetchedChannel = Context.Guild.GetTextChannel(fetchedChannelId);
222+
if(fetchedChannel == null)
223+
{
224+
await ReplyAsync("There has not been set a welcome channel yet!");
225+
await _servers.ClearWelcomeAsync(Context.Guild.Id);
226+
return;
227+
}
228+
229+
var fetchedBackground = await _servers.GetBackgroundAsync(Context.Guild.Id);
230+
231+
if (fetchedBackground != null)
232+
await ReplyAsync($"The channel used for the welcome module is {fetchedChannel.Mention}.\nThe background is set to {fetchedBackground}.");
233+
else
234+
await ReplyAsync($"The channel used for the welcome module is {fetchedChannel.Mention}.");
235+
236+
return;
237+
}
238+
239+
if(option == "channel" && value != null)
240+
{
241+
if(!MentionUtils.TryParseChannel(value, out ulong parsedId))
242+
{
243+
await ReplyAsync("Please pass in a valid channel!");
244+
return;
245+
}
246+
247+
var parsedChannel = Context.Guild.GetTextChannel(parsedId);
248+
if(parsedChannel == null)
249+
{
250+
await ReplyAsync("Please pass in a valid channel!");
251+
return;
252+
}
253+
254+
await _servers.ModifyWelcomeAsync(Context.Guild.Id, parsedId);
255+
await ReplyAsync($"Successfully modified the welcome channel to {parsedChannel.Mention}.");
256+
return;
257+
}
258+
259+
if (option == "background" && value != null)
260+
{
261+
if (value == "clear")
262+
{
263+
await _servers.ClearBackgroundAsync(Context.Guild.Id);
264+
await ReplyAsync("Successfully cleared the background for this server.");
265+
return;
266+
}
267+
268+
await _servers.ModifyBackgroundAsync(Context.Guild.Id, value);
269+
await ReplyAsync($"Successfully modified the background to {value}.");
270+
return;
271+
}
272+
273+
if(option == "clear" && value == null)
274+
{
275+
await _servers.ClearWelcomeAsync(Context.Guild.Id);
276+
await ReplyAsync("Successfully cleared the welcome channel.");
277+
return;
278+
}
279+
280+
await ReplyAsync("You did not use this command properly.");
281+
}
206282
}
207283
}

Template/Services/CommandHandler.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ public class CommandHandler : InitializedService
2121
private readonly IConfiguration _config;
2222
private readonly Servers _servers;
2323
private readonly AutoRolesHelper _autoRolesHelper;
24+
private readonly Images _images;
2425

25-
public CommandHandler(IServiceProvider provider, DiscordSocketClient client, CommandService service, IConfiguration config, Servers servers, AutoRolesHelper autoRolesHelper)
26+
public CommandHandler(IServiceProvider provider, DiscordSocketClient client, CommandService service, IConfiguration config, Servers servers, AutoRolesHelper autoRolesHelper, Images images)
2627
{
2728
_provider = provider;
2829
_client = client;
2930
_service = service;
3031
_config = config;
3132
_servers = servers;
3233
_autoRolesHelper = autoRolesHelper;
34+
_images = images;
3335
}
3436

3537
public override async Task InitializeAsync(CancellationToken cancellationToken)
@@ -42,12 +44,33 @@ public override async Task InitializeAsync(CancellationToken cancellationToken)
4244
}
4345

4446
private async Task OnUserJoined(SocketGuildUser arg)
47+
{
48+
var newTask = new Task(async () => await HandleUserJoined(arg));
49+
newTask.Start();
50+
}
51+
52+
private async Task HandleUserJoined(SocketGuildUser arg)
4553
{
4654
var roles = await _autoRolesHelper.GetAutoRolesAsync(arg.Guild);
47-
if (roles.Count < 1)
55+
if (roles.Count > 0)
56+
await arg.AddRolesAsync(roles);
57+
58+
var channelId = await _servers.GetWelcomeAsync(arg.Guild.Id);
59+
if (channelId == 0)
4860
return;
4961

50-
await arg.AddRolesAsync(roles);
62+
var channel = arg.Guild.GetTextChannel(channelId);
63+
if(channel == null)
64+
{
65+
await _servers.ClearWelcomeAsync(arg.Guild.Id);
66+
return;
67+
}
68+
69+
var background = await _servers.GetBackgroundAsync(arg.Guild.Id);
70+
string path = await _images.CreateImageAsync(arg, background);
71+
72+
await channel.SendFileAsync(path, null);
73+
System.IO.File.Delete(path);
5174
}
5275

5376
private async Task OnMessageReceived(SocketMessage arg)

Template/Utilities/Images.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ namespace Template.Utilities
1111
{
1212
public class Images
1313
{
14-
public async Task<string> CreateImageAsync(SocketGuildUser user)
14+
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")
1515
{
1616
var avatar = await FetchImageAsync(user.GetAvatarUrl(size: 2048, format: Discord.ImageFormat.Png) ?? user.GetDefaultAvatarUrl());
17-
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");
17+
var background = await FetchImageAsync(url);
1818

1919
background = CropToBanner(background);
2020
avatar = ClipImageToCircle(avatar);
Binary file not shown.

0 commit comments

Comments
 (0)