Skip to content

Commit

Permalink
Merge branch 'release/v1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed May 28, 2023
2 parents f8e77b7 + eed1328 commit 65786d6
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 23 deletions.
43 changes: 36 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
# simplebot

simplebot is a discord bot to entertain your server
# 🤖 simplebot

## Setup
Make sure that you installed [.NET 7.0 or newer](https://dotnet.microsoft.com/en-us/download).
Simple Discord bot to entertain your server
## Bot Setup
### Requirements:
- [.NET 7.0 or newer](https://dotnet.microsoft.com/en-us/download).
- [API Ninjas API Key](https://api-ninjas.com/api)
- [Discord Application (bot)](https://discord.com/developers/applications)

To use this bot, you need create a `config.json` file in root directory with the following content:
### Setting up bot:
- Create your aplication at Discord Developer Portal ![Image 1](https://cdn.discordapp.com/attachments/969140752766631969/1112370145889894400/ss1.png)

- In your app, go to `Bot/Privileged Gateway Intents` and enable all settings ![Image 2](https://cdn.discordapp.com/attachments/969140752766631969/1112370606256705617/ss2.png)

- Next, go to `OAuth2/URL Generator` and generate bot invite link ![Image 3](https://cdn.discordapp.com/attachments/969140752766631969/1112371418689183794/ss3.png) ![Image 4](https://cdn.discordapp.com/attachments/969140752766631969/1112371838278963230/ss4.png)

- And here we go! We now created and configured your bot!

### Getting Bot Token
#### To get bot working, we need bot token
- To get one, go to `Bot/Reset Token` and click `Reset Token`: ![Image 5](https://cdn.discordapp.com/attachments/969140752766631969/1112373709311852654/ss5.png)
- If you have 2FA Authentication, you will be asked to prompt a 6 digit code
- Voulà! Now we have Discord Bot Token!

### Configurating `config.json`:
- If you have bot token and API Ninja Api Key, create `config.json` file in root directory (where is `Program.cs` file)
```json
{
"token": "YOUR DISCORD BOT TOKEN",
"prefix": ">"
"token": "Discord bot token here",
"prefix": ">",
"api-ninjas_apikey": "API Ninjas API Key here"
}
```


## Acknowledgements

- [RestSharp](https://restsharp.dev)
- [Newtonsoft.Json](https://www.newtonsoft.com/json)
- [DSharpPlus](https://dsharpplus.github.io/DSharpPlus/)
- [Excuser API](https://excuser-three.vercel.app)

5 changes: 5 additions & 0 deletions simplebot/Api/ApiRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace simplebot.Api;

public abstract class ApiRequest {
private protected abstract string GetRequest();
}
44 changes: 44 additions & 0 deletions simplebot/Api/GetExcuse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using RestSharp;
using Newtonsoft.Json;
using simplebot.Classes;

namespace simplebot.Api;

public class GetExcuse : ApiRequest {
private const string BaseUrl = "https://excuser-three.vercel.app/v1/";

private protected override string GetRequest() {
try {
var client = new RestClient(BaseUrl);
var request = new RestRequest("excuse");
var response = client.Get(request).Content;

if (response == null) {
throw new Exception("Response is null");
}

return response;
}
catch (Exception e) {
Console.WriteLine(e);
throw;
}
}

public List<ExcuseClass> ParseData() {
try {
var data = GetRequest();
var excuse = JsonConvert.DeserializeObject<List<ExcuseClass>>(data);

if (excuse == null) {
throw new Exception("Excuse is null");
}

return excuse;
}
catch (Exception e) {
Console.WriteLine(e);
throw;
}
}
}
47 changes: 47 additions & 0 deletions simplebot/Api/GetRandomFact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Newtonsoft.Json;
using RestSharp;
using simplebot.Classes;

namespace simplebot.Api;

public class GetRandomFact : ApiRequest {
private const string BaseUrl = "https://api.api-ninjas.com/v1/";

private protected override string GetRequest() {
try {
var apiKey = ConfigHandler.GetConfig().ApiNinjasApiKey;

var client = new RestClient(BaseUrl);
var request = new RestRequest("facts?limit=1").AddHeader("X-Api-Key", apiKey);

var response = client.Get(request).Content;

if (response == null) {
throw new Exception("Response is null");
}

return response;
}
catch (Exception e) {
Console.WriteLine(e);
throw;
}
}

public List<FactClass> ParseData() {
try {
var data = GetRequest();
var fact = JsonConvert.DeserializeObject<List<FactClass>>(data);

if (fact == null) {
throw new Exception("Fact is null");
}

return fact;
}
catch (Exception e) {
Console.WriteLine(e);
throw;
}
}
}
21 changes: 8 additions & 13 deletions simplebot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Newtonsoft.Json;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
Expand All @@ -15,19 +16,11 @@ public class Bot {
public InteractivityExtension Interactivity { get; private set; }

public async Task RunAsync() {
string json;

await using (var fs = File.OpenRead("config.json")) {
using (var sr = new StreamReader(fs, new UTF8Encoding(false))) {
json = await sr.ReadToEndAsync().ConfigureAwait(false);
}
}

var configJson = JsonConvert.DeserializeObject<Config>(json);
var json = ConfigHandler.GetConfig();

var config = new DiscordConfiguration {
Intents = DiscordIntents.All,
Token = configJson.Token,
Token = json.Token,
TokenType = TokenType.Bot,
AutoReconnect = true,
MinimumLogLevel = Microsoft.Extensions.Logging.LogLevel.Debug,
Expand All @@ -39,18 +32,20 @@ public async Task RunAsync() {
});

var commandsConfig = new CommandsNextConfiguration() {
StringPrefixes = new [] {configJson.Prefix},
StringPrefixes = new [] { json.Prefix },
EnableMentionPrefix = true,
EnableDms = true,
EnableDefaultHelp = false
EnableDefaultHelp = true
};

Commands = Client.UseCommandsNext(commandsConfig);

// commands registration
Commands.RegisterCommands<UtilityCommands>();
Commands.RegisterCommands<ModerationCommand>();
Commands.RegisterCommands<FunCommands>();

await Client.ConnectAsync();
await Client.ConnectAsync(new DiscordActivity("Powered by SimpleBot", ActivityType.Watching));
await Task.Delay(-1); // make the bot stay online
}

Expand Down
8 changes: 8 additions & 0 deletions simplebot/Classes/ExcuseClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Newtonsoft.Json;

namespace simplebot.Classes;

public class ExcuseClass {
[JsonProperty("excuse")] public string Excuse { get; set; } = null!;
[JsonProperty("category")] public string Category { get; set; } = null!;
}
7 changes: 7 additions & 0 deletions simplebot/Classes/FactClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Newtonsoft.Json;

namespace simplebot.Classes;

public class FactClass {
[JsonProperty("fact")] public string Fact { get; set; } = null!;
}
58 changes: 58 additions & 0 deletions simplebot/Commands/FunCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using simplebot.Api;

namespace simplebot.Commands;

public class FunCommands : BaseCommandModule {
[Command("8ball")]
public async Task EightBallAsync(CommandContext ctx, string input) {
var responses = new[] {
"Yes",
"No",
"Maybe",
"Ask again later",
"I don't know",
"I don't think so",
"I think so",
};

var embed = new DiscordEmbedBuilder() {
Title = ":8ball: 8ball",
Description = $"Question: {input}\nAnswer: {responses[new Random().Next(0, responses.Length)]}",
Color = DiscordColor.Gold,
Timestamp = DateTime.Now
};

await ctx.Channel.SendMessageAsync(embed);
}

[Command("excuse")]
public async Task ExcuseAsync(CommandContext ctx) {
var excuse = new GetExcuse().ParseData();

var embed = new DiscordEmbedBuilder() {
Title = "Random Excuse",
Description = $"**Excuse:** `{excuse[0].Excuse}`\n**Category:** `{excuse[0].Category}`",
Color = DiscordColor.Magenta,
Timestamp = DateTime.Now
};

await ctx.Channel.SendMessageAsync(embed);
}

[Command("randomfact")]
public async Task RandomFactAsync(CommandContext ctx) {
var fact = new GetRandomFact().ParseData();

var embed = new DiscordEmbedBuilder() {
Title = ":question: Did you know?",
Description = $"`{fact[0].Fact}`",
Color = DiscordColor.DarkGreen,
Timestamp = DateTime.Now
};

await ctx.Channel.SendMessageAsync(embed);
}
}
Loading

0 comments on commit 65786d6

Please sign in to comment.