-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResinCommands.cs
225 lines (213 loc) · 12.9 KB
/
ResinCommands.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using PaimonBot.Extensions.DataModels;
using PaimonBot.Services;
using PaimonBot.Services.HelpFormatter;
using PaimonBot.Services.ResinHelper;
using Serilog;
using System;
using System.Text;
using System.Threading.Tasks;
namespace PaimonBot.Commands
{
[Group("resin")]
[Description("A set of commands to manipulate your current resin count! Make sure to create an account if you haven't!")]
[Category(CategoryName.Account)]
public class ResinCommands : BaseCommandModule
{
[GroupCommand]
[Description("Default command to return back your current resin amount!")]
[Cooldown(1, 3, CooldownBucketType.Channel)]
public async Task ResinCall(CommandContext ctx)
{
var traveler = SharedData.PaimonDB.GetTravelerBy("DiscordID", ctx.User.Id);
if (traveler != null)
{
var embed = ResinEmbed(traveler, ctx.User);
var messagebuilder = new DiscordMessageBuilder().AddEmbed(embed);
await ctx.Channel.SendMessageAsync(messagebuilder).ConfigureAwait(false);
}
else
await PaimonServices.SendRespondAsync(ctx, $"It seems you're not in the database, please create an account using `{SharedData.prefixes[0]}acc create` or to simply use the resin counter," +
$" please set your resin using `{SharedData.prefixes[0]}resin set [amount]`. {Emojis.CryEmote}", TimeSpan.FromSeconds(5)).ConfigureAwait(false);
}
[Command("set")]
[Description("Sets your resin amount into the database. If you do not have an account, a new Traveler account will be created.")]
[Cooldown(1, 3, CooldownBucketType.User)]
public async Task ResinSet(CommandContext ctx, int amount)
{
var traveler = SharedData.PaimonDB.GetTravelerBy("DiscordID", ctx.User.Id);
int prevRemindAmnt = 0; // Just in the case a resin remind was previous active
DiscordChannel dmChannelBuffer = null;
if (amount > 160)
amount = 160;
else if (amount < 0)
amount = 0;
// Removes an already existing timer
if (SharedData.resinTimers.Exists(timer => timer._discordID == ctx.User.Id))
{
var timer = SharedData.resinTimers.Find(timer => timer._discordID == ctx.User.Id);
// Previous Resin Remind check
if (timer._remind)
{
prevRemindAmnt = timer._remindAt;
dmChannelBuffer = timer.GetDmChannelInstance();
}
timer.StopAndDispose();
SharedData.resinTimers.Remove(timer);
Log.Information($"Previous Resin timer for User {ctx.User.Id} has been removed.");
}
if (traveler != null)
{
var updateTraveler = traveler;
updateTraveler.ResinAmount = amount;
updateTraveler.ResinUpdatedTime = DateTime.UtcNow;
// Starts a new ResinTimer
var aTimer = new ResinTimer(ctx.User.Id);
// If there was a previous remind
if (prevRemindAmnt > 0 && dmChannelBuffer != null) // Condition can only mean that there was a resin remind, unless im dumb af
aTimer.EnableRemind(prevRemindAmnt, dmChannelBuffer);
aTimer.Start();
SharedData.resinTimers.Add(aTimer);
aTimer.ResinCapped += PaimonServices.ATimer_ResinCapped;
Log.Information($"Resin Timer for {aTimer._discordID} just started!");
SharedData.PaimonDB.UpdateTraveler(traveler, "ResinAmount", updateTraveler.ResinAmount);
SharedData.PaimonDB.UpdateTraveler(traveler, "ResinUpdated", updateTraveler.ResinUpdatedTime);
await ctx.RespondAsync($"You now have **{updateTraveler.ResinAmount}** resin {Emojis.ResinEmote}. Please use `{SharedData.prefixes[0]}resin` to check your resin. {Emojis.HappyEmote}").ConfigureAwait(false);
}
else
{
Traveler newTraveler = new Traveler()
{
DiscordID = ctx.User.Id,
ResinAmount = amount,
ResinUpdatedTime = DateTime.UtcNow,
// Default Values for the 'empty' fields
ParaGadgetNextUse = null,
RealmCurrency = int.MinValue,
CurrencyUpdated = null
};
SharedData.PaimonDB.InsertTraveler(newTraveler);
// Starts a new ResinTimer
var aTimer = new ResinTimer(ctx.User.Id);
aTimer.Start();
SharedData.resinTimers.Add(aTimer);
aTimer.ResinCapped += PaimonServices.ATimer_ResinCapped;
Log.Information($"Resin Timer for {aTimer._discordID} just started!");
await ctx.RespondAsync($"A new resin user was made, you now have **{newTraveler.ResinAmount}** resin {Emojis.ResinEmote}. Please use `{SharedData.prefixes[0]}resin` to check your resin. {Emojis.HappyEmote}").ConfigureAwait(false);
}
}
[Command("reset")]
[Description("Instantly resets your current resin amount to 0. If you do not have an account, a new Traveler account will be created.")]
[Cooldown(1, 3, CooldownBucketType.User)]
public async Task ResinReset(CommandContext ctx)
{
await ResinSet(ctx, 0);
}
[Command("remind")]
[Description("Sets PaimonBot to remind you through DMs once your resin has reached that specific amount, defaults to 160 (Full) resin.")]
[Cooldown(1, 3, CooldownBucketType.User)]
[RequireGuild]
public async Task ResinRemind(CommandContext ctx, int amount = 160)
{
if (amount > 160 || amount < 0)
{ await PaimonServices.SendRespondAsync(ctx, "Please enter a resin amount between `0-160`. Please try again.", TimeSpan.FromSeconds(3)); return; }
var traveler = SharedData.PaimonDB.GetTravelerBy("DiscordID", ctx.User.Id);
if (traveler != null)
{
if (SharedData.resinTimers.Exists(x => x._discordID == ctx.User.Id))
{
var resinTimer = SharedData.resinTimers.Find(x => x._discordID == ctx.User.Id);
if (resinTimer._remind) // Already to be reminded.
{
await PaimonServices.SendEmbedToChannelAsync(ctx.Channel, "Resin Remind Error", $"It seems that you already have a Resin Remind at **{resinTimer._remindAt}** {Emojis.ResinEmote} **active**. To disable, please use " +
$"`{SharedData.prefixes[0]}resin unremind`. {Emojis.BlurpEmote}", TimeSpan.FromSeconds(6), ResponseType.Warning);
return;
}
try
{
var DmChannel = await ctx.Member.CreateDmChannelAsync().ConfigureAwait(false);
resinTimer.EnableRemind(amount, DmChannel);
Log.Information("Resin Remind for User {Id} Enabled, at {DateTime}; Resin: {Resin}", ctx.User.Id, DateTime.Now, resinTimer._remindAt);
await ctx.RespondAsync($"{Emojis.HappyEmote} Resin Remind Successful! Paimon will now remind you when your resin hits {resinTimer._remindAt} {Emojis.ResinEmote}. Be sure to keep DMs from server members enabled!").ConfigureAwait(false);
}
catch (Exception e)
{
if (e is UnauthorizedException)
{
await PaimonServices.SendEmbedToChannelAsync(ctx.Channel, "Resin Remind Error", "It seems that you do not have DMs from Server Members Enabled. " +
"Please enable to let Paimon talk to you! Once Enabled, please try again. Learn more [here](https://support.discord.com/hc/en-us/articles/217916488-Blocking-Privacy-Settings-)!"
, TimeSpan.FromSeconds(5), ResponseType.Warning);
Log.Warning("User {Id} tried to enable Resin Remind, however does not have DMs Enabled. {ExceptionMessage}", ctx.User.Id, e.Message);
}
else
Log.Error("This is Exception E {Exception} , Message: {E.Message} Stacktrace: {E.StackTrace}", e, e.Message, e.StackTrace);
}
}
else
Log.Warning($"Invalid Operation: ResinRemind was called, User {ctx.User.Id} exists in the database but does not have a ResinTimer started in the List.");
}
else
await PaimonServices.SendRespondAsync(ctx, $"It seems you're not in the database, please create an account using `{SharedData.prefixes[0]}acc create` or to simply use the resin counter," +
$" please set your resin using `{SharedData.prefixes[0]}resin set [amount]`.", TimeSpan.FromSeconds(5)).ConfigureAwait(false);
}
[Command("unremind")]
[Description("Tells PaimonBot to disable your Resin Reminder if active.")]
[Cooldown(1, 3, CooldownBucketType.User)]
[RequireGuild]
public async Task ResinUnremind(CommandContext ctx)
{
var traveler = SharedData.PaimonDB.GetTravelerBy("DiscordID", ctx.User.Id);
if (traveler != null)
{
if (SharedData.resinTimers.Exists(x => x._discordID == ctx.User.Id))
{
var resinTimer = SharedData.resinTimers.Find(x => x._discordID == ctx.User.Id);
if (!resinTimer._remind) // Resin Remind not active.
{
await PaimonServices.SendEmbedToChannelAsync(ctx.Channel, "Resin Remind Error", $"It seems that you do not have Resin Remind currently active. Use `{SharedData.prefixes[0]}resin remind [amount]` to enable a resin remind."
,TimeSpan.FromSeconds(6), ResponseType.Warning);
return;
}
resinTimer.DisableRemind();
Log.Information("Resin Remind for User {Id} Disabled, at {DateTime};", ctx.User.Id, DateTime.Now);
await ctx.RespondAsync($"{Emojis.BlurpEmote} Your resin remind has been disabled. To re-enable, use `{SharedData.prefixes[0]}resin remind [amount]`.").ConfigureAwait(false);
}
else
Log.Warning($"Invalid Operation: ResinRemind was called, User {ctx.User.Id} exists in the database but does not have a ResinTimer started in the List.");
}
else
await PaimonServices.SendRespondAsync(ctx, $"It seems you're not in the database, please create an account using `{SharedData.prefixes[0]}acc create` or to simply use the resin counter," +
$" please set your resin using `{SharedData.prefixes[0]}resin set [amount]`.", TimeSpan.FromSeconds(5)).ConfigureAwait(false);
}
private DiscordEmbedBuilder ResinEmbed(Traveler traveler, DiscordUser user)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"You currently have {Formatter.Bold(traveler.ResinAmount.ToString())}/160 resin {Emojis.ResinEmote}");
sb.AppendLine($"Resin was last updated/added at <t:{((DateTimeOffset)traveler.ResinUpdatedTime.ToUniversalTime()).ToUnixTimeSeconds()}>");
TimeSpan timeToCap = TimeSpan.FromMinutes((160 - traveler.ResinAmount) * 8);
DateTimeOffset capTime = (DateTimeOffset)traveler.ResinUpdatedTime.ToUniversalTime() + timeToCap;
sb.AppendLine($"Your resin will be filled <t:{capTime.ToUnixTimeSeconds()}:R>");
if (SharedData.resinTimers.Exists(x => x._discordID == user.Id))
{
int resinRemind = SharedData.resinTimers.Find(x => x._discordID == traveler.DiscordID)._remindAt;
if (resinRemind != int.MinValue)
{
sb.AppendLine();
sb.AppendLine($"You will be reminded when your resin hits **{resinRemind}** {Emojis.ResinEmote}.");
}
}
DiscordEmbedBuilder embed = new DiscordEmbedBuilder()
.WithTitle($"{user.Username}#{user.Discriminator}'s Resin Information")
.WithColor(SharedData.defaultColour)
.WithThumbnail(user.AvatarUrl)
.WithFooter($"Use {SharedData.prefixes[0]}resin set [amount] to set a new resin amount")
.WithDescription(sb.ToString())
.WithTimestamp(DateTime.Now);
return embed;
}
}
}