Skip to content

Commit ff55dce

Browse files
authored
Feature/fixed presentation (#2)
* Fixed issues with subscription * Changed dev settings
1 parent f165e20 commit ff55dce

File tree

7 files changed

+96
-88
lines changed

7 files changed

+96
-88
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# Created by https://www.toptal.com/developers/gitignore/api/osx,dotnetcore,windows
33
# Edit at https://www.toptal.com/developers/gitignore?templates=osx,dotnetcore,windows
44

5+
tuck.db
6+
Hangfire.db
7+
58
### DotnetCore ###
69
# .NET Core build folders
710
bin/

Modules/AdminModule.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@ public async Task Relay(ulong channelId, [Remainder] string msg) {
1818
await channel.SendMessageAsync(msg);
1919
}
2020
}
21+
22+
[Command("time")]
23+
public async Task Time() {
24+
if(Context.User.Id == 103492791069327360) {
25+
await ReplyAsync(DateTime.Now.ToString());
26+
}
27+
}
2128
}
2229
}

Modules/SubscriptionModule.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using System.Text.RegularExpressions;
5+
using Discord.Commands;
6+
using Tuck.Model;
7+
using System.Collections.Generic;
8+
using Discord;
9+
using Hangfire;
10+
using Tuck.Services;
11+
12+
namespace Tuck.Modules
13+
{
14+
[Group("wbuff")]
15+
public class SubscriptionModule : ModuleBase<SocketCommandContext>
16+
{
17+
[Command("subscribe")]
18+
[RequireContext(ContextType.Guild)]
19+
public async Task AddSubscription(ulong guildId) {
20+
using(var context = new TuckContext()) {
21+
22+
if(context.Subscriptions.AsQueryable().Where(s => s.GuildId == Context.Guild.Id).Any()) {
23+
await ReplyAsync("The subscription was **not** added. A subscription already exists. Do unsubscribe to change the server subscribe too.");
24+
return;
25+
}
26+
27+
var subscription = new Subscription() {
28+
GuildId = Context.Guild.Id,
29+
ChannelId = Context.Channel.Id,
30+
TargetGuildId = guildId
31+
};
32+
33+
await context.AddAsync(subscription);
34+
await context.SaveChangesAsync();
35+
await ReplyAsync("The subscription was added");
36+
}
37+
}
38+
39+
[Command("unsubscribe")]
40+
[RequireContext(ContextType.Guild)]
41+
public async Task Subscription() {
42+
using(var context = new TuckContext()) {
43+
var mathces = context.Subscriptions.AsQueryable().Where(t => t.GuildId == Context.Guild.Id);
44+
context.RemoveRange(mathces);
45+
await context.SaveChangesAsync();
46+
await ReplyAsync("The subscription was removed");
47+
}
48+
}
49+
}
50+
}

Modules/WorldBuffModule.cs

Lines changed: 30 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class WorldBuffModule : ModuleBase<SocketCommandContext>
2020
{BuffType.Ony, 6},
2121
{BuffType.Rend, 2}
2222
};
23-
2423
private static Dictionary<BuffType, string> _icons = new Dictionary<BuffType, string> {
2524
{BuffType.Ony, "<:ony1:724665644641026068>"},
2625
{BuffType.Nef, "<:nef1:724665563967782923>"},
@@ -29,31 +28,23 @@ public class WorldBuffModule : ModuleBase<SocketCommandContext>
2928
};
3029

3130
[Command("list")]
31+
[Alias("overview")]
3232
[RequireContext(ContextType.Guild)]
3333
public async Task PostBuffOverview() {
3434
using(var context = new TuckContext()) {
35-
await ReplyAsync(GetBuffPost(context, Context.Guild.Id));
36-
}
37-
}
38-
39-
[Command("overview")]
40-
[RequireContext(ContextType.Guild)]
41-
public async Task PostBuffOverviewFromSubscribedGuilds() {
42-
using(var context = new TuckContext()) {
43-
var subscriptions = context.Subscriptions.AsQueryable()
35+
var subscription = context.Subscriptions.AsQueryable()
4436
.Where(s => s.GuildId == Context.Guild.Id)
45-
.ToList();
37+
.FirstOrDefault();
4638

47-
foreach(var subscription in subscriptions) {
48-
await ReplyAsync(GetBuffPost(context, subscription.TargetGuildId));
49-
}
39+
await ReplyAsync(GetBuffPost(context, subscription?.TargetGuildId ?? Context.Guild.Id));
5040
}
5141
}
5242

