Archived
0
0
Fork 0

feat: added database

This commit is contained in:
Daryl Ronningen 2021-10-15 20:18:33 -07:00
parent baf797d8c3
commit c7b933b3f8
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
9 changed files with 281 additions and 80 deletions

View file

@ -12,7 +12,8 @@ namespace Sharpy.Commands.Music
{
[SlashCommand("play", "Plays a song")]
[SlashRequireGuild]
public async Task PlayCommand(InteractionContext ctx, [Option("song", "The song to play. (Accepts YouTube URLs too)")] string song)
public async Task PlayCommand(InteractionContext ctx,
[Option("song", "The song to play. (Accepts YouTube URLs too)")] string song)
{
if (ctx.Member.VoiceState == null || ctx.Member.VoiceState.Channel == null)
{

View file

@ -12,7 +12,8 @@ namespace Sharpy.Commands.Music
{
[SlashCommand("search", "Searches for songs")]
[SlashRequireGuild]
public async Task SearchCommand(InteractionContext ctx, [Option("search", "The song to search for. (Accepts YouTube URLs too)")] string search)
public async Task SearchCommand(InteractionContext ctx,
[Option("search", "The song to search for. (Accepts YouTube URLs too)")] string search)
{
var lava = ctx.Client.GetLavalink();

View file

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Sharpy.Database.Models;
namespace Sharpy.Database
{
public class Context : DbContext
{
public DbSet<Infractions> Infractions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(
$"Host={Sharpy.config.GetSection("database").GetValue<string>("host")};Port={Sharpy.config.GetSection("database").GetValue<string>("port")};Database={Sharpy.config.GetSection("database").GetValue<string>("database")};Username={Sharpy.config.GetSection("database").GetValue<string>("username")};Password={Sharpy.config.GetSection("database").GetValue<string>("password")}");
}
}
}

View file

@ -0,0 +1,28 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace Sharpy.Database.Models
{
public class Infractions
{
[Key]
public Guid InfractionId { get; set; }
[Required]
public InfractionTypes InfractionType { get; set; }
[Required]
public string InfractionReason { get; set; }
[Required]
public ulong UserId { get; set; }
}
public enum InfractionTypes
{
Ban,
Kick,
Mute,
Warn
}
}

View file

@ -0,0 +1,50 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Sharpy.Database;
#nullable disable
namespace Sharpy.Migrations
{
[DbContext(typeof(Context))]
[Migration("20211016030853_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.0-rc.2.21480.5")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Sharpy.Database.Models.Infractions", b =>
{
b.Property<Guid>("InfractionId")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("InfractionReason")
.IsRequired()
.HasColumnType("text");
b.Property<int>("InfractionType")
.HasColumnType("integer");
b.Property<decimal>("UserId")
.HasColumnType("numeric(20,0)");
b.HasKey("InfractionId");
b.ToTable("Infractions");
});
#pragma warning restore 612, 618
}
}
}

View file

@ -0,0 +1,33 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Sharpy.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Infractions",
columns: table => new
{
InfractionId = table.Column<Guid>(type: "uuid", nullable: false),
InfractionType = table.Column<int>(type: "integer", nullable: false),
InfractionReason = table.Column<string>(type: "text", nullable: false),
UserId = table.Column<decimal>(type: "numeric(20,0)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Infractions", x => x.InfractionId);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Infractions");
}
}
}

View file

@ -0,0 +1,48 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Sharpy.Database;
#nullable disable
namespace Sharpy.Migrations
{
[DbContext(typeof(Context))]
partial class ContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.0-rc.2.21480.5")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Sharpy.Database.Models.Infractions", b =>
{
b.Property<Guid>("InfractionId")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("InfractionReason")
.IsRequired()
.HasColumnType("text");
b.Property<int>("InfractionType")
.HasColumnType("integer");
b.Property<decimal>("UserId")
.HasColumnType("numeric(20,0)");
b.HasKey("InfractionId");
b.ToTable("Infractions");
});
#pragma warning restore 612, 618
}
}
}

View file

