Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Emzi0767 committed Jul 20, 2018
1 parent ff72f2e commit 63badee
Show file tree
Hide file tree
Showing 41 changed files with 4,514 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Database/schema_v1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-- This file is a part of Music Turret project.
--
-- Copyright 2018 Emzi0767
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- ----------------------------------------------------------------------------
--
-- Turret's PostgreSQL database schema
--
-- Version: 1
-- Bot version: v1.0.0
-- Timestamp: 2018-07-20 08:24 +02:00
-- Author: Emzi0767
-- Project: Music Turret
-- License: Apache License 2.0
-- PostgreSQL version: 9.6 or above
--
-- ----------------------------------------------------------------------------
--
-- Types

-- entity_type
-- Determines entity type of the attached ID.
create type entity_kind as enum('user', 'channel', 'guild');

-- ----------------------------------------------------------------------------
--
-- Tables

-- metadata
-- This table holds a key-value pairs, which hold various metadata about the
-- database schema. This table is pre-populated.
create table metadata(
meta_key text not null,
meta_value text not null,
primary key(meta_key)
);
insert into metadata(meta_key, meta_value) values
('schema_version', '1'),
('timestamp', '2018-07-20T08:24+02:00'),
('author', 'Emzi0767'),
('project', 'Music Turret'),
('license', 'Apache License 2.0');

-- prefixes
-- Holds information about prefixes set in various guilds and channels.
create table prefixes(
guild_id bigint, -- snowflake
prefixes text[] not null,
enable_default boolean not null default true,
primary key(guild_id)
);

-- blocked_entities
-- Holds information about blocked users, channels, and guilds, along with
-- information about block reason.
create table blocked_entities(
id bigint not null, -- snowflake
kind entity_kind not null,
reason text,
since timestamp with time zone not null,
primary key(id, kind)
);
43 changes: 43 additions & 0 deletions Emzi0767.MusicTurret.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2035
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Emzi0767.MusicTurret", "Emzi0767.MusicTurret\Emzi0767.MusicTurret.csproj", "{9040B8E2-057E-40CF-949D-9F7D9D0BEA04}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E1AE4D00-A5E7-4BDB-A427-C20AA0D16B30}"
ProjectSection(SolutionItems) = preProject
license.txt = license.txt
notice.txt = notice.txt
readme.md = readme.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Database", "Database", "{FBF4A86B-4C48-48A4-B94A-EC436D0600B8}"
ProjectSection(SolutionItems) = preProject
Database\schema_v1.sql = Database\schema_v1.sql
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scripts", "Scripts", "{06C9B896-03DC-456F-81F5-43329B7A018A}"
ProjectSection(SolutionItems) = preProject
Scripts\bot.sh = Scripts\bot.sh
Scripts\lavalink.sh = Scripts\lavalink.sh
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9040B8E2-057E-40CF-949D-9F7D9D0BEA04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9040B8E2-057E-40CF-949D-9F7D9D0BEA04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9040B8E2-057E-40CF-949D-9F7D9D0BEA04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9040B8E2-057E-40CF-949D-9F7D9D0BEA04}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0B23A7AB-E37F-46FE-8B40-F931EAE40A02}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions Emzi0767.MusicTurret/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.json
131 changes: 131 additions & 0 deletions Emzi0767.MusicTurret/AsyncExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// This file is a part of Music Turret project.
//
// Copyright 2018 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Emzi0767.MusicTurret
{
/// <summary>
/// Provides a simplified way of executing asynchronous code synchronously.
/// </summary>
public class AsyncExecutor
{
// this is used to wait for tasks to finish executing.
private SemaphoreSlim Semaphore { get; }

/// <summary>
/// Creates a new instance of asynchronous executor.
/// </summary>
public AsyncExecutor()
{
this.Semaphore = new SemaphoreSlim(1, 1);
}

/// <summary>
/// Executes a specified task in an asynchronous manner, waiting for its completion.
/// </summary>
/// <param name="task">Task to execute.</param>
public void Execute(Task task)
{
// wait for execution slot
this.Semaphore.Wait();

// this is used to capture task execution exception
Exception taskex = null;

// queue a task and wait for it to finish executing
var are = new AutoResetEvent(false);
_ = Task.Run(Executor);
are.WaitOne();

// release execution slot
this.Semaphore.Release();

// check for and rethrow any exceptions
if (taskex != null)
throw new Exception("Exception occured while executing asynchronous code.", taskex);

// executor method
async Task Executor()
{
try
{
// try and execute the supplied task
await task;
}
catch (Exception ex)
{
// capture any exceptions
taskex = ex;
}

// signal that the execution is done
are.Set();
}
}

/// <summary>
/// Executes a specified task in an asynchronous manner, waiting for its completion, and returning the result.
/// </summary>
/// <typeparam name="T">Type of the Task's return value.</typeparam>
/// <param name="task">Task to execute.</param>
/// <returns>Task's result.</returns>
public T Execute<T>(Task<T> task)
{
// wait for execution slot
this.Semaphore.Wait();

// this is used to capture task execution exception and result
Exception taskex = null;
T result = default;

// queue a task and wait for it to finish executing
var are = new AutoResetEvent(false);
_ = Task.Run(Executor);
are.WaitOne();

// release execution slot
this.Semaphore.Release();

// check for and rethrow any exceptions
if (taskex != null)
throw new Exception("Exception occured while executing asynchronous code.", taskex);

// return the execution result
return result;

// executor method
async Task Executor()
{
try
{
// try and execute the supplied task
result = await task;
}
catch (Exception ex)
{
// capture any exceptions
taskex = ex;
}

// signal that the execution is done
are.Set();
}
}
}
}
52 changes: 52 additions & 0 deletions Emzi0767.MusicTurret/Attributes/NotBlockedAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This file is a part of Music Turret project.
//
// Copyright 2018 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Linq;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using Emzi0767.MusicTurret.Data;
using Emzi0767.MusicTurret.Services;
using Microsoft.Extensions.DependencyInjection;