5343
[Command("add")]
5444
[RequireContext(ContextType.Guild)]
5545
public async Task RegisterBuff(BuffType type, DateTime time) {
56-
await RegisterBuff(type, time, Context.Guild.GetUser(Context.User.Id).Nickname);
46+
var user = Context.Guild.GetUser(Context.User.Id);
47+
await RegisterBuff(type, time, user.Nickname ?? user.Username);
5748
}
5849

5950
[Command("add")]
@@ -67,6 +58,15 @@ public async Task RegisterBuff(BuffType type, DateTime time, [Remainder] string
6758

6859
using(var context = new TuckContext()) {
6960

61+
var subscription = context.Subscriptions.AsQueryable()
62+
.Where(s => s.GuildId == Context.Guild.Id)
63+
.Any();
64+
65+
if(subscription) {
66+
await ReplyAsync("You cannot add buffs if a subscription exists");
67+
return;
68+
}
69+
7070
var buff = new BuffInstance {
7171
GuildId = Context.Guild.Id,
7272
UserId = Context.User.Id,
@@ -100,7 +100,7 @@ public async Task RegisterBuff(BuffType type, DateTime time, [Remainder] string
100100
}
101101

102102
// Adding a job for later to make notifications
103-
var notification = $"{_icons[buff.Type]} {buff.Type} is popping in {(buff.Time - DateTime.Now).Minutes} minutes by {buff.Username} @here";
103+
var notification = $"{_icons[buff.Type]} {buff.Type} is being popped in {(buff.Time - DateTime.Now).Minutes} minutes by {buff.Username}";
104104
buff.JobId = NotificationService.PushNotification(buff.GuildId, notification, buff.Time - DateTime.Now - TimeSpan.FromMinutes(5));
105105

106106
// Saving the buff instance
@@ -120,86 +120,32 @@ public async Task RegisterBuff(BuffType type, DateTime time, [Remainder] string
120120
[RequireContext(ContextType.Guild)]
121121
public async Task RemoveBuff(BuffType type, DateTime time) {
122122
using(var context = new TuckContext()) {
123-
var mathces = context.Buffs.AsQueryable()
123+
var match = context.Buffs.AsQueryable()
124124
.Where(t => t.GuildId == Context.Guild.Id)
125125
.Where(t => t.Type == type && t.Time == time)
126-
.ToList();
127-
128-
foreach(var buff in mathces) {
129-
NotificationService.CancelNotification(buff.JobId);
130-
var update = $"{_icons[buff.Type]} {buff.Type} that was planned for {buff.Time.ToString("dddd")} at {buff.Time.ToString("HH:mm")} has been cancelled by <@{Context.User.Id}>.";
131-
NotificationService.PushNotification(buff.GuildId, update);
132-
}
126+
.FirstOrDefault();
133127

134-
context.RemoveRange(mathces);
135-
await context.SaveChangesAsync();
136-
await ReplyAsync(GetBuffPost(context, Context.Guild.Id));
137-
}
138-
}
139-
140-
[Command("subscribe")]
141-
[RequireContext(ContextType.Guild)]
142-
public async Task AddSubscription(ulong guildId) {
143-
using(var context = new TuckContext()) {
144-
var subscription = new Subscription() {
145-
GuildId = Context.Guild.Id,
146-
ChannelId = Context.Channel.Id,
147-
TargetGuildId = guildId
148-
};
149-
await context.AddAsync(subscription);
150-
await context.SaveChangesAsync();
151-
await ReplyAsync("The subscription was added");
152-
}
153-
}
154-
155-
[Command("unsubscribe")]
156-
[RequireContext(ContextType.Guild)]
157-
public async Task Subscription(ulong guildId) {
158-
using(var context = new TuckContext()) {
159-
var mathces = context.Subscriptions.AsQueryable()
160-
.Where(t => t.GuildId == Context.Guild.Id)
161-
.Where(t => t.TargetGuildId == guildId);
162-
context.RemoveRange(mathces);
163-
await context.SaveChangesAsync();
164-
await ReplyAsync("The subscription was removed");
165-
}
166-
}
167-
168-
[Command("clear")]
169-
[RequireContext(ContextType.Guild)]
170-
public async Task RemoveAllBuffs() {
171-
if(Context.User.Id == 103492791069327360) {
172-
using(var context = new TuckContext()) {
173-
var mathces = context.Buffs.AsQueryable()
174-
.Where(t => t.GuildId == Context.Guild.Id);
175-
176-
foreach(var buff in mathces) {
177-
NotificationService.CancelNotification(buff.JobId);
178-
var update = $"{_icons[buff.Type]} {buff.Type} that was planned for {buff.Time.ToString("dddd")} at {buff.Time.ToString("HH:mm")} has been cancelled by <@{Context.User.Id}>.";
179-
NotificationService.PushNotification(buff.GuildId, update);
180-
}
181-
182-
context.RemoveRange(mathces);
128+
if(match != null){
129+
NotificationService.CancelNotification(match.JobId);
130+
var update = $"{_icons[match.Type]} {match.Type} that was planned for {match.Time.ToString("dddd")} at {match.Time.ToString("HH:mm")} has been cancelled by <@{Context.User.Id}>.";
131+
NotificationService.PushNotification(match.GuildId, update);
132+
context.Remove(match);
183133
await context.SaveChangesAsync();
134+
await ReplyAsync(GetBuffPost(context, Context.Guild.Id));
184135
}
185136
}
186137
}
187138

188139
private string GetBuffPost(TuckContext context, ulong guildId) {
189140
var buffs = context.Buffs.AsQueryable()
190141
.Where(t => t.GuildId == guildId && DateTime.Now < t.Time)
191-
.ToList();
142+
.OrderByDescending(t => t.Time).ThenBy(t => t.Type)
143+
.ToList()
144+
.Select(t => $"\n> {_icons[t.Type]} {t.Time.ToString("HH:mm")} by {t.Username}")
145+
.Aggregate("", (s1,s2) => s1 + s2);
192146

193-
var msg = "**World buff schedule:**\n";
194-
if(buffs.Count() > 0) {
195-
foreach(var type in buffs.GroupBy(t => t.Type)) {
196-
var times = type.OrderBy(t => t.Time).Select(e => $"{e.Time.ToString("HH:mm")} ({e.Username})");
197-
msg += $"> {_icons[type.Key]} " + String.Join(", ", times) + "\n";
198-
}
199-
} else {
200-
msg += "> Nothing have been added yet...";
201-
}
202-
return msg;
147+
return "**World buff schedule:**" +
148+
(string.IsNullOrEmpty(buffs) ? "\n> Nothing have been added yet..." : buffs);
203149
}
204150
}
205151
}

Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ private ServiceProvider ConfigureServices()
5858
.BuildServiceProvider();
5959
}
6060
}
61-
}
61+
}

