summaryrefslogtreecommitdiff
path: root/DiscordBot
diff options
context:
space:
mode:
authoruzvkl <dennis.kobert@student.kit.edu>2019-05-20 03:00:44 +0200
committeruzvkl <dennis.kobert@student.kit.edu>2019-05-20 03:00:44 +0200
commitd51f315a8da246c3852017b4a1c127a29a43811f (patch)
treef28fdc31578f4bbd9e3f1fb9a8b2ef9591634670 /DiscordBot
parented26623e17e8dfcc036f88cca6de10d5a35697ec (diff)
Begin Testing
Diffstat (limited to 'DiscordBot')
-rw-r--r--DiscordBot/CommandHandler.cs100
1 files changed, 50 insertions, 50 deletions
diff --git a/DiscordBot/CommandHandler.cs b/DiscordBot/CommandHandler.cs
index 0f6aa7e..b41756f 100644
--- a/DiscordBot/CommandHandler.cs
+++ b/DiscordBot/CommandHandler.cs
@@ -1,32 +1,30 @@
-
+#region
+
using System;
using System.Collections.Generic;
-using System.IO;
using System.Linq;
-using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
-using Discord;
using Discord.Commands;
using Discord.WebSocket;
+#endregion
+
namespace DiscordBot
{
public class CommandHandler
{
+ private static readonly HttpClient _HttpClient = new HttpClient();
private readonly DiscordSocketClient _client;
private readonly CommandService _commands;
- private static readonly HttpClient _HttpClient = new HttpClient();
- public CommandHandler(DiscordSocketClient client, CommandService commands)
- {
+ public CommandHandler(DiscordSocketClient client, CommandService commands) {
_commands = commands;
_client = client;
}
- public async Task InstallCommandsAsync()
- {
+ public Task InstallCommandsAsync() {
// Hook the MessageReceived event into our command handler
_client.MessageReceived += HandleCommandAsync;
@@ -38,24 +36,49 @@ namespace DiscordBot
//
// If you do not use Dependency Injection, pass null.
// See Dependency Injection guide for more information.
- await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(),
- services: null);
+ return _commands.AddModulesAsync(Assembly.GetEntryAssembly(),
+ null);
+ }
+
+
+ private static async Task<string> SendCommand(string name, string command, string url) {
+ command = command.Remove(0, 1);
+ var args = command.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
+
+ string cmdContent = string.Empty;
+ if (args.Length > 1) {
+ cmdContent = "\"" + args.Skip(1).Aggregate((s, n) => s + "\", \"" + n) + "\"";
+ }
+
+ var values = new Dictionary<string, string> {
+ {"Name", name},
+ {"CmdIdentifier", args.First()},
+ {"CmdTexts", "[" + cmdContent + "]"}
+ };
+
+ var content = new FormUrlEncodedContent(values);
+
+ var response = await _HttpClient.PostAsync(url, content);
+
+ return await response.Content.ReadAsStringAsync();
}
- private async Task HandleCommandAsync(SocketMessage messageParam)
- {
+ private async Task HandleCommandAsync(SocketMessage messageParam) {
// Don't process the command if it was a system message
var message = messageParam as SocketUserMessage;
- if (message == null) return;
+ if (message == null) {
+ return;
+ }
// Create a number to track where the prefix ends and the command begins
- int argPos = 0;
+ var argPos = 0;
// Determine if the message is a command based on the prefix and make sure no bots trigger commands
if (!(message.HasCharPrefix('!', ref argPos) ||
- message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
- message.Author.IsBot)
+ message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
+ message.Author.IsBot) {
return;
+ }
// Create a WebSocket-based command context based on the message
var context = new SocketCommandContext(_client, message);
@@ -66,47 +89,24 @@ namespace DiscordBot
// Keep in mind that result does not indicate a return value
// rather an object stating if the command executed successfully.
var result = await _commands.ExecuteAsync(
- context: context,
- argPos: argPos,
- services: null);
+ context,
+ argPos,
+ null);
// Optionally, we may inform the user if the command fails
// to be executed; however, this may not always be desired,
// as it may clog up the request queue should a user spam a
// command.
- if (result.Error == CommandError.UnknownCommand)
- {
- var response = await SendCommand(message.Author.Username, message.Content,"https://kobert.dev/api/dsa/commands");
+ if (result.Error == CommandError.UnknownCommand) {
+ string response = await SendCommand(message.Author.Username, message.Content,
+ "https://kobert.dev/api/dsa/commands");
//var response = "invalid";
await context.Channel.SendMessageAsync(response);
}
- else if (!result.IsSuccess) await context.Channel.SendMessageAsync(result.ErrorReason);
- }
-
-
-
- private static async Task<string> SendCommand(string name, string command, string url)
- {
- command = command.Remove(0, 1);
- var args = command.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
-
- var cmdContent = string.Empty;
- if (args.Length > 1) cmdContent = "\"" + args.Skip(1).Aggregate((s, n) => s + "\", \"" + n) + "\"";
-
- var values = new Dictionary<string, string>
- {
- { "Name", name },
- { "CmdIdentifier", args.First()},
- { "CmdTexts", "[" + cmdContent + "]"}
- };
-
- var content = new FormUrlEncodedContent(values);
-
- var response = await _HttpClient.PostAsync(url, content);
-
- return await response.Content.ReadAsStringAsync();
+ else if (!result.IsSuccess) {
+ await context.Channel.SendMessageAsync(result.ErrorReason);
+ }
}
-
}
-}
+} \ No newline at end of file