Adding dBot and AJ
This commit is contained in:
parent
ad7a75b38e
commit
d23f00c94c
@ -9,7 +9,10 @@
|
|||||||
<e p="bin" t="ExcludeRecursive" />
|
<e p="bin" t="ExcludeRecursive" />
|
||||||
<e p="ChaosBot.csproj" t="IncludeRecursive" />
|
<e p="ChaosBot.csproj" t="IncludeRecursive" />
|
||||||
<e p="Discord" t="Include">
|
<e p="Discord" t="Include">
|
||||||
<e p="DBot.cs" t="Include" />
|
<e p="DiscordConnect.cs" t="Include" />
|
||||||
|
<e p="Services" t="Include">
|
||||||
|
<e p="CommandHandler.cs" t="Include" />
|
||||||
|
</e>
|
||||||
</e>
|
</e>
|
||||||
<e p="Logging.cs" t="Include" />
|
<e p="Logging.cs" t="Include" />
|
||||||
<e p="obj" t="ExcludeRecursive">
|
<e p="obj" t="ExcludeRecursive">
|
||||||
|
|||||||
@ -1,16 +1,18 @@
|
|||||||
using NLog;
|
using NLog;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Discord.Commands;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
|
||||||
namespace ChaosBot.Discord
|
namespace ChaosBot.Discord
|
||||||
{
|
{
|
||||||
public class DBot
|
public class DiscordConnect
|
||||||
{
|
{
|
||||||
public static DiscordSocketClient _client;
|
private static DiscordSocketClient _client;
|
||||||
private static Logger _logger = Program._logger;
|
private static Logger _logger = Program._logger;
|
||||||
|
|
||||||
public static async Task Connect()
|
public static async Task Connect()
|
||||||
@ -38,7 +40,6 @@ namespace ChaosBot.Discord
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static Task Log(LogMessage msg)
|
private static Task Log(LogMessage msg)
|
||||||
{
|
{
|
||||||
switch (msg.Severity)
|
switch (msg.Severity)
|
||||||
96
ChaosBot/Discord/Services/CommandHandler.cs
Normal file
96
ChaosBot/Discord/Services/CommandHandler.cs
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Discord;
|
||||||
|
using Discord.Commands;
|
||||||
|
using Discord.WebSocket;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
namespace ChaosBot.Discord.Services
|
||||||
|
{
|
||||||
|
public class CommandHandler
|
||||||
|
{
|
||||||
|
// setup fields to be set later in the constructor
|
||||||
|
private readonly IConfiguration _config;
|
||||||
|
private readonly CommandService _commands;
|
||||||
|
private readonly DiscordSocketClient _client;
|
||||||
|
private readonly IServiceProvider _services;
|
||||||
|
|
||||||
|
public CommandHandler(IServiceProvider services)
|
||||||
|
{
|
||||||
|
// juice up the fields with these services
|
||||||
|
// since we passed the services in, we can use GetRequiredService to pass them into the fields set earlier
|
||||||
|
_config = services.GetRequiredService<IConfiguration>();
|
||||||
|
_commands = services.GetRequiredService<CommandService>();
|
||||||
|
_client = services.GetRequiredService<DiscordSocketClient>();
|
||||||
|
_services = services;
|
||||||
|
|
||||||
|
// take action when we execute a command
|
||||||
|
_commands.CommandExecuted += CommandExecutedAsync;
|
||||||
|
|
||||||
|
// take action when we receive a message (so we can process it, and see if it is a valid command)
|
||||||
|
_client.MessageReceived += MessageReceivedAsync;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InitializeAsync()
|
||||||
|
{
|
||||||
|
// register modules that are public and inherit ModuleBase<T>.
|
||||||
|
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
|
||||||
|
}
|
||||||
|
|
||||||
|
// this class is where the magic starts, and takes actions upon receiving messages
|
||||||
|
public async Task MessageReceivedAsync(SocketMessage rawMessage)
|
||||||
|
{
|
||||||
|
// ensures we don't process system/other bot messages
|
||||||
|
if (!(rawMessage is SocketUserMessage message))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.Source != MessageSource.User)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// sets the argument position away from the prefix we set
|
||||||
|
var argPos = 0;
|
||||||
|
|
||||||
|
// get prefix from the configuration file
|
||||||
|
char prefix = Char.Parse(_config["Prefix"]);
|
||||||
|
|
||||||
|
// determine if the message has a valid prefix, and adjust argPos based on prefix
|
||||||
|
if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasCharPrefix(prefix, ref argPos)))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var context = new SocketCommandContext(_client, message);
|
||||||
|
|
||||||
|
// execute command if one is found that matches
|
||||||
|
await _commands.ExecuteAsync(context, argPos, _services);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
|
||||||
|
{
|
||||||
|
// if a command isn't found, log that info to console and exit this method
|
||||||
|
if (!command.IsSpecified)
|
||||||
|
{
|
||||||
|
System.Console.WriteLine($"Command failed to execute for [{context.User.Username}] <-> [{result.ErrorReason}]!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// log success to the console and exit this method
|
||||||
|
if (result.IsSuccess)
|
||||||
|
{
|
||||||
|
System.Console.WriteLine($"Command [{command.Value.Name}] executed for -> [{context.User.Username}]");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// failure scenario, let's let the user know
|
||||||
|
await context.Channel.SendMessageAsync($"Sorry, {context.User.Username}... something went wrong -> [{result}]!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -15,7 +15,9 @@ namespace ChaosBot
|
|||||||
public static Logger _logger;
|
public static Logger _logger;
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
=> new Program().MainFunction().GetAwaiter().GetResult();
|
{
|
||||||
|
new Program().MainFunction().GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
private async Task MainFunction()
|
private async Task MainFunction()
|
||||||
{
|
{
|
||||||
@ -38,8 +40,8 @@ namespace ChaosBot
|
|||||||
*/
|
*/
|
||||||
_logger.Info($"Starting Up {Cfg.GetValue<string>("Bot:Name")} v{Cfg.GetValue<string>("Bot:Version")}");
|
_logger.Info($"Starting Up {Cfg.GetValue<string>("Bot:Name")} v{Cfg.GetValue<string>("Bot:Version")}");
|
||||||
|
|
||||||
var DiscordBot = DBot.Connect();
|
|
||||||
|
|
||||||
|
var DiscordBot = DiscordConnect.Connect();
|
||||||
await DiscordBot;
|
await DiscordBot;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user