Implement LEADTOOLS with Discord - C# .NET 6

This tutorial shows how to implement a Discord bot in Discord.NET using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use the LEADTOOLS SDK in a Discord.NET bot application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (4 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET 6+ (Console)

  • Python: Python

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial and the basics of creating a discord.net bot, before working on this tutorial.

Disclaimer

Discord.NET is an open-source .NET API for Discord applications.

Refer to the Discord.NET GitHub license for their terms of use. Discord.NET license is separate from LEADTOOLS licensing. To obtain a LEADTOOLS SDK license, refer to Setting a Runtime License.

Create the Project and Add the LEADTOOLS References

Start by following the Discord.NET tutorial.

The references needed depend upon the purpose of the project.

This tutorial requires the following NuGet Packages:

Ensure that the Discord.NET NuGet packages are installed, for further details refer to the Discord.Net Installation

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add LEADTOOLS References

With the project created, the references added, and the license set, coding can begin. In the Solution Explorer, open Program.cs and place the following at the top of Program.cs.

C#
using Discord; 
using Discord.Net; 
using Discord.WebSocket; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Barcode; 
using Newtonsoft.Json; 

Setting the LEADTOOLS license

Add a new method called InitLEAD() and call it in the MainAsync() method.

C#
static void InitLEAD() 
{ 
    var leadSdkBase = @"C:\LEADTOOLS23\"; 
    var licenseDir = Path.Combine(leadSdkBase, @"Support\Common\License\"); 
 
    // Set the LEADTOOLS License  
    RasterSupport.SetLicense(licenseDir + "LEADTOOLS.LIC", File.ReadAllText(licenseDir + "LEADTOOLS.LIC.KEY")); 
 
    if (RasterSupport.KernelExpired) 
        Console.WriteLine("License file invalid or expired."); 
    else 
        Console.WriteLine("License file set successfully."); 
} 

Read Barcodes From Uploaded Files

Add a new slash command that takes an image as an argument and outputs the content of any barcodes detected within the image.

First add a new SlashCommandBuilder to the ClientReady() method.

C#
public async Task ClientReady() 
   { 
      ulong guildId = REPLACE WITH YOUR GUILD ID; 
 
      var barcodeCommand = new SlashCommandBuilder() 
        .WithName("barcode-read") 
        .WithDescription("Reads any barcodes in an attached image") 
        .AddOption("file", ApplicationCommandOptionType.Attachment, "The file which contains a barcode", isRequired: true); 
 
      try 
      { 
         await client.Rest.CreateGuildCommand(barcodeCommand.Build(), guildId); 
      } 
      catch (ApplicationCommandException exception) 
      { 
         var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented); 
         Console.WriteLine(json); 
      } 
   } 

Add a new method to Program.cs called SlashCommandHandler.

C#
private async Task SlashCommandHandler(SocketSlashCommand command) 
{ 
   // Let's add a switch statement for the command name so we can handle multiple commands in one event. 
   switch (command.Data.Name) 
   { 
      case "barcode-read": 
          await BarcodeReadCommand(command); 
          break; 
   } 
} 

Next wire the new SlashCommandHandler method to the client by adding the following line in the MainAsync() method.

C#
client.SlashCommandExecuted += SlashCommandHandler; 

Add a new method to Program.cs called BarcodeReadCommand.

C#
 private async Task BarcodeReadCommand(SocketSlashCommand command) 
   { 
      await command.RespondAsync("Reading Barcode(s)"); 
      RasterCodecs codecs = new RasterCodecs(); 
      Attachment attachment = (Attachment)command.Data.Options.First().Value; 
      Uri url = new Uri(attachment.Url); 
      RasterImage image = codecs.Load(url).Result; 
 
      BarcodeEngine barcodeEngineInstance = new BarcodeEngine(); 
      BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes(image, LeadRect.Empty, 0, null); 
 
      StringBuilder sb = new StringBuilder(); 
      sb.AppendFormat("{0} barcode(s) found", dataArray.Length); 
      sb.AppendLine(); 
 
      for (int i = 0; i < dataArray.Length; i++) { 
         BarcodeData data = dataArray[i]; 
         sb.AppendFormat("Symbology: {0}, Location: {1}, Data: {2}", data.Symbology, data.Bounds, data.Value); 
         sb.AppendLine(); 
      } 
      await command.FollowupAsync(sb.ToString()); 
   } 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and adds a new read-barcode slash command to a discord bot.

output from the barcode-read command

Wrap-up

This tutorial showcases how to implement the LEADTOOLS barcode SDK with a Discord.NET application. The downloadable project for this tutorial includes commands to OCR text, apply image processing commands, read barcodes, and convert files.

See Also

Help Version 23.0.2024.9.18
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.