geekbot/Geekbot.net/Program.cs

76 lines
2.7 KiB
C#
Raw Normal View History

2017-04-12 21:49:04 +02:00
using System;
using System.Reflection;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
namespace Geekbot.net
{
class Program
{
private CommandService commands;
private DiscordSocketClient client;
private DependencyMap map;
private static void Main(string[] args)
{
Console.WriteLine("Staring...");
new Program().MainAsync().GetAwaiter().GetResult();
}
public async Task MainAsync()
{
client = new DiscordSocketClient();
commands = new CommandService();
string token = "MTgxMDkyOTgxMDUzNDU2Mzg0.C8_UTw.PvXLAVOTccbrWKLMeyvN9WqRPlU";
map = new DependencyMap();
await InstallCommands();
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
await Task.Delay(-1);
}
public async Task InstallCommands()
{
// Hook the MessageReceived Event into our Command Handler
client.MessageReceived += HandleCommand;
client.MessageReceived += HandleMessageReceived;
// Discover all of the commands in this assembly and load them.
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
}
public async Task HandleCommand(SocketMessage messageParam)
{
// Don't process the command if it was a System Message
var message = messageParam as SocketUserMessage;
if (message == null) return;
// Create a number to track where the prefix ends and the command begins
int argPos = 0;
// Determine if the message is a command, based on if it starts with '!' or a mention prefix
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
// Create a Command Context
var context = new CommandContext(client, message);
// Execute the command. (result does not indicate a return value,
// rather an object stating if the command executed succesfully)
var result = await commands.ExecuteAsync(context, argPos, map);
if (!result.IsSuccess)
{
await context.Channel.SendMessageAsync(result.ErrorReason);
}
}
public async Task HandleMessageReceived(SocketMessage messsageParam)
{
var message = messsageParam;
if (message == null) return;
if (message.Author.Username.Contains("Geekbot")) return;
Console.WriteLine(message.Channel + " - " + message.Author + " - " + message.Content);
}
}
}