Skip to content

Commit

Permalink
Feature/fixed presentation (#2)
Browse files Browse the repository at this point in the history
* Fixed issues with subscription

* Changed dev settings
  • Loading branch information
cholewa1992 authored Jun 28, 2020
1 parent f165e20 commit ff55dce
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Created by https://www.toptal.com/developers/gitignore/api/osx,dotnetcore,windows
# Edit at https://www.toptal.com/developers/gitignore?templates=osx,dotnetcore,windows

tuck.db
Hangfire.db

### DotnetCore ###
# .NET Core build folders
bin/
Expand Down
7 changes: 7 additions & 0 deletions Modules/AdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,12 @@ public async Task Relay(ulong channelId, [Remainder] string msg) {
await channel.SendMessageAsync(msg);
}
}

[Command("time")]
public async Task Time() {
if(Context.User.Id == 103492791069327360) {
await ReplyAsync(DateTime.Now.ToString());
}
}
}
}
50 changes: 50 additions & 0 deletions Modules/SubscriptionModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Discord.Commands;
using Tuck.Model;
using System.Collections.Generic;
using Discord;
using Hangfire;
using Tuck.Services;

namespace Tuck.Modules
{
[Group("wbuff")]
public class SubscriptionModule : ModuleBase<SocketCommandContext>
{
[Command("subscribe")]
[RequireContext(ContextType.Guild)]
public async Task AddSubscription(ulong guildId) {
using(var context = new TuckContext()) {

if(context.Subscriptions.AsQueryable().Where(s => s.GuildId == Context.Guild.Id).Any()) {
await ReplyAsync("The subscription was **not** added. A subscription already exists. Do unsubscribe to change the server subscribe too.");
return;
}

var subscription = new Subscription() {
GuildId = Context.Guild.Id,
ChannelId = Context.Channel.Id,
TargetGuildId = guildId
};

await context.AddAsync(subscription);
await context.SaveChangesAsync();
await ReplyAsync("The subscription was added");
}
}

[Command("unsubscribe")]
[RequireContext(ContextType.Guild)]
public async Task Subscription() {
using(var context = new TuckContext()) {
var mathces = context.Subscriptions.AsQueryable().Where(t => t.GuildId == Context.Guild.Id);
context.RemoveRange(mathces);
await context.SaveChangesAsync();
await ReplyAsync("The subscription was removed");
}
}
}
}
114 changes: 30 additions & 84 deletions Modules/WorldBuffModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class WorldBuffModule : ModuleBase<SocketCommandContext>
{BuffType.Ony, 6},
{BuffType.Rend, 2}
};

private static Dictionary<BuffType, string> _icons = new Dictionary<BuffType, string> {
{BuffType.Ony, "<:ony1:724665644641026068>"},
{BuffType.Nef, "<:nef1:724665563967782923>"},
Expand All @@ -29,31 +28,23 @@ public class WorldBuffModule : ModuleBase<SocketCommandContext>
};

[Command("list")]
[Alias("overview")]
[RequireContext(ContextType.Guild)]
public async Task PostBuffOverview() {
using(var context = new TuckContext()) {
await ReplyAsync(GetBuffPost(context, Context.Guild.Id));
}
}

[Command("overview")]
[RequireContext(ContextType.Guild)]
public async Task PostBuffOverviewFromSubscribedGuilds() {
using(var context = new TuckContext()) {
var subscriptions = context.Subscriptions.AsQueryable()
var subscription = context.Subscriptions.AsQueryable()
.Where(s => s.GuildId == Context.Guild.Id)
.ToList();
.FirstOrDefault();

foreach(var subscription in subscriptions) {
await ReplyAsync(GetBuffPost(context, subscription.TargetGuildId));
}
await ReplyAsync(GetBuffPost(context, subscription?.TargetGuildId ?? Context.Guild.Id));
}
}

[Command("add")]
[RequireContext(ContextType.Guild)]
public async Task RegisterBuff(BuffType type, DateTime time) {
await RegisterBuff(type, time, Context.Guild.GetUser(Context.User.Id).Nickname);
var user = Context.Guild.GetUser(Context.User.Id);
await RegisterBuff(type, time, user.Nickname ?? user.Username);
}

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

