Friday, 4 December 2020

Technologies for Websites

 Making websites from scratch isn't easy, you need to use specific technologies and protect yourself from common Internet Crimes, such as hacking. If you are making a website on a service such as WordPress the website would be quite secure and you don't have to worry about the security that much.

In 2020 the most common frameworks used for web development are Angular and React. These are also the most requested technologies by employers. 

Angular is a framework that is focused around TypeScript while React is based on JavaScript. You can also make a website by using plain HTML, JavaScript and CSS but it would be more difficult. WordPress is based around php.

Python is also a good language for website. Google uses Python in their websites. The Python programming language is quite easy to understand and it is very reliable.

Before diving in into React or Angular or any other website framework it is recommended that you at least have a basic understanding of HTML, JavaScript, CSS and TypeScript as these are the building blocks for a website. 

Making a website for free is possible but hosting a website requires money. Domains like .com or .net are expensive. If you are making a small project you can use domains like .gg .xyz .lol.


Friday, 20 November 2020

Angular Web Framework

 Angular web framework is a framework that is used to develop websites and web-apps. In this tutorial will show you how to get started with Angular. You need to have installed a code IDE and Node.JS.

Angular is a framework made by Google and can be installed using npm install angular

For the console command to work properly you need to first have made a npm project. You can do that by navigating to your project folder and in your VSCode console type npm init. Fill in the details and you should see a package.json file in your project. Having that done, type npm install angular and the installation process should begin. Once the installation is over you should see more files created in your project.

The files that you are going to use to develop your website are located in the app folder. There is app.component.ts, app.component.css and app.component.html. TS is short for TypeScript. In the app.component.html you can write your html. In the html file you will also see code that is not html. That is for the script processes of the website. Angular works in components, so all components are a part of the page. There could be a sidebar component, a navbar component and the main component. These all make the single page web-app

An easy way to get started on angular and web development is to use Stackblitz.com. This is an online code editor that can help you in getting started with angular and many other web frameworks

Friday, 9 October 2020

RestSharp

You need to connect to some APIs but you don't know how or you are having some trouble to do that. Well, why not download RestSharp?

RestSharp is a REST API client for your .NET apps. Extremely useful if you are developing websites or apps that retrieve information from APIs. 

The REST mechanism is a easier and more secure way of fetching information from APIs
REST stands for Representational State Transfer. A web server is the place where the API is hosted. A HTTP request could be a GET, POST, PUT, DELETE... these are used to tell the API what action you want to perform. The client is your application. The Web server will respond in JSON or XML or both

The two packages that you need to install are Newtonsoft.JSON and RestSharp. Newtonsoft.JSON is used to parse the response from the API. Most APIs use a JSON response. Before doing anything you need to know how the API is going to reply and how the JSON response is going to look like. This is crucial as it is going to help you in creating a model class for your app. A model class is a class that represents the response and everything that the JSON file contains. 

You can create a model class from scratch by looking at the JSON file and copying every field title or you can use json2csharp. This website is going to ask for the JSON file and convert it to a C# model class.

On your main file create an async Task:

public async Task ApiMethod(string search){
            var client = new RestClient($"https://apiurl.com/{search}");
            var request = new RestRequest(Method.GET);
            request.AddHeader("a header", "header content");
            request.AddHeader("another header", "header content");
            IRestResponse response = client.Execute(request);


This is going to initialize the client and make the request to the API. The AddHeader is used to pass any headers that the API wants in order to allow access, this could be an API key for example. Having this done now you need to convert your JSON to C# model class. The code will change according to the API you use and the request. An example could be:

public class Response 
{
    public long id {get; set;}
       public string text {get; set;}
}

Now you need to deserialize by doing:

Response result = JsonConvert.DeserializeObject<Response>(response.Content)


And there you go! 
You can use the model by doing: result.[whatever value you have in the class]





Friday, 2 October 2020

Discord Bots!

So you are a developer that wants to build a Discord bot and you don't know where to start, right? Well, you are in the right place, follow this guide and learn the basics of the Discord API.

Discord does not provide wrappers for it's API so instead it advertises some 3rd Party ones. Developers have created lots of wrappers but the one we are using is Discord.NET. Discord.NET is a 3rd party Discord API wrapper for C#. It is not the only one as there is also DSharp+. The most common library is discord.js. It is widely used for developing bots because it is the easiest to use. Discord.NET doesn't have much documentation but it has a very efficient logger service that will say exactly what happens when a command is executed.

Onto the code now!

The first thing that you need to do is create a C# console project. Name the project with the name of your bot. Once you got that ready, head over to the Discord Developer Portal and create an application. Name the application with the same name as your console project. Once the application is created head over to the "Bots" tab and click create bot. Note: The action is irreversible, once you create a bot you cannot delete it

Once the bot is created don't close the page but head over to your code. You should have 2 classes already created: Startup.cs and Program.cs. Head over to the NuGet package manager and install these packages:


Once you got them installed proceed to to Program.cs and type the following:
using System;
using System.Threading.Tasks;
using Discord.Net;

    namespace YourProjectName
    { 
        class Program
        { 

        public static async Task Main(string[] args) 
        => await Startup.RunAsync(args); 

This class will return an error saying that it cannot find the RunAsync method. We will create this method in the Startup.cs class. Now switch to the Startup.cs class and type the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Brobot.Services;

namespace Brobot
    public class Startup
    
    public IConfigurationRoot configuration { get; } 
    
    public Startup(string[] args)
    
        var builder = new ConfigurationBuilder()
                              .SetBasePath(AppContext.BaseDirectory)
                              .AddYamlFile("_config.yml");
       
        configuration = builder.Build();
    } 

   public static async Task RunAsync(string[] args)
  
       var startup = new Startup(args);
       await startup.RunAsync();

   } 

   public async Task RunAsync()
   {
      var services = new ServiceCollection();
      ConfigureServices(services);
      
     var provider = services.BuildServiceProvider();
     provider.GetRequiredService(); 
     var logging = provider.GetRequiredService();
     await provider.GetRequiredService().StartAsync();
     await Task.Delay(-1); 

  } 

 private void ConfigureServices(ServiceCollection services)
 { 

     services.AddSingleton(new DiscordSocketClient(new DiscordSocketConfig 
         { 
             LogLevel = Discord.LogSeverity.Verbose, 
             MessageCacheSize = 1000 
         })) 
             .AddSingleton(new CommandService(new CommandServiceConfig 
         {
             LogLevel = Discord.LogSeverity.Verbose, 
             DefaultRunMode = RunMode.Async, 
             CaseSensitiveCommands = false 
         })) 
             .AddSingleton<CommandHandler>() 
             .AddSingleton<LoggerService() 
             .AddSingleton<StartupService>() 
             .AddSingleton(configuration); 
  } 
 }
}

