Sets up an echo command
This commit is contained in:
parent
b3da42522f
commit
7179c70463
|
@ -7,4 +7,8 @@
|
|||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.12.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BiblioTech
|
||||
{
|
||||
public class CommandHandler
|
||||
{
|
||||
private readonly DiscordSocketClient client;
|
||||
private readonly CommandService commands;
|
||||
|
||||
// Retrieve client and CommandService instance via ctor
|
||||
public CommandHandler(DiscordSocketClient client, CommandService commands)
|
||||
{
|
||||
this.commands = commands;
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public async Task InstallCommandsAsync()
|
||||
{
|
||||
// Hook the MessageReceived event into our command handler
|
||||
client.MessageReceived += HandleCommandAsync;
|
||||
|
||||
// Here we discover all of the command modules in the entry
|
||||
// assembly and load them. Starting from Discord.NET 2.0, a
|
||||
// service provider is required to be passed into the
|
||||
// module registration method to inject the
|
||||
// required dependencies.
|
||||
//
|
||||
// If you do not use Dependency Injection, pass null.
|
||||
// See Dependency Injection guide for more information.
|
||||
await commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(),
|
||||
services: null);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// 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 the prefix and make sure no bots trigger commands
|
||||
if (!(message.HasCharPrefix('!', ref argPos) ||
|
||||
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);
|
||||
|
||||
// Execute the command with the command context we just
|
||||
// created, along with the service provider for precondition checks.
|
||||
await commands.ExecuteAsync(
|
||||
context: context,
|
||||
argPos: argPos,
|
||||
services: null);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
using Discord.Commands;
|
||||
|
||||
namespace BiblioTech
|
||||
{
|
||||
public class HelloWorldModule : ModuleBase<SocketCommandContext>
|
||||
{
|
||||
[Command("say")]
|
||||
[Summary("Echoes a message.")]
|
||||
public Task SayAsync([Remainder][Summary("The text to echo")] string echo)
|
||||
{
|
||||
return ReplyAsync("I say: " + echo);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +1,29 @@
|
|||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Starting Codex Discord bot BiblioTech...");
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static Task Main(string[] args) => new Program().MainAsync();
|
||||
|
||||
private DiscordSocketClient client;
|
||||
|
||||
public async Task MainAsync()
|
||||
{
|
||||
client = new DiscordSocketClient();
|
||||
|
||||
client.Log += Log;
|
||||
|
||||
// You can assign your bot token to a string, and pass that in to connect.
|
||||
// This is, however, insecure, particularly if you plan to have your code hosted in a public repository.
|
||||
var token = "token";
|
||||
|
||||
await client.LoginAsync(TokenType.Bot, token);
|
||||
await client.StartAsync();
|
||||
await Task.Delay(-1);
|
||||
}
|
||||
private Task Log(LogMessage msg)
|
||||
{
|
||||
Console.WriteLine(msg.ToString());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue