Skip to content

Commit

Permalink
Merge pull request #18 from Nikolai558/main
Browse files Browse the repository at this point in the history
Releasing 1.2.0-alpha
  • Loading branch information
Nikolai558 authored Apr 14, 2022
2 parents c47ff3f + 0590b98 commit 66c7002
Show file tree
Hide file tree
Showing 26 changed files with 499 additions and 182 deletions.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ assignees: Nikolai558

---

## Document Name:
**Document Name:**


## Requested Changes:
**Requested Changes:**


## Additional Context:
**Additional Context:**
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config.json
.env

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
Expand Down
12 changes: 12 additions & 0 deletions FEBuddyDiscordBot/DataAccess/DB/IMongoDbConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using FEBuddyDiscordBot.Models;
using MongoDB.Driver;

namespace FEBuddyDiscordBot.DataAccess.DB;

public interface IMongoDbConnection
{
MongoClient Client { get; }
string DbName { get; }
IMongoCollection<GuildModel> GuildCollection { get; }
string GuildCollectionName { get; }
}
11 changes: 11 additions & 0 deletions FEBuddyDiscordBot/DataAccess/DB/IMongoGuildData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FEBuddyDiscordBot.Models;

namespace FEBuddyDiscordBot.DataAccess.DB;

public interface IMongoGuildData
{
Task CreateGuild(GuildModel guild);
Task<List<GuildModel>> GetAllGuildsAsync();
Task<GuildModel> GetGuildAsync(string id);
Task UpdateGuild(GuildModel guild);
}
26 changes: 26 additions & 0 deletions FEBuddyDiscordBot/DataAccess/DB/MongoDbConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using MongoDB.Driver;
using FEBuddyDiscordBot.Models;

namespace FEBuddyDiscordBot.DataAccess.DB;
public class MongoDbConnection : IMongoDbConnection
{
private string _connectionId = "MongoDB";
private readonly IConfiguration _config;
private readonly IMongoDatabase _db;

public string DbName { get; private set; }
public string GuildCollectionName { get; private set; } = "discord-guilds";

public MongoClient Client { get; private set; }
public IMongoCollection<GuildModel> GuildCollection { get; private set; }

public MongoDbConnection(IConfiguration config)
{
_config = config;
Client = new MongoClient(_config.GetConnectionString(_connectionId));
DbName = _config["DatabaseName"];
_db = Client.GetDatabase(DbName);

GuildCollection = _db.GetCollection<GuildModel>(GuildCollectionName);
}
}
41 changes: 41 additions & 0 deletions FEBuddyDiscordBot/DataAccess/DB/MongoGuildData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using FEBuddyDiscordBot.Models;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FEBuddyDiscordBot.DataAccess.DB;
public class MongoGuildData : IMongoGuildData
{
private readonly IMongoCollection<GuildModel> _guilds;

public MongoGuildData(IMongoDbConnection db)
{
_guilds = db.GuildCollection;
}

public async Task<List<GuildModel>> GetAllGuildsAsync()
{
var results = await _guilds.FindAsync(_ => true);
return results.ToList();
}

public async Task<GuildModel> GetGuildAsync(string id)
{
var results = await _guilds.FindAsync(guild => guild.GuildId == id);
return results.FirstOrDefault();
}

public Task CreateGuild(GuildModel guild)
{
return _guilds.InsertOneAsync(guild);
}

public Task UpdateGuild(GuildModel guild)
{
var filter = Builders<GuildModel>.Filter.Eq("Id", guild.GuildId);
return _guilds.ReplaceOneAsync(filter, guild, new ReplaceOptions { IsUpsert = true });
}
}
26 changes: 26 additions & 0 deletions FEBuddyDiscordBot/DataAccess/VatusaApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Net;
using System.Text.Json;
using static FEBuddyDiscordBot.Models.VatusaUserModel;

namespace FEBuddyDiscordBot.DataAccess;
public class VatusaApi
{
public async Task<VatusaUserData?> GetVatusaUserInfo(ulong MemberId)
{
string url = $"https://api.vatusa.net/v2/user/{MemberId}?d";

using (WebClient webClient = new WebClient())
{
try
{
string jsonResponse = await webClient.DownloadStringTaskAsync(url);
VatusaUserData? userData = JsonSerializer.Deserialize<VatusaUserData>(jsonResponse);
return userData;
}
catch (WebException)
{
return null;
}
}
}
}
2 changes: 1 addition & 1 deletion FEBuddyDiscordBot/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .

COPY config.json .
COPY appsettings.json .
ENTRYPOINT ["dotnet", "FEBuddyDiscordBot.dll"]
20 changes: 19 additions & 1 deletion FEBuddyDiscordBot/FEBuddyDiscordBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>7d53816e-d5a7-4120-8b6d-8976fba7a785</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand All @@ -15,16 +16,33 @@
<PackageReference Include="Discord.Net" Version="3.5.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="MongoDB.Driver" Version="2.15.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="config.json">
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
42 changes: 18 additions & 24 deletions FEBuddyDiscordBot/FeBuddyBot.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using FEBuddyDiscordBot.DataAccess;
using FEBuddyDiscordBot.DataAccess.DB;
using FEBuddyDiscordBot.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Serilog.Events;