using(var context = new TuckContext()) {

var subscription = context.Subscriptions.AsQueryable()
.Where(s => s.GuildId == Context.Guild.Id)
.Any();

if(subscription) {
await ReplyAsync("You cannot add buffs if a subscription exists");
return;
}

var buff = new BuffInstance {
GuildId = Context.Guild.Id,
UserId = Context.User.Id,
Expand Down Expand Up @@ -100,7 +100,7 @@ public async Task RegisterBuff(BuffType type, DateTime time, [Remainder] string
}

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

// Saving the buff instance
Expand All @@ -120,86 +120,32 @@ public async Task RegisterBuff(BuffType type, DateTime time, [Remainder] string
[RequireContext(ContextType.Guild)]
public async Task RemoveBuff(BuffType type, DateTime time) {
using(var context = new TuckContext()) {
var mathces = context.Buffs.AsQueryable()
var match = context.Buffs.AsQueryable()
.Where(t => t.GuildId == Context.Guild.Id)
.Where(t => t.Type == type && t.Time == time)
.ToList();

foreach(var buff in mathces) {
NotificationService.CancelNotification(buff.JobId);
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}>.";
NotificationService.PushNotification(buff.GuildId, update);
}
.FirstOrDefault();

context.RemoveRange(mathces);
await context.SaveChangesAsync();
await ReplyAsync(GetBuffPost(context, Context.Guild.Id));
}
}

[Command("subscribe")]
[RequireContext(ContextType.Guild)]
public async Task AddSubscription(ulong guildId) {
using(var context = new TuckContext()) {
var subscription = new Subscription() {
GuildId = Context.Guild.Id,
ChannelId = Context.Channel.Id,
TargetGuildId = guildId
};
await context.AddAsync(subscription);
await context.SaveChangesAsync();
await ReplyAsync("The subscription was added");
}
}

[Command("unsubscribe")]
[RequireContext(ContextType.Guild)]
public async Task Subscription(ulong guildId) {
using(var context = new TuckContext()) {
var mathces = context.Subscriptions.AsQueryable()
.Where(t => t.GuildId == Context.Guild.Id)
.Where(t => t.TargetGuildId == guildId);
context.RemoveRange(mathces);
await context.SaveChangesAsync();
await ReplyAsync("The subscription was removed");
}
}

[Command("clear")]
[RequireContext(ContextType.Guild)]
public async Task RemoveAllBuffs() {
if(Context.User.Id == 103492791069327360) {
using(var context = new TuckContext()) {
var mathces = context.Buffs.AsQueryable()
.Where(t => t.GuildId == Context.Guild.Id);

foreach(var buff in mathces) {
NotificationService.CancelNotification(buff.JobId);
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}>.";
NotificationService.PushNotification(buff.GuildId, update);
}

context.RemoveRange(mathces);
if(match != null){
NotificationService.CancelNotification(match.JobId);
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}>.";
NotificationService.PushNotification(match.GuildId, update);
context.Remove(match);
await context.SaveChangesAsync();
await ReplyAsync(GetBuffPost(context, Context.Guild.Id));
}
}
}

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

var msg = "**World buff schedule:**\n";
if(buffs.Count() > 0) {
foreach(var type in buffs.GroupBy(t => t.Type)) {
var times = type.OrderBy(t => t.Time).Select(e => $"{e.Time.ToString("HH:mm")} ({e.Username})");
msg += $"> {_icons[type.Key]} " + String.Join(", ", times) + "\n";
}
} else {
msg += "> Nothing have been added yet...";
}
return msg;
return "**World buff schedule:**" +
(string.IsNullOrEmpty(buffs) ? "\n> Nothing have been added yet..." : buffs);
}
}
}
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ private ServiceProvider ConfigureServices()
.BuildServiceProvider();
}
}
}
}
5 changes: 2 additions & 3 deletions Services/CommandHandlingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System.Text.RegularExpressions;
using Tuck.Readers;
using Tuck.Model;

Expand Down Expand Up @@ -52,7 +51,7 @@ public async Task MessageReceivedAsync(SocketMessage rawMessage)
await message.Channel.SendMessageAsync("Fuck you Sena");
}
if(message.Content.Contains("Death's Sting") || message.Content.Contains("item=21126")) {
await message.Channel.SendMessageAsync("That weapon should be prio to Mondino!");
await message.Channel.SendMessageAsync("That weapon should be prio to <@103492791069327360>!");
}
if(_random.Next(100) == 0) {
var channel = await message.Author.GetOrCreateDMChannelAsync();
Expand Down Expand Up @@ -90,4 +89,4 @@ public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandCo
await context.Channel.SendMessageAsync($"{result}");
}
}
}
}
3 changes: 3 additions & 0 deletions Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public NotificationService(IServiceProvider services) {
}

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

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

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

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

0 comments on commit ff55dce

Please sign in to comment.