@ -1,7 +1,4 @@
using System;
#if RELEASE
using System.Net;
#endif
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
@ -13,82 +10,94 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sharpy.Commands.General;
using Sharpy.Commands.Music;
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.Build();
#if DEBUG
var discord = new DiscordClient(new DiscordConfiguration
{
AlwaysCacheMembers = true,
AutoReconnect = true,
GatewayCompressionLevel = GatewayCompressionLevel.Stream,
HttpTimeout = TimeSpan.FromSeconds(10),
Intents = DiscordIntents.All,
LogTimestampFormat = "MMM dd yyyy - hh:mm:ss tt",
MinimumLogLevel = LogLevel.Debug,
ReconnectIndefinitely = false,
Token = config.GetValue<string>("token"),
TokenType = TokenType.Bot
});
#else
var discord = new DiscordClient(new DiscordConfiguration
{
AlwaysCacheMembers = true,
AutoReconnect = true,
GatewayCompressionLevel = GatewayCompressionLevel.Stream,
HttpTimeout = TimeSpan.FromSeconds(10),
Intents = DiscordIntents.All,
LogTimestampFormat = "MMM dd yyyy - hh:mm:ss tt",
MinimumLogLevel = LogLevel.Information,
ReconnectIndefinitely = false,
ShardCount = Convert.ToInt32(Dns.GetHostName().Substring(Dns.GetHostName().IndexOf('-') + 1)),
ShardId = Convert.ToInt32(Dns.GetHostName().Substring(Dns.GetHostName().IndexOf('-') + 1)),
Token = config.GetValue<string>("token"),
TokenType = TokenType.Bot,
});
#if RELEASE
using System.Net;
#endif
var services = new ServiceCollection()
.BuildServiceProvider();
// Register Lavalink
var lavalink = discord.UseLavalink();
// Register Slash Commands
var slash = discord.UseSlashCommands(new SlashCommandsConfiguration
namespace Sharpy
{
Services = services
});
// Register Commands
// General
slash.RegisterCommands<Info>(772228301552222258);
// Music
slash.RegisterCommands<Play>(772228301552222258);
slash.RegisterCommands<Search>(772228301552222258);
await discord.ConnectAsync(new DiscordActivity("With DSharpPlus"));
await lavalink.ConnectAsync(new LavalinkConfiguration
{
Password = config.GetSection("lavalink").GetValue<string>("password"),
RestEndpoint = new ConnectionEndpoint
public static class Sharpy
{
Hostname = config.GetSection("lavalink").GetValue<string>("host"),
Port = config.GetSection("lavalink").GetValue<int>("port"),
Secured = false
},
ResumeTimeout = 30,
SocketAutoReconnect = true,
SocketEndpoint = new ConnectionEndpoint
{
Hostname = config.GetSection("lavalink").GetValue<string>("host"),
Port = config.GetSection("lavalink").GetValue<int>("port"),
Secured = false
},
WebSocketCloseTimeout = 30
});
public static readonly IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.Build();
await Task.Delay(-1);
public static async Task Main()
{
#if DEBUG
var discord = new DiscordClient(new DiscordConfiguration
{
AlwaysCacheMembers = true,
AutoReconnect = true,
GatewayCompressionLevel = GatewayCompressionLevel.Stream,
HttpTimeout = TimeSpan.FromSeconds(10),
Intents = DiscordIntents.All,
LogTimestampFormat = "MMM dd yyyy - hh:mm:ss tt",
MinimumLogLevel = LogLevel.Debug,
ReconnectIndefinitely = false,
Token = config.GetValue<string>("token"),
TokenType = TokenType.Bot
});
#else
var discord = new DiscordClient(new DiscordConfiguration
{
AlwaysCacheMembers = true,
AutoReconnect = true,
GatewayCompressionLevel = GatewayCompressionLevel.Stream,
HttpTimeout = TimeSpan.FromSeconds(10),
Intents = DiscordIntents.All,
LogTimestampFormat = "MMM dd yyyy - hh:mm:ss tt",
MinimumLogLevel = LogLevel.Information,
ReconnectIndefinitely = false,
ShardCount = Convert.ToInt32(Dns.GetHostName().Substring(Dns.GetHostName().IndexOf('-') + 1)),
ShardId = Convert.ToInt32(Dns.GetHostName().Substring(Dns.GetHostName().IndexOf('-') + 1)),
Token = config.GetValue<string>("token"),
TokenType = TokenType.Bot,
});
#endif
var services = new ServiceCollection()
.BuildServiceProvider();
// Register Lavalink
var lavalink = discord.UseLavalink();
// Register Slash Commands
var slash = discord.UseSlashCommands(new SlashCommandsConfiguration
{
Services = services
});
// Register Commands
// General
slash.RegisterCommands<Info>(772228301552222258);
// Music
slash.RegisterCommands<Play>(772228301552222258);
slash.RegisterCommands<Search>(772228301552222258);
await discord.ConnectAsync(new DiscordActivity("With DSharpPlus"));
await lavalink.ConnectAsync(new LavalinkConfiguration
{
Password = config.GetSection("lavalink").GetValue<string>("password"),
RestEndpoint = new ConnectionEndpoint
{
Hostname = config.GetSection("lavalink").GetValue<string>("host"),
Port = config.GetSection("lavalink").GetValue<int>("port"),
Secured = false
},
ResumeTimeout = 30,
SocketAutoReconnect = true,
SocketEndpoint = new ConnectionEndpoint
{
Hostname = config.GetSection("lavalink").GetValue<string>("host"),
Port = config.GetSection("lavalink").GetValue<int>("port"),
Secured = false
},
WebSocketCloseTimeout = 30
});
await Task.Delay(-1);
}
}
}

View file

@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Nullable>disable</Nullable>
<IsPackable>false</IsPackable>
<Configurations>Release;Debug</Configurations>
<Platforms>x64</Platforms>
@ -39,11 +39,21 @@
<PackageReference Include="DSharpPlus.Lavalink" Version="4.2.0-nightly-01030" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.2.0-nightly-01030" />
<PackageReference Include="Hardware.Info" Version="1.1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-rc.2.21480.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-rc.2.21480.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="NATS.Client" Version="0.14.0-pre3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-rc.2" />
</ItemGroup>
<ItemGroup>
@ -52,4 +62,8 @@
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="Commands\Moderation" />
</ItemGroup>
</Project>