setting up slash commands
This commit is contained in:
parent
8c7229504e
commit
f33866efc1
|
@ -10,6 +10,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.12.0" />
|
||||
<ProjectReference Include="..\..\Framework\ArgsUniform\ArgsUniform.csproj" />
|
||||
<ProjectReference Include="..\..\ProjectPlugins\CodexPlugin\CodexPlugin.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -43,10 +43,9 @@ namespace BiblioTech
|
|||
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;
|
||||
if (message.Author.IsBot) return;
|
||||
if (!message.HasMentionPrefix(client.CurrentUser, ref argPos) &&
|
||||
!message.Content.StartsWith("!")) return;
|
||||
|
||||
// Create a WebSocket-based command context based on the message
|
||||
var context = new SocketCommandContext(client, message);
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
using CodexPlugin;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BiblioTech
|
||||
{
|
||||
public class DeploymentFilesMonitor
|
||||
{
|
||||
private DateTime lastUpdate = DateTime.MinValue;
|
||||
private CodexDeployment[] deployments = Array.Empty<CodexDeployment>();
|
||||
|
||||
public CodexDeployment[] GetDeployments()
|
||||
{
|
||||
if (ShouldUpdate())
|
||||
{
|
||||
UpdateDeployments();
|
||||
}
|
||||
|
||||
return deployments;
|
||||
}
|
||||
|
||||
private void UpdateDeployments()
|
||||
{
|
||||
lastUpdate = DateTime.UtcNow;
|
||||
var path = Program.Config.DeploymentsPath;
|
||||
var files = Directory.GetFiles(path);
|
||||
deployments = files.Select(ProcessFile).Where(d => d != null).Cast<CodexDeployment>().ToArray();
|
||||
}
|
||||
|
||||
private CodexDeployment? ProcessFile(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
var lines = File.ReadAllLines(filename);
|
||||
return JsonConvert.DeserializeObject<CodexDeployment>(string.Join(" ", lines));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private bool ShouldUpdate()
|
||||
{
|
||||
return !deployments.Any() || (DateTime.UtcNow - lastUpdate) > TimeSpan.FromMinutes(10);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
using Discord;
|
||||
using Discord.Net;
|
||||
using Discord.WebSocket;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BiblioTech
|
||||
{
|
||||
public class HelloWorldCommand
|
||||
{
|
||||
private readonly DiscordSocketClient client;
|
||||
|
||||
public HelloWorldCommand(DiscordSocketClient client)
|
||||
{
|
||||
this.client = client;
|
||||
|
||||
client.Ready += Client_Ready;
|
||||
}
|
||||
|
||||
private async Task Client_Ready()
|
||||
{
|
||||
// Let's build a guild command! We're going to need a guild so lets just put that in a variable.
|
||||
var guild = client.Guilds.Single(g => g.Name == "ThatBen's server");
|
||||
|
||||
// Next, lets create our slash command builder. This is like the embed builder but for slash commands.
|
||||
var guildCommand = new SlashCommandBuilder();
|
||||
|
||||
// Note: Names have to be all lowercase and match the regular expression ^[\w-]{3,32}$
|
||||
guildCommand.WithName("do-thing");
|
||||
|
||||
// Descriptions can have a max length of 100.
|
||||
guildCommand.WithDescription("This command does the thing!");
|
||||
|
||||
//// Let's do our global command
|
||||
//var globalCommand = new SlashCommandBuilder();
|
||||
//globalCommand.WithName("first-global-command");
|
||||
//globalCommand.WithDescription("This is my first global slash command");
|
||||
|
||||
try
|
||||
{
|
||||
// Now that we have our builder, we can call the CreateApplicationCommandAsync method to make our slash command.
|
||||
await guild.CreateApplicationCommandAsync(guildCommand.Build());
|
||||
|
||||
// With global commands we don't need the guild.
|
||||
//await client.CreateGlobalApplicationCommandAsync(globalCommand.Build());
|
||||
// Using the ready event is a simple implementation for the sake of the example. Suitable for testing and development.
|
||||
// For a production bot, it is recommended to only run the CreateGlobalApplicationCommandAsync() once for each command.
|
||||
}
|
||||
catch (ApplicationCommandException exception)
|
||||
{
|
||||
// If our command was invalid, we should catch an ApplicationCommandException. This exception contains the path of the error as well as the error message. You can serialize the Error field in the exception to get a visual of where your error is.
|
||||
var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented);
|
||||
|
||||
// You can send this error somewhere or just print it to the console, for this example we're just going to print it.
|
||||
Console.WriteLine(json);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
using ArgsUniform;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.Net;
|
||||
using Discord.WebSocket;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BiblioTech
|
||||
{
|
||||
|
@ -9,6 +12,7 @@ namespace BiblioTech
|
|||
private DiscordSocketClient client = null!;
|
||||
|
||||
public static Configuration Config { get; private set; } = null!;
|
||||
public static DeploymentFilesMonitor DeploymentFilesMonitor { get; } = new DeploymentFilesMonitor();
|
||||
|
||||
public static Task Main(string[] args)
|
||||
{
|
||||
|
@ -22,14 +26,16 @@ namespace BiblioTech
|
|||
{
|
||||
Console.WriteLine("Starting Codex Discord Bot...");
|
||||
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";
|
||||
var helloWorld = new HelloWorldCommand(client);
|
||||
|
||||
await client.LoginAsync(TokenType.Bot, token);
|
||||
//var cmdService = new CommandService();
|
||||
//var handler = new CommandHandler(client, cmdService);
|
||||
//await handler.InstallCommandsAsync();
|
||||
//Console.WriteLine("Command handler installed...");
|
||||
|
||||
await client.LoginAsync(TokenType.Bot, Config.ApplicationToken);
|
||||
await client.StartAsync();
|
||||
Console.WriteLine("Running...");
|
||||
await Task.Delay(-1);
|
||||
|
|
|
@ -2,5 +2,6 @@ FROM mcr.microsoft.com/dotnet/sdk:7.0
|
|||
|
||||
WORKDIR app
|
||||
COPY ./Tools/BiblioTech ./Tools/BiblioTech
|
||||
COPY ./Framework/ArgsUniform ./Framework/ArgsUniform
|
||||
COPY ./Framework ./Framework
|
||||
COPY ./ProjectPlugins ./ProjectPlugins
|
||||
CMD ["dotnet", "run", "--project", "Tools/BiblioTech"]
|
||||
|
|
Loading…
Reference in New Issue