-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathCommands.cs
More file actions
142 lines (124 loc) · 4.92 KB
/
MathCommands.cs
File metadata and controls
142 lines (124 loc) · 4.92 KB
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
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using System;
using System.Collections.Generic;
using System.Text;
namespace Wadebot
{
public class MathCommands : BaseCommandModule
{
/*
* Command : Add
* Function : Wadebot will add two integers provided by the user and return the sum
* Cooldown : 1 use every 5 seconds per user
*/
[Command("Add")]
[Cooldown(1, 5, CooldownBucketType.User)]
public async Task AddCommand(CommandContext ctx, int a, int b)
{
int sum = a + b;
await ctx.Channel.SendMessageAsync($"The sum of {a} and {b} is {sum}.");
string timestamp = DateTime.Now.ToString("HH:mm:ss");
var user = ctx.User;
LogCommand(ctx, "Add", $"{user} asked what {a} + {b} is and the sum is: {sum}");
await HandleXpAsync(ctx);
}
/*
* Command : Subtract
* Function : Wadebot will subtract two integers provided by the user and return the difference
* Cooldown : 1 use every 5 seconds per user
*/
[Command("Subtract")]
[Cooldown(1, 5, CooldownBucketType.User)]
public async Task SubtractCommand(CommandContext ctx, int a, int b)
{
int difference = a - b;
await ctx.Channel.SendMessageAsync($"The difference between {a} and {b} is {difference}.");
string timestamp = DateTime.Now.ToString("HH:mm:ss");
var user = ctx.User;
LogCommand(ctx, "Subtract", $"{user} asked what {a} - {b} is and the difference is: {difference}");
await HandleXpAsync(ctx);
}
/*
* Command : Multiply
* Function : Wadebot will multiply two integers provided by the user and return the product
* Cooldown : 1 use every 5 seconds per user
*/
[Command("Multiply")]
[Cooldown(1, 5, CooldownBucketType.User)]
public async Task MultiplyCommand(CommandContext ctx, int a, int b)
{
int product = a * b;
await ctx.Channel.SendMessageAsync($"The product of {a} and {b} is {product}.");
string timestamp = DateTime.Now.ToString("HH:mm:ss");
var user = ctx.User;
LogCommand(ctx, "Multiply", $"{user} asked what {a} * {b} is and the product is: {product}");
await HandleXpAsync(ctx);
}
/*
* Command : Divide
* Function : Wadebot will divide two integers provided by the user and return the quotient
* Cooldown : 1 use every 5 seconds per user
*/
[Command("Divide")]
[Cooldown(1, 5, CooldownBucketType.User)]
public async Task DivideCommand(CommandContext ctx, int a, int b)
{
if (b == 0 || a == 0)
{
await ctx.Channel.SendMessageAsync("Error: Division by zero is not allowed.");
return;
}
double quotient = (double)a / b;
await ctx.Channel.SendMessageAsync($"The quotient of {a} divided by {b} is {quotient}.");
string timestamp = DateTime.Now.ToString("HH:mm:ss");
var user = ctx.User;
LogCommand(ctx, "Divide", $"{user} asked what {a} / {b} is and the difference is: {quotient}");
await HandleXpAsync(ctx);
}
// Global Random Instance
private static readonly Random rand = new Random();
//XP Handling Method
private async Task HandleXpAsync(CommandContext ctx, int xp = 10)
{
bool leveledUp = Database.AddXp(
ctx.User.Id,
ctx.Guild.Id,
xp,
out int newLevel
);
if (leveledUp)
{
await ctx.Channel.SendMessageAsync(
$"🎉 {ctx.User.Mention} reached **Level {newLevel}**!"
);
}
}
//Logging Method
private void LogCommand(
CommandContext ctx,
string command,
string output
)
{
using var connection = Database.GetConnection();
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText =
@"
INSERT INTO Logs
(UserId, UserName, GuildId, GuildName, Command, Date, Output)
VALUES
($uid, $user, $gid, $gname, $cmd, $date, $out);
";
cmd.Parameters.AddWithValue("$uid", ctx.User.Id.ToString());
cmd.Parameters.AddWithValue("$user", ctx.User.Username);
cmd.Parameters.AddWithValue("$gid", ctx.Guild.Id.ToString());
cmd.Parameters.AddWithValue("$gname", ctx.Guild.Name);
cmd.Parameters.AddWithValue("$cmd", command);
cmd.Parameters.AddWithValue("$date", DateTime.Now.ToString("HH:mm:ss"));
cmd.Parameters.AddWithValue("$out", output);
cmd.ExecuteNonQuery();
}
}
}