This is basically the class that initialises the basics of the bot and how it is supposed to work. This class will also start up the Logger Service, Command Handler and the Startup service. 
Now create a new folder and call it Services. Add a class StartupService.cs and type the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;

namespace Brobot.Services
{
    public class StartupService
    {
        public static IServiceProvider _provider;
        private readonly DiscordSocketClient _discord;
        private readonly CommandService _commands;
        private readonly IConfigurationRoot _config;
        public StartupService(IServiceProvider provider, DiscordSocketClient discord, CommandService commands, IConfigurationRoot config)
        {
            _provider = provider;
            _discord = discord;
            _config = config;
            _commands = commands;
        }

        public async Task StartAsync()
        {
            string token = _config["token:discord"];
            
            if (string.IsNullOrEmpty(token))
            {
                Console.WriteLine("Provide the Discord token in _config.yml. If the file is not present, make it!");
                return;
            }

            await _discord.LoginAsync(TokenType.Bot, token);
            await _discord.StartAsync();
            await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _provider);
        }

    }
}

This class will require you to set up a config.yml file. In this file you will paste the bot's token and prefix

prefix: 'bot's prefix!'
token:
    discord: 'the token goes here'


now you're all set. the bot will start but not do anything.
In the service folder add a new class and call it CommandHandler.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;

namespace Brobot.Services
{
    public class CommandHandler
    {
        public static IServiceProvider _provider;
        public static DiscordSocketClient _discord;
        public static CommandService _commands;
        public static IConfigurationRoot _config;

        public CommandHandler(DiscordSocketClient discord, CommandService commands, IConfigurationRoot config, IServiceProvider provider)
        {
            _provider = provider;
            _discord = discord;
            _config = config;
            _commands = commands;

            _discord.Ready += OnReady;
            _discord.MessageReceived += OnMessageReceived;

        }

        private async Task OnMessageReceived(SocketMessage arg)
        {
            var msg = arg as SocketUserMessage;
            if (msg.Author.IsBot) return;

            var context = new SocketCommandContext(_discord, msg);

            int pos = 0;
            //msg.HasStringPrefix(_config["prefix"], ref pos) ||  just for the prefix
            if (msg.HasStringPrefix(_config["prefix"], ref pos) || msg.HasMentionPrefix(_discord.CurrentUser, ref pos))
            {
                var result = await _commands.ExecuteAsync(context, pos, _provider);

                if (!result.IsSuccess)
                {
                    var reason = result.Error;
                    Console.WriteLine("There has been an error!");
                    Console.WriteLine(reason);
                    Console.WriteLine(result);

                    await context.Channel.SendMessageAsync($"There has been an error in executing your command. Here's the error: \n ```{result}``` \n Please contact if you encounter further problems." +
                        $"\n Run  for error definitions");//error message

                    
                }
            }
        }

        public Task OnReady()
        {
            _discord.SetGameAsync("Version 1.1.0");
            Console.WriteLine("Brobot is up and running!");//login message
            Console.WriteLine($"Connected as: {_discord.CurrentUser.Username}#{_discord.CurrentUser.Discriminator}");
            return Task.CompletedTask;
        }
    }
}

This class will handle all the commands and post errors in the console and in the discord channels. You can change the error message and the Login message and the Activity to whatever you want!

Now in the same folder make a class and call it LoggerService.cs
Type the following code
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;

public class LoggingService
{
public LoggingService(DiscordSocketClient client, CommandService command)
{
client.Log += LogAsync;
command.Log += LogAsync;
}
private Task LogAsync(LogMessage message)
{
if (message.Exception is CommandException cmdException)
{
Console.WriteLine($"[Command/{message.Severity}] {cmdException.Command.Aliases.First()}"
+ $" failed to execute in {cmdException.Context.Channel}.");
Console.WriteLine(cmdException);
}
else
Console.WriteLine($"[General/{message.Severity}] {message}");

return Task.CompletedTask;
}
}

This will create a logger that will post in the console when the bot connects, ping time, where the bot connects and any errors.

The bot is ready to run but it is missing the commands. Create a Commands folder in the main directory of the project and add a class. Name it whatever you want
The process is equal for most commands:

1. Make the main class public and inherit ModuleBase (just add " : ModuleBase" at the end of the class declaration
2. Create an async Task
3. add the command content and action!



Congratulations! you have built your first Discord bot in C#

Technologies for Websites

 Making websites from scratch isn't easy, you need to use specific technologies and protect yourself from common Internet Crimes, such a...