namespace Emzi0767.MusicTurret.Attributes
{
/// <summary>
/// Verifies that the user is not blocked for the purpose of the command usage.
/// </summary>
public sealed class NotBlockedAttribute : CheckBaseAttribute
{
public override Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help)
{
if (ctx.Guild == null)
return Task.FromResult(false);

if (help)
return Task.FromResult(true);

if (ctx.User == ctx.Client.CurrentApplication.Owner)
return Task.FromResult(true);

var uid = (long)ctx.User.Id;
var cid = (long)ctx.Channel.Id;
var gid = (long)ctx.Guild.Id;

var db = ctx.Services.GetService<DatabaseContext>();
var blocked = db.BlockedEntities.Any(x => (x.Id == uid && x.Kind == DatabaseEntityKind.User) || (x.Id == cid && x.Kind == DatabaseEntityKind.Channel) || (x.Id == gid && x.Kind == DatabaseEntityKind.Channel));
return Task.FromResult(blocked);
}
}
}
52 changes: 52 additions & 0 deletions Emzi0767.MusicTurret/Data/DatabaseBlockedEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This file is a part of Music Turret project.
//
// Copyright 2018 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.ComponentModel.DataAnnotations.Schema;

namespace Emzi0767.MusicTurret.Data
{
/// <summary>
/// Represents a collection of entities blocked from using the bot.
/// </summary>
[Table("blocked_entities")]
public partial class DatabaseBlockedEntity
{
/// <summary>
/// Gets or sets the entity's ID.
/// </summary>
[Column("id")]
public long Id { get; set; }

/// <summary>
/// Gets or sets the entity's kind.
/// </summary>
[Column("kind")]
public DatabaseEntityKind Kind { get; set; }

/// <summary>
/// Gets or sets the reason why the entity was blocked.
/// </summary>
[Column("reason")]
public string Reason { get; set; }

/// <summary>
/// Gets or sets when the entity was blocked.
/// </summary>
[Column("since", TypeName = "timestamp with time zone")]
public DateTime Since { get; set; }
}
}
39 changes: 39 additions & 0 deletions Emzi0767.MusicTurret/Data/DatabaseEntityKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is a part of Music Turret project.
//
// Copyright 2018 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Emzi0767.MusicTurret.Data
{
/// <summary>
/// Represents kind of an entity with associated ID.
/// </summary>
public enum DatabaseEntityKind
{
/// <summary>
/// Defines that the entity is a user.
/// </summary>
User,

/// <summary>
/// Defines that the entity is a channel.
/// </summary>
Channel,

/// <summary>
/// Defines that the entity is a guild.
/// </summary>
Guild
}
}
Loading

0 comments on commit 63badee

Please sign in to comment.