namespace FEBuddyDiscordBot;
public class FeBuddyBot
{
private static IConfigurationRoot _config;
private static IConfiguration _config;

public async Task StartAsync()
{
var _builder = new ConfigurationBuilder().SetBasePath(AppContext.BaseDirectory).AddJsonFile(path: "config.json");
var _builder = new ConfigurationBuilder().SetBasePath(AppContext.BaseDirectory).AddJsonFile(path: "appsettings.json");
_config = _builder.Build();

DiscordSocketConfig discordConfig = new DiscordSocketConfig()
{

GatewayIntents = GatewayIntents.GuildMembers | GatewayIntents.DirectMessages | GatewayIntents.GuildMessages | GatewayIntents.Guilds | GatewayIntents.GuildVoiceStates
};

Expand All @@ -37,15 +28,18 @@ public async Task StartAsync()
.AddSingleton<StartupService>()
.AddSingleton<LoggingService>()
.AddSingleton<CommandHandler>()
.AddSingleton<RoleAssignmentService>();
.AddSingleton<RoleAssignmentService>()
.AddSingleton<VatusaApi>()
.AddSingleton<IMongoDbConnection, MongoDbConnection>()
.AddSingleton<IMongoGuildData, MongoGuildData>();

ConfigureServices(services);

var serviceProvider = services.BuildServiceProvider();

serviceProvider.GetRequiredService<LoggingService>();

await serviceProvider.GetRequiredService<StartupService>().StartAsync();
await serviceProvider.GetRequiredService<StartupService>().StartAsync(UseDevToken: true);

serviceProvider.GetRequiredService<CommandHandler>();

Expand All @@ -60,19 +54,19 @@ private void ConfigureServices(IServiceCollection services)

services.RemoveAll<IHttpMessageHandlerBuilderFilter>();

var logLevel = _config["logLevel"];
var level = Serilog.Events.LogEventLevel.Error;
string logLevel = _config.GetRequiredSection("Logging").GetRequiredSection("LogLevel").Value;
LogEventLevel level = LogEventLevel.Error;

if (!string.IsNullOrEmpty(logLevel))
{
switch (logLevel.ToLower())
{
case "error": { level = Serilog.Events.LogEventLevel.Error; break; }
case "info": { level = Serilog.Events.LogEventLevel.Information; break; }
case "debug": { level = Serilog.Events.LogEventLevel.Debug; break; }
case "crit": { level = Serilog.Events.LogEventLevel.Fatal; break; }
case "warn": { level = Serilog.Events.LogEventLevel.Warning; break; }
case "trace": { level = Serilog.Events.LogEventLevel.Debug; break; }
case "error": { level = LogEventLevel.Error; break; }
case "info": { level = LogEventLevel.Information; break; }
case "debug": { level = LogEventLevel.Debug; break; }
case "crit": { level = LogEventLevel.Fatal; break; }
case "warn": { level = LogEventLevel.Warning; break; }
case "trace": { level = LogEventLevel.Debug; break; }
}
}

Expand Down
6 changes: 6 additions & 0 deletions FEBuddyDiscordBot/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global using Discord;
global using Discord.Commands;
global using Discord.WebSocket;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
12 changes: 12 additions & 0 deletions FEBuddyDiscordBot/Models/GuildModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace FEBuddyDiscordBot.Models;
public class GuildModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string GuildId { get; set; }
public string Name { get; set; }
}
8 changes: 1 addition & 7 deletions FEBuddyDiscordBot/Models/VatusaUserModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FEBuddyDiscordBot.Models;
namespace FEBuddyDiscordBot.Models;
public class VatusaUserModel
{

Expand Down
19 changes: 3 additions & 16 deletions FEBuddyDiscordBot/Modules/Admin/AdminCommands.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FEBuddyDiscordBot.Modules.Admin;
namespace FEBuddyDiscordBot.Modules.Admin;

[Name("Admin Commands")]
[Summary("These commands are to assist the Administrators of the server.")]
[RequireUserPermission(Discord.GuildPermission.Administrator)]
public class AdminCommands: ModuleBase
{
private readonly IServiceProvider _services;
private readonly IConfigurationRoot _config;
private readonly IConfiguration _config;
private readonly DiscordSocketClient _discord;
private readonly ILogger _logger;
private readonly string _prefix;

public AdminCommands(IServiceProvider services)
{
_services = services;
_config = _services.GetRequiredService<IConfigurationRoot>();
_config = _services.GetRequiredService<IConfiguration>();
_discord = _services.GetRequiredService<DiscordSocketClient>();
_logger = _services.GetRequiredService<ILogger<AdminCommands>>();
_prefix = _config["prefix"];

_logger.LogInformation("Module: Loaded AdminCommands");
}
Expand Down
19 changes: 3 additions & 16 deletions FEBuddyDiscordBot/Modules/Owner/OwnerCommands.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace FEBuddyDiscordBot.Modules.Owner;

[Name("Bot Owner Commands")]
Expand All @@ -18,18 +7,16 @@ namespace FEBuddyDiscordBot.Modules.Owner;
public class OwnerCommands : ModuleBase
{
private readonly IServiceProvider _services;
private readonly IConfigurationRoot _config;
private readonly IConfiguration _config;
private readonly DiscordSocketClient _discord;
private readonly ILogger _logger;
private readonly string _prefix;

public OwnerCommands(IServiceProvider services)
{
_services = services;
_config = _services.GetRequiredService<IConfigurationRoot>();
_config = _services.GetRequiredService<IConfiguration>();
_discord = _services.GetRequiredService<DiscordSocketClient>();
_logger = _services.GetRequiredService<ILogger<OwnerCommands>>();
_prefix = _config["prefix"];

_logger.LogInformation("Module: Loaded OwnerCommands");
}
Expand Down
Loading

0 comments on commit 66c7002

Please sign in to comment.