@@ -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}
0 commit comments