Services/CommandHandlingService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Discord;
66
using Discord.Commands;
77
using Discord.WebSocket;
8-
using System.Text.RegularExpressions;
98
using Tuck.Readers;
109
using Tuck.Model;
1110

@@ -52,7 +51,7 @@ public async Task MessageReceivedAsync(SocketMessage rawMessage)
5251
await message.Channel.SendMessageAsync("Fuck you Sena");
5352
}
5453
if(message.Content.Contains("Death's Sting") || message.Content.Contains("item=21126")) {
55-
await message.Channel.SendMessageAsync("That weapon should be prio to Mondino!");
54+
await message.Channel.SendMessageAsync("That weapon should be prio to <@103492791069327360>!");
5655
}
5756
if(_random.Next(100) == 0) {
5857
var channel = await message.Author.GetOrCreateDMChannelAsync();
@@ -90,4 +89,4 @@ public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandCo
9089
await context.Channel.SendMessageAsync($"{result}");
9190
}
9291
}
93-
}
92+
}

Services/NotificationService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public NotificationService(IServiceProvider services) {
1818
}
1919

2020
public static string PushNotification(ulong guildId, string msg) {
21+
Console.WriteLine($"Notification added. ({msg})");
2122
return BackgroundJob.Enqueue<NotificationService>(ns => ns.HandleNotification(guildId, msg));
2223
}
2324

2425
public static string PushNotification(ulong guildId, string msg, TimeSpan time) {
26+
Console.WriteLine($"Notification added. ({time} )({msg})");
2527
return BackgroundJob.Schedule<NotificationService>(ns => ns.HandleNotification(guildId, msg), time);
2628
}
2729

@@ -38,6 +40,7 @@ private IEnumerable<Subscription> getSubscriptions(ulong guildId){
3840
}
3941

4042
public async Task HandleNotification(ulong guildId, string msg) {
43+
Console.WriteLine($"Now notifying. ({msg})");
4144
foreach(var subscription in getSubscriptions(guildId)) {
4245
var channel = _client.GetChannel(subscription.ChannelId) as ISocketMessageChannel;
4346
await channel.SendMessageAsync(msg);

0 commit comments

Comments
 (0)