Initial split of the modules

This commit is contained in:
Master Kwoth
2017-09-30 00:46:33 +02:00
parent cdc2c43913
commit 599245b1ca
499 changed files with 469 additions and 256 deletions

View File

@ -0,0 +1,33 @@
using NadekoBot.Services.Database.Repositories;
using System;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database
{
public interface IUnitOfWork : IDisposable
{
NadekoContext _context { get; }
IQuoteRepository Quotes { get; }
IGuildConfigRepository GuildConfigs { get; }
IDonatorsRepository Donators { get; }
IClashOfClansRepository ClashOfClans { get; }
IReminderRepository Reminders { get; }
ISelfAssignedRolesRepository SelfAssignedRoles { get; }
IBotConfigRepository BotConfig { get; }
IUnitConverterRepository ConverterUnits { get; }
ICustomReactionRepository CustomReactions { get; }
ICurrencyRepository Currency { get; }
ICurrencyTransactionsRepository CurrencyTransactions { get; }
IMusicPlaylistRepository MusicPlaylists { get; }
IPokeGameRepository PokeGame { get; }
IWaifuRepository Waifus { get; }
IDiscordUserRepository DiscordUsers { get; }
IWarningsRepository Warnings { get; }
IXpRepository Xp { get; }
IClubRepository Clubs { get; }
int Complete();
Task<int> CompleteAsync();
}
}

View File

@ -0,0 +1,51 @@
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Models
{
public class AntiRaidSetting : DbEntity
{
public int GuildConfigId { get; set; }
public GuildConfig GuildConfig { get; set; }
public int UserThreshold { get; set; }
public int Seconds { get; set; }
public PunishmentAction Action { get; set; }
}
public class AntiSpamSetting : DbEntity
{
public int GuildConfigId { get; set; }
public GuildConfig GuildConfig { get; set; }
public PunishmentAction Action { get; set; }
public int MessageThreshold { get; set; } = 3;
public int MuteTime { get; set; } = 0;
public HashSet<AntiSpamIgnore> IgnoredChannels { get; set; } = new HashSet<AntiSpamIgnore>();
}
public enum PunishmentAction
{
Mute,
Kick,
Ban,
Softban
}
public class AntiSpamIgnore : DbEntity
{
public ulong ChannelId { get; set; }
public override int GetHashCode() => ChannelId.GetHashCode();
public override bool Equals(object obj)
{
var inst = obj as AntiSpamIgnore;
if (inst == null)
return false;
return inst.ChannelId == ChannelId;
}
}
}

View File

@ -0,0 +1,177 @@
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Models
{
public class BotConfig : DbEntity
{
public HashSet<BlacklistItem> Blacklist { get; set; }
public ulong BufferSize { get; set; } = 4000000;
public bool ForwardMessages { get; set; } = true;
public bool ForwardToAllOwners { get; set; } = true;
public float CurrencyGenerationChance { get; set; } = 0.02f;
public int CurrencyGenerationCooldown { get; set; } = 10;
public HashSet<ModulePrefix> ModulePrefixes { get; set; } = new HashSet<ModulePrefix>();
public List<PlayingStatus> RotatingStatusMessages { get; set; } = new List<PlayingStatus>();
public bool RotatingStatuses { get; set; } = false;
public string RemindMessageFormat { get; set; } = "❗⏰**I've been told to remind you to '%message%' now by %user%.**⏰❗";
//currency
public string CurrencySign { get; set; } = "🌸";
public string CurrencyName { get; set; } = "Nadeko Flower";
public string CurrencyPluralName { get; set; } = "Nadeko Flowers";
public int TriviaCurrencyReward { get; set; } = 0;
public int MinimumBetAmount { get; set; } = 2;
public float BetflipMultiplier { get; set; } = 1.95f;
public int CurrencyDropAmount { get; set; } = 1;
public int? CurrencyDropAmountMax { get; set; } = null;
public float Betroll67Multiplier { get; set; } = 2;
public float Betroll91Multiplier { get; set; } = 4;
public float Betroll100Multiplier { get; set; } = 10;
//public HashSet<CommandCost> CommandCosts { get; set; } = new HashSet<CommandCost>();
/// <summary>
/// I messed up, don't use
/// </summary>
public HashSet<CommandPrice> CommandPrices { get; set; } = new HashSet<CommandPrice>();
public HashSet<EightBallResponse> EightBallResponses { get; set; } = new HashSet<EightBallResponse>();
public HashSet<RaceAnimal> RaceAnimals { get; set; } = new HashSet<RaceAnimal>();
public string DMHelpString { get; set; } = "Type `.h` for help.";
public string HelpString { get; set; } = @"To add me to your server, use this link -> <https://discordapp.com/oauth2/authorize?client_id={0}&scope=bot&permissions=66186303>
You can use `{1}modules` command to see a list of all modules.
You can use `{1}commands ModuleName`
(for example `{1}commands Administration`) to see a list of all of the commands in that module.
For a specific command help, use `{1}h CommandName` (for example {1}h {1}q)
**LIST OF COMMANDS CAN BE FOUND ON THIS LINK**
<http://nadekobot.readthedocs.io/en/latest/Commands%20List/>
Nadeko Support Server: https://discord.gg/nadekobot";
public int MigrationVersion { get; set; }
public string OkColor { get; set; } = "71cd40";
public string ErrorColor { get; set; } = "ee281f";
public string Locale { get; set; } = null;
public List<StartupCommand> StartupCommands { get; set; }
public HashSet<BlockedCmdOrMdl> BlockedCommands { get; set; }
public HashSet<BlockedCmdOrMdl> BlockedModules { get; set; }
public int PermissionVersion { get; set; }
public string DefaultPrefix { get; set; } = ".";
public bool CustomReactionsStartWith { get; set; } = false;
public int XpPerMessage { get; set; } = 3;
public int XpMinutesTimeout { get; set; } = 5;
}
public class BlockedCmdOrMdl : DbEntity
{
public string Name { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return ((BlockedCmdOrMdl)obj).Name.ToLowerInvariant() == Name.ToLowerInvariant();
}
public override int GetHashCode() => Name.GetHashCode();
}
public class StartupCommand : DbEntity, IIndexed
{
public int Index { get; set; }
public string CommandText { get; set; }
public ulong ChannelId { get; set; }
public string ChannelName { get; set; }
public ulong? GuildId { get; set; }
public string GuildName { get; set; }
public ulong? VoiceChannelId { get; set; }
public string VoiceChannelName { get; set; }
}
public class PlayingStatus :DbEntity
{
public string Status { get; set; }
}
public class BlacklistItem : DbEntity
{
public ulong ItemId { get; set; }
public BlacklistType Type { get; set; }
}
public enum BlacklistType
{
Server,
Channel,
User
}
public class EightBallResponse : DbEntity
{
public string Text { get; set; }
public override int GetHashCode()
{
return Text.GetHashCode();
}
public override bool Equals(object obj)
{
if (!(obj is EightBallResponse))
return base.Equals(obj);
return ((EightBallResponse)obj).Text == Text;
}
}
public class RaceAnimal : DbEntity
{
public string Icon { get; set; }
public string Name { get; set; }
public override int GetHashCode()
{
return Icon.GetHashCode();
}
public override bool Equals(object obj)
{
if (!(obj is RaceAnimal))
return base.Equals(obj);
return ((RaceAnimal)obj).Icon == Icon;
}
}
public class ModulePrefix : DbEntity
{
public string ModuleName { get; set; }
public string Prefix { get; set; }
public override int GetHashCode()
{
return ModuleName.GetHashCode();
}
public override bool Equals(object obj)
{
if(!(obj is ModulePrefix))
return base.Equals(obj);
return ((ModulePrefix)obj).ModuleName == ModuleName;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace NadekoBot.Services.Database.Models
{
public class ClashCaller : DbEntity
{
public int? SequenceNumber { get; set; } = null;
public string CallUser { get; set; }
public DateTime TimeAdded { get; set; }
public bool BaseDestroyed { get; set; }
public int Stars { get; set; } = 3;
public int ClashWarId { get; set; }
[ForeignKey(nameof(ClashWarId))]
public ClashWar ClashWar { get; set; }
}
}

View File

@ -0,0 +1,32 @@
using Discord;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace NadekoBot.Services.Database.Models
{
public class ClashWar : DbEntity
{
public string EnemyClan { get; set; }
public int Size { get; set; }
public StateOfWar WarState { get; set; } = StateOfWar.Created;
public DateTime StartedAt { get; set; }
public ulong GuildId { get; set; }
public ulong ChannelId { get; set; }
[NotMapped]
public ITextChannel Channel { get; set; }
public List<ClashCaller> Bases { get; set; } = new List<ClashCaller>();
}
public enum DestroyStars
{
One, Two, Three
}
public enum StateOfWar
{
Started, Ended, Created
}
}

View File

@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace NadekoBot.Services.Database.Models
{
public class ClubInfo : DbEntity
{
[MaxLength(20)]
public string Name { get; set; }
public int Discrim { get; set; }
public string ImageUrl { get; set; } = "";
public int MinimumLevelReq { get; set; } = 5;
public int Xp { get; set; } = 0;
public int OwnerId { get; set; }
public DiscordUser Owner { get; set; }
public List<DiscordUser> Users { get; set; } = new List<DiscordUser>();
public List<ClubApplicants> Applicants { get; set; } = new List<ClubApplicants>();
public List<ClubBans> Bans { get; set; } = new List<ClubBans>();
public override string ToString()
{
return Name + "#" + Discrim;
}
}
public class ClubApplicants
{
public int ClubId { get; set; }
public ClubInfo Club { get; set; }
public int UserId { get; set; }
public DiscordUser User { get; set; }
}
public class ClubBans
{
public int ClubId { get; set; }
public ClubInfo Club { get; set; }
public int UserId { get; set; }
public DiscordUser User { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace NadekoBot.Services.Database.Models
{
public class CommandCooldown : DbEntity
{
public int Seconds { get; set; }
public string CommandName { get; set; }
}
}

View File

@ -0,0 +1,21 @@
namespace NadekoBot.Services.Database.Models
{
public class CommandCost : DbEntity
{
public int Cost { get; set; }
public string CommandName { get; set; }
public override int GetHashCode() =>
CommandName.GetHashCode();
public override bool Equals(object obj)
{
var instance = obj as CommandCost;
if (instance == null)
return false;
return instance.CommandName == CommandName;
}
}
}

View File

@ -0,0 +1,9 @@
namespace NadekoBot.Services.Database.Models
{
public class CommandPrice : DbEntity
{
public int Price { get; set; }
//this is unique
public string CommandName { get; set; }
}
}

View File

@ -0,0 +1,47 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
namespace NadekoBot.Services.Database.Models
{
public class ConvertUnit : DbEntity
{
public ConvertUnit() { }
[NotMapped]
private string[] _triggersValue;
[NotMapped]
public string[] Triggers
{
get
{
return _triggersValue ?? (_triggersValue = InternalTrigger.Split('|'));
}
set
{
_triggersValue = value;
InternalTrigger = string.Join("|", _triggersValue);
}
}
//protected or private?
/// <summary>
/// DO NOT CALL THIS
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public string InternalTrigger { get; set; }
public string UnitType { get; set; }
public decimal Modifier { get; set; }
public override bool Equals(object obj)
{
var cu = obj as ConvertUnit;
if (cu == null)
return false;
return cu.UnitType == this.UnitType;
}
public override int GetHashCode()
{
return this.UnitType.GetHashCode();
}
}
}

View File

@ -0,0 +1,8 @@
namespace NadekoBot.Services.Database.Models
{
public class Currency : DbEntity
{
public ulong UserId { get; set; }
public long Amount { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace NadekoBot.Services.Database.Models
{
public class CurrencyTransaction : DbEntity
{
public long Amount { get; set; }
public string Reason { get; set; }
public ulong UserId { get; set; }
}
}

View File

@ -0,0 +1,33 @@
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.RegularExpressions;
namespace NadekoBot.Services.Database.Models
{
public class CustomReaction : DbEntity
{
public ulong? GuildId { get; set; }
[NotMapped]
[JsonIgnore]
public Regex Regex { get; set; }
public string Response { get; set; }
public string Trigger { get; set; }
public bool IsRegex { get; set; }
public bool OwnerOnly { get; set; }
public bool AutoDeleteTrigger { get; set; }
public bool DmResponse { get; set; }
[JsonIgnore]
public bool IsGlobal => !GuildId.HasValue;
public bool ContainsAnywhere { get; set; }
}
public class ReactionResponse : DbEntity
{
public bool OwnerOnly { get; set; }
public string Text { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace NadekoBot.Services.Database.Models
{
public class DbEntity
{
[Key]
public int Id { get; set; }
public DateTime? DateAdded { get; set; } = DateTime.UtcNow;
}
}

View File

@ -0,0 +1,35 @@
using System;
namespace NadekoBot.Services.Database.Models
{
public class DiscordUser : DbEntity
{
public ulong UserId { get; set; }
public string Username { get; set; }
public string Discriminator { get; set; }
public string AvatarId { get; set; }
public ClubInfo Club { get; set; }
public bool IsClubAdmin { get; set; }
public int TotalXp { get; set; }
public DateTime LastLevelUp { get; set; } = DateTime.UtcNow;
public DateTime LastXpGain { get; set; } = DateTime.MinValue;
public XpNotificationType NotifyOnLevelUp { get; set; }
public override bool Equals(object obj)
{
return obj is DiscordUser du
? du.UserId == UserId
: false;
}
public override int GetHashCode()
{
return UserId.GetHashCode();
}
public override string ToString() =>
Username + "#" + Discriminator;
}
}

View File

@ -0,0 +1,9 @@
namespace NadekoBot.Services.Database.Models
{
public class Donator : DbEntity
{
public ulong UserId { get; set; }
public string Name { get; set; }
public int Amount { get; set; } = 0;
}
}

View File

@ -0,0 +1,23 @@
namespace NadekoBot.Services.Database.Models
{
public class FeedSub : DbEntity
{
public int GuildConfigId { get; set; }
public GuildConfig GuildConfig { get; set; }
public ulong ChannelId { get; set; }
public string Url { get; set; }
public override int GetHashCode()
{
return Url.GetHashCode() ^ GuildConfigId.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is FeedSub s
? s.Url == Url && s.GuildConfigId == GuildConfigId
: false;
}
}
}

View File

@ -0,0 +1,31 @@
namespace NadekoBot.Services.Database.Models
{
public class FollowedStream : DbEntity
{
public ulong ChannelId { get; set; }
public string Username { get; set; }
public FollowedStreamType Type { get; set; }
public ulong GuildId { get; set; }
public enum FollowedStreamType
{
Twitch, Smashcast, Mixer
}
public override int GetHashCode() =>
ChannelId.GetHashCode() ^
Username.GetHashCode() ^
Type.GetHashCode();
public override bool Equals(object obj)
{
var fs = obj as FollowedStream;
if (fs == null)
return false;
return fs.ChannelId == ChannelId &&
fs.Username.ToLowerInvariant().Trim() == Username.ToLowerInvariant().Trim() &&
fs.Type == Type;
}
}
}

View File

@ -0,0 +1,254 @@
using System;
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Models
{
public class GuildConfig : DbEntity
{
public ulong GuildId { get; set; }
public string Prefix { get; set; } = null;
public bool DeleteMessageOnCommand { get; set; }
public ulong AutoAssignRoleId { get; set; }
//greet stuff
public bool AutoDeleteGreetMessages { get; set; } //unused
public bool AutoDeleteByeMessages { get; set; } // unused
public int AutoDeleteGreetMessagesTimer { get; set; } = 30;
public int AutoDeleteByeMessagesTimer { get; set; } = 30;
public ulong GreetMessageChannelId { get; set; }
public ulong ByeMessageChannelId { get; set; }
public bool SendDmGreetMessage { get; set; }
public string DmGreetMessageText { get; set; } = "Welcome to the %server% server, %user%!";
public bool SendChannelGreetMessage { get; set; }
public string ChannelGreetMessageText { get; set; } = "Welcome to the %server% server, %user%!";
public bool SendChannelByeMessage { get; set; }
public string ChannelByeMessageText { get; set; } = "%user% has left!";
public LogSetting LogSetting { get; set; } = new LogSetting();
//self assignable roles
public bool ExclusiveSelfAssignedRoles { get; set; }
public bool AutoDeleteSelfAssignedRoleMessages { get; set; }
public float DefaultMusicVolume { get; set; } = 1.0f;
public bool VoicePlusTextEnabled { get; set; }
//stream notifications
public HashSet<FollowedStream> FollowedStreams { get; set; } = new HashSet<FollowedStream>();
//currencyGeneration
public HashSet<GCChannelId> GenerateCurrencyChannelIds { get; set; } = new HashSet<GCChannelId>();
//permissions
public Permission RootPermission { get; set; } = null;
public List<Permissionv2> Permissions { get; set; }
public bool VerbosePermissions { get; set; } = true;
public string PermissionRole { get; set; } = "Nadeko";
public HashSet<CommandCooldown> CommandCooldowns { get; set; } = new HashSet<CommandCooldown>();
//filtering
public bool FilterInvites { get; set; }
public HashSet<FilterChannelId> FilterInvitesChannelIds { get; set; } = new HashSet<FilterChannelId>();
public bool FilterWords { get; set; }
public HashSet<FilteredWord> FilteredWords { get; set; } = new HashSet<FilteredWord>();
public HashSet<FilterChannelId> FilterWordsChannelIds { get; set; } = new HashSet<FilterChannelId>();
public HashSet<MutedUserId> MutedUsers { get; set; } = new HashSet<MutedUserId>();
public string MuteRoleName { get; set; }
public bool CleverbotEnabled { get; set; }
public HashSet<GuildRepeater> GuildRepeaters { get; set; } = new HashSet<GuildRepeater>();
public AntiRaidSetting AntiRaidSetting { get; set; }
public AntiSpamSetting AntiSpamSetting { get; set; }
public string Locale { get; set; } = null;
public string TimeZoneId { get; set; } = null;
public HashSet<UnmuteTimer> UnmuteTimers { get; set; } = new HashSet<UnmuteTimer>();
public HashSet<VcRoleInfo> VcRoleInfos { get; set; }
public HashSet<CommandAlias> CommandAliases { get; set; } = new HashSet<CommandAlias>();
public List<WarningPunishment> WarnPunishments { get; set; } = new List<WarningPunishment>();
public bool WarningsInitialized { get; set; }
public HashSet<SlowmodeIgnoredUser> SlowmodeIgnoredUsers { get; set; }
public HashSet<SlowmodeIgnoredRole> SlowmodeIgnoredRoles { get; set; }
public HashSet<NsfwBlacklitedTag> NsfwBlacklistedTags { get; set; } = new HashSet<NsfwBlacklitedTag>();
public List<ShopEntry> ShopEntries { get; set; }
public ulong? GameVoiceChannel { get; set; } = null;
public bool VerboseErrors { get; set; } = false;
public StreamRoleSettings StreamRole { get; set; }
public XpSettings XpSettings { get; set; }
public List<FeedSub> FeedSubs { get; set; } = new List<FeedSub>();
//public List<ProtectionIgnoredChannel> ProtectionIgnoredChannels { get; set; } = new List<ProtectionIgnoredChannel>();
}
public class NsfwBlacklitedTag : DbEntity
{
public string Tag { get; set; }
public override int GetHashCode()
{
return Tag.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is NsfwBlacklitedTag x
? x.Tag == Tag
: false;
}
}
public class SlowmodeIgnoredUser : DbEntity
{
public ulong UserId { get; set; }
// override object.Equals
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return ((SlowmodeIgnoredUser)obj).UserId == UserId;
}
// override object.GetHashCode
public override int GetHashCode()
{
return UserId.GetHashCode();
}
}
public class SlowmodeIgnoredRole : DbEntity
{
public ulong RoleId { get; set; }
// override object.Equals
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return ((SlowmodeIgnoredRole)obj).RoleId == RoleId;
}
// override object.GetHashCode
public override int GetHashCode()
{
return RoleId.GetHashCode();
}
}
public class WarningPunishment : DbEntity
{
public int Count { get; set; }
public PunishmentAction Punishment { get; set; }
public int Time { get; set; }
}
public class CommandAlias : DbEntity
{
public string Trigger { get; set; }
public string Mapping { get; set; }
//// override object.Equals
//public override bool Equals(object obj)
//{
// if (obj == null || GetType() != obj.GetType())
// {
// return false;
// }
// return ((CommandAlias)obj).Trigger.Trim().ToLowerInvariant() == Trigger.Trim().ToLowerInvariant();
//}
//// override object.GetHashCode
//public override int GetHashCode()
//{
// return Trigger.Trim().ToLowerInvariant().GetHashCode();
//}
}
public class VcRoleInfo : DbEntity
{
public ulong VoiceChannelId { get; set; }
public ulong RoleId { get; set; }
}
public class UnmuteTimer : DbEntity
{
public ulong UserId { get; set; }
public DateTime UnmuteAt { get; set; }
public override int GetHashCode() =>
UserId.GetHashCode();
public override bool Equals(object obj)
{
var ut = obj as UnmuteTimer;
if (ut == null)
return false;
return ut.UserId == UserId;
}
}
public class FilterChannelId : DbEntity
{
public ulong ChannelId { get; set; }
}
public class FilteredWord : DbEntity
{
public string Word { get; set; }
}
public class MutedUserId : DbEntity
{
public ulong UserId { get; set; }
public override int GetHashCode()
{
return UserId.GetHashCode();
}
public override bool Equals(object obj)
{
var mui = obj as MutedUserId;
if (mui == null)
return false;
return mui.UserId == this.UserId;
}
}
public class GCChannelId : DbEntity
{
public ulong ChannelId { get; set; }
public override bool Equals(object obj)
{
var gc = obj as GCChannelId;
if (gc == null)
return false;
return gc.ChannelId == this.ChannelId;
}
public override int GetHashCode() =>
this.ChannelId.GetHashCode();
}
}

View File

@ -0,0 +1,8 @@
namespace NadekoBot.Services.Database.Models
{
public class IgnoredLogChannel : DbEntity
{
public LogSetting LogSetting { get; set; }
public ulong ChannelId { get; set; }
}
}

View File

@ -0,0 +1,105 @@
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Models
{
public class LogSetting : DbEntity
{
public HashSet<IgnoredLogChannel> IgnoredChannels { get; set; } = new HashSet<IgnoredLogChannel>();
public HashSet<IgnoredVoicePresenceChannel> IgnoredVoicePresenceChannelIds { get; set; } = new HashSet<IgnoredVoicePresenceChannel>();
public ulong? LogOtherId { get; set; } = null;
public ulong? MessageUpdatedId { get; set; } = null;
public ulong? MessageDeletedId { get; set; } = null;
public ulong? UserJoinedId { get; set; } = null;
public ulong? UserLeftId { get; set; } = null;
public ulong? UserBannedId { get; set; } = null;
public ulong? UserUnbannedId { get; set; } = null;
public ulong? UserUpdatedId { get; set; } = null;
public ulong? ChannelCreatedId { get; set; } = null;
public ulong? ChannelDestroyedId { get; set; } = null;
public ulong? ChannelUpdatedId { get; set; } = null;
public ulong? UserMutedId { get; set; }
//userpresence
public ulong? LogUserPresenceId { get; set; } = null;
//voicepresence
public ulong? LogVoicePresenceId { get; set; } = null;
public ulong? LogVoicePresenceTTSId { get; set; } = null;
//-------------------DO NOT USE----------------
// these old fields are here because sqlite doesn't support drop column operation
// will be removed after bot moves to another database provider
/// <summary>
/// DON'T USE
/// </summary>
public bool IsLogging { get; set; }
/// <summary>
/// DON'T USE
/// </summary>
public ulong ChannelId { get; set; }
/// <summary>
/// DON'T USE
/// </summary>
public bool MessageUpdated { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool MessageDeleted { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool UserJoined { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool UserLeft { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool UserBanned { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool UserUnbanned { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool UserUpdated { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool ChannelCreated { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool ChannelDestroyed { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool ChannelUpdated { get; set; } = true;
/// <summary>
/// DON'T USE
/// </summary>
public bool LogUserPresence { get; set; } = false;
/// <summary>
/// DON'T USE
/// </summary>
public ulong UserPresenceChannelId { get; set; }
/// <summary>
/// DON'T USE
/// </summary>
public bool LogVoicePresence { get; set; } = false;
/// <summary>
/// DON'T USE
/// </summary>
public ulong VoicePresenceChannelId { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Models
{
public class MusicPlaylist : DbEntity
{
public string Name { get; set; }
public string Author { get; set; }
public ulong AuthorId { get; set; }
public List<PlaylistSong> Songs { get; set; } = new List<PlaylistSong>();
}
}

View File

@ -0,0 +1,130 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
namespace NadekoBot.Services.Database.Models
{
[DebuggerDisplay("{global::NadekoBot.Modules.Permissions.PermissionExtensions.GetCommand(this)}", Target = typeof(Permission))]
public class Permission : DbEntity
{
public Permission Previous { get; set; } = null;
public Permission Next { get; set; } = null;
public PrimaryPermissionType PrimaryTarget { get; set; }
public ulong PrimaryTargetId { get; set; }
public SecondaryPermissionType SecondaryTarget { get; set; }
public string SecondaryTargetName { get; set; }
public bool State { get; set; }
public Permissionv2 Tov2() =>
new Permissionv2()
{
PrimaryTarget = PrimaryTarget,
PrimaryTargetId = PrimaryTargetId,
SecondaryTarget = SecondaryTarget,
SecondaryTargetName = SecondaryTargetName,
State = State,
};
//[NotMapped]
//private static Permission AllowAllPerm => new Permission()
//{
// PrimaryTarget = PrimaryPermissionType.Server,
// PrimaryTargetId = 0,
// SecondaryTarget = SecondaryPermissionType.AllModules,
// SecondaryTargetName = "*",
// State = true,
//};
//[NotMapped]
//private static Permission BlockNsfwPerm => new Permission()
//{
// PrimaryTarget = PrimaryPermissionType.Server,
// PrimaryTargetId = 0,
// SecondaryTarget = SecondaryPermissionType.Module,
// SecondaryTargetName = "nsfw",
// State = false,
//};
//public Permission Clone() => new Permission()
//{
// PrimaryTarget = PrimaryTarget,
// SecondaryTarget = SecondaryTarget,
// PrimaryTargetId = PrimaryTargetId,
// SecondaryTargetName = SecondaryTargetName,
// State = State,
//};
}
public interface IIndexed
{
int Index { get; set; }
}
[DebuggerDisplay("{PrimaryTarget}{SecondaryTarget} {SecondaryTargetName} {State} {PrimaryTargetId}")]
public class Permissionv2 : DbEntity, IIndexed
{
public int? GuildConfigId { get; set; }
public int Index { get; set; }
public PrimaryPermissionType PrimaryTarget { get; set; }
public ulong PrimaryTargetId { get; set; }
public SecondaryPermissionType SecondaryTarget { get; set; }
public string SecondaryTargetName { get; set; }
public bool State { get; set; }
[NotMapped]
public static Permissionv2 AllowAllPerm => new Permissionv2()
{
PrimaryTarget = PrimaryPermissionType.Server,
PrimaryTargetId = 0,
SecondaryTarget = SecondaryPermissionType.AllModules,
SecondaryTargetName = "*",
State = true,
Index = 0,
};
[NotMapped]
private static Permissionv2 BlockNsfwPerm => new Permissionv2()
{
PrimaryTarget = PrimaryPermissionType.Server,
PrimaryTargetId = 0,
SecondaryTarget = SecondaryPermissionType.Module,
SecondaryTargetName = "nsfw",
State = false,
Index = 1
};
public static List<Permissionv2> GetDefaultPermlist =>
new List<Permissionv2>
{
BlockNsfwPerm,
AllowAllPerm
};
//public Permission Clone() => new Permission()
//{
// PrimaryTarget = PrimaryTarget,
// SecondaryTarget = SecondaryTarget,
// PrimaryTargetId = PrimaryTargetId,
// SecondaryTargetName = SecondaryTargetName,
// State = State,
//};
}
public enum PrimaryPermissionType
{
User,
Channel,
Role,
Server
}
public enum SecondaryPermissionType
{
Module,
Command,
AllModules
}
}

View File

@ -0,0 +1,19 @@
namespace NadekoBot.Services.Database.Models
{
public class PlaylistSong : DbEntity
{
public string Provider { get; set; }
public MusicType ProviderType { get; set; }
public string Title { get; set; }
public string Uri { get; set; }
public string Query { get; set; }
}
public enum MusicType
{
Radio,
YouTube,
Local,
Soundcloud
}
}

View File

@ -0,0 +1,8 @@
namespace NadekoBot.Services.Database.Models
{
public class UserPokeTypes : DbEntity
{
public ulong UserId { get; set; }
public string type { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
namespace NadekoBot.Services.Database.Models
{
public class Quote : DbEntity
{
public ulong GuildId { get; set; }
[Required]
public string Keyword { get; set; }
[Required]
public string AuthorName { get; set; }
public ulong AuthorId { get; set; }
[Required]
public string Text { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
namespace NadekoBot.Services.Database.Models
{
public class Reminder : DbEntity
{
public DateTime When { get; set; }
public ulong ChannelId { get; set; }
public ulong ServerId { get; set; }
public ulong UserId { get; set; }
public string Message { get; set; }
public bool IsPrivate { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System;
namespace NadekoBot.Services.Database.Models
{
public class Repeater : DbEntity
{
public ulong GuildId { get; set; }
public ulong ChannelId { get; set; }
public string Message { get; set; }
public TimeSpan Interval { get; set; }
public TimeSpan? StartTimeOfDay { get; set; }
}
public class GuildRepeater : Repeater
{
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace NadekoBot.Services.Database.Models
{
public class RewardedUser : DbEntity
{
public ulong UserId { get; set; }
public string PatreonUserId { get; set; }
public int AmountRewardedThisMonth { get; set; }
public DateTime LastReward { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace NadekoBot.Services.Database.Models
{
public class SelfAssignedRole : DbEntity
{
public ulong GuildId { get; set; }
public ulong RoleId { get; set; }
}
}

View File

@ -0,0 +1,45 @@
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Models
{
public enum ShopEntryType
{
Role,
List,
//Infinite_List,
}
public class ShopEntry : DbEntity, IIndexed
{
public int Index { get; set; }
public int Price { get; set; }
public string Name { get; set; }
public ulong AuthorId { get; set; }
public ShopEntryType Type { get; set; }
//role
public string RoleName { get; set; }
public ulong RoleId { get; set; }
//list
public HashSet<ShopEntryItem> Items { get; set; } = new HashSet<ShopEntryItem>();
}
public class ShopEntryItem : DbEntity
{
public string Text { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return ((ShopEntryItem)obj).Text == Text;
}
public override int GetHashCode() =>
Text.GetHashCode();
}
}

View File

@ -0,0 +1,83 @@
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Models
{
public class StreamRoleSettings : DbEntity
{
public int GuildConfigId { get; set; }
public GuildConfig GuildConfig { get; set; }
/// <summary>
/// Whether the feature is enabled in the guild.
/// </summary>
public bool Enabled { get; set; }
/// <summary>
/// Id of the role to give to the users in the role 'FromRole' when they start streaming
/// </summary>
public ulong AddRoleId { get; set; }
/// <summary>
/// Id of the role whose users are eligible to get the 'AddRole'
/// </summary>
public ulong FromRoleId { get; set; }
/// <summary>
/// If set, feature will only apply to users who have this keyword in their streaming status.
/// </summary>
public string Keyword { get; set; }
/// <summary>
/// A collection of whitelisted users' IDs. Whitelisted users don't require 'keyword' in
/// order to get the stream role.
/// </summary>
public HashSet<StreamRoleWhitelistedUser> Whitelist { get; set; } = new HashSet<StreamRoleWhitelistedUser>();
/// <summary>
/// A collection of blacklisted users' IDs. Blacklisted useres will never get the stream role.
/// </summary>
public HashSet<StreamRoleBlacklistedUser> Blacklist { get; set; } = new HashSet<StreamRoleBlacklistedUser>();
}
public class StreamRoleBlacklistedUser : DbEntity
{
public ulong UserId { get; set; }
public string Username { get; set; }
public override bool Equals(object obj)
{
var x = obj as StreamRoleBlacklistedUser;
if (x == null)
return false;
return x.UserId == UserId;
}
public override int GetHashCode()
{
return UserId.GetHashCode();
}
}
public class StreamRoleWhitelistedUser : DbEntity
{
public ulong UserId { get; set; }
public string Username { get; set; }
public override bool Equals(object obj)
{
var x = obj as StreamRoleWhitelistedUser;
if (x == null)
return false;
return x.UserId == UserId;
}
public override int GetHashCode()
{
return UserId.GetHashCode();
}
}
}

View File

@ -0,0 +1,16 @@
using System;
namespace NadekoBot.Services.Database.Models
{
public class UserXpStats : DbEntity
{
public ulong UserId { get; set; }
public ulong GuildId { get; set; }
public int Xp { get; set; }
public int AwardedXp { get; set; }
public XpNotificationType NotifyOnLevelUp { get; set; }
public DateTime LastLevelUp { get; set; } = DateTime.UtcNow;
}
public enum XpNotificationType { None, Dm, Channel }
}

View File

@ -0,0 +1,8 @@
namespace NadekoBot.Services.Database.Models
{
public class IgnoredVoicePresenceChannel : DbEntity
{
public LogSetting LogSetting { get; set; }
public ulong ChannelId { get; set; }
}
}

View File

@ -0,0 +1,46 @@
using NadekoBot.Extensions;
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Models
{
public class WaifuInfo : DbEntity
{
public int WaifuId { get; set; }
public DiscordUser Waifu { get; set; }
public int? ClaimerId { get; set; }
public DiscordUser Claimer { get; set; }
public int? AffinityId { get; set; }
public DiscordUser Affinity { get; set; }
public int Price { get; set; }
public List<WaifuItem> Items { get; set; } = new List<WaifuItem>();
public override string ToString()
{
var claimer = "no one";
var status = "";
var waifuUsername = Waifu.Username.TrimTo(20);
var claimerUsername = Claimer?.Username.TrimTo(20);
if (Claimer != null)
{
claimer = $"{ claimerUsername }#{Claimer.Discriminator}";
}
if (AffinityId == null)
{
status = $"... but {waifuUsername}'s heart is empty";
}
else if (AffinityId == ClaimerId)
{
status = $"... and {waifuUsername} likes {claimerUsername} too <3";
}
else {
status = $"... but {waifuUsername}'s heart belongs to {Affinity.Username.TrimTo(20)}#{Affinity.Discriminator}";
}
return $"**{waifuUsername}#{Waifu.Discriminator}** - claimed by **{claimer}**\n\t{status}";
}
}
}

View File

@ -0,0 +1,87 @@
using System;
namespace NadekoBot.Services.Database.Models
{
public class WaifuItem : DbEntity
{
public string ItemEmoji { get; set; }
public int Price { get; set; }
public ItemName Item { get; set; }
public enum ItemName
{
Cookie,
Rose,
LoveLetter,
Chocolate,
Rice,
MovieTicket,
Book,
Lipstick,
Laptop,
Violin,
Ring,
Helicopter,
}
public WaifuItem()
{
}
public WaifuItem(string itemEmoji, int price, ItemName item)
{
ItemEmoji = itemEmoji;
Price = price;
Item = item;
}
public static WaifuItem GetItem(ItemName itemName)
{
switch (itemName)
{
case ItemName.Cookie:
return new WaifuItem("🍪", 10, itemName);
case ItemName.Rose:
return new WaifuItem("🌹", 50, itemName);
case ItemName.LoveLetter:
return new WaifuItem("💌", 100, itemName);
case ItemName.Chocolate:
return new WaifuItem("🍫", 200, itemName);
case ItemName.Rice:
return new WaifuItem("🍚", 400, itemName);
case ItemName.MovieTicket:
return new WaifuItem("🎟", 800, itemName);
case ItemName.Book:
return new WaifuItem("📔", 1500, itemName);
case ItemName.Lipstick:
return new WaifuItem("💄", 3000, itemName);
case ItemName.Laptop:
return new WaifuItem("💻", 5000, itemName);
case ItemName.Violin:
return new WaifuItem("🎻", 7500, itemName);
case ItemName.Ring:
return new WaifuItem("💍", 10000, itemName);
case ItemName.Helicopter:
return new WaifuItem("🚁", 20000, itemName);
default:
throw new ArgumentException(nameof(itemName));
}
}
}
}
/*
🍪 Cookie 10
🌹 Rose 50
💌 Love Letter 100
🍫 Chocolate 200
🍚 Rice 400
🎟 Movie Ticket 800
📔 Book 1.5k
💄 Lipstick 3k
💻 Laptop 5k
🎻 Violin 7.5k
💍 Ring 10k
*/

View File

@ -0,0 +1,21 @@
namespace NadekoBot.Services.Database.Models
{
public class WaifuUpdate : DbEntity
{
public int UserId { get; set; }
public DiscordUser User { get; set; }
public WaifuUpdateType UpdateType { get; set; }
public int? OldId { get; set; }
public DiscordUser Old { get; set; }
public int? NewId { get; set; }
public DiscordUser New { get; set; }
}
public enum WaifuUpdateType
{
AffinityChanged,
Claimed
}
}

View File

@ -0,0 +1,12 @@
namespace NadekoBot.Services.Database.Models
{
public class Warning : DbEntity
{
public ulong GuildId { get; set; }
public ulong UserId { get; set; }
public string Reason { get; set; }
public bool Forgiven { get; set; }
public string ForgivenBy { get; set; }
public string Moderator { get; set; }
}
}

View File

@ -0,0 +1,53 @@
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Models
{
public class XpSettings : DbEntity
{
public int GuildConfigId { get; set; }
public GuildConfig GuildConfig { get; set; }
public HashSet<XpRoleReward> RoleRewards { get; set; } = new HashSet<XpRoleReward>();
public bool XpRoleRewardExclusive { get; set; }
public string NotifyMessage { get; set; } = "Congratulations {0}! You have reached level {1}!";
public HashSet<ExcludedItem> ExclusionList { get; set; } = new HashSet<ExcludedItem>();
public bool ServerExcluded { get; set; }
}
public enum ExcludedItemType { Channel, Role }
public class XpRoleReward : DbEntity
{
public int XpSettingsId { get; set; }
public XpSettings XpSettings { get; set; }
public int Level { get; set; }
public ulong RoleId { get; set; }
public override int GetHashCode()
{
return Level.GetHashCode() ^ XpSettingsId.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is XpRoleReward xrr && xrr.Level == Level && xrr.XpSettingsId == XpSettingsId;
}
}
public class ExcludedItem : DbEntity
{
public ulong ItemId { get; set; }
public ExcludedItemType ItemType { get; set; }
public override int GetHashCode()
{
return ItemId.GetHashCode() ^ ItemType.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is ExcludedItem ei && ei.ItemId == ItemId && ei.ItemType == ItemType;
}
}
}

View File

@ -0,0 +1,355 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using NadekoBot.Services.Database.Models;
using NadekoBot.Extensions;
using System;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Data.Sqlite;
using System.IO;
namespace NadekoBot.Services.Database
{
public class NadekoContextFactory : IDesignTimeDbContextFactory<NadekoContext>
{
public NadekoContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<NadekoContext>();
var builder = new SqliteConnectionStringBuilder("Data Source=data/NadekoBot.db");
builder.DataSource = Path.Combine(AppContext.BaseDirectory, builder.DataSource);
optionsBuilder.UseSqlite(builder.ToString());
var ctx = new NadekoContext(optionsBuilder.Options);
ctx.Database.SetCommandTimeout(60);
return ctx;
}
}
public class NadekoContext : DbContext
{
public DbSet<Quote> Quotes { get; set; }
public DbSet<Donator> Donators { get; set; }
public DbSet<GuildConfig> GuildConfigs { get; set; }
public DbSet<ClashWar> ClashOfClans { get; set; }
public DbSet<ClashCaller> ClashCallers { get; set; }
public DbSet<Reminder> Reminders { get; set; }
public DbSet<SelfAssignedRole> SelfAssignableRoles { get; set; }
public DbSet<BotConfig> BotConfig { get; set; }
public DbSet<Currency> Currency { get; set; }
public DbSet<ConvertUnit> ConversionUnits { get; set; }
public DbSet<MusicPlaylist> MusicPlaylists { get; set; }
public DbSet<CustomReaction> CustomReactions { get; set; }
public DbSet<CurrencyTransaction> CurrencyTransactions { get; set; }
public DbSet<UserPokeTypes> PokeGame { get; set; }
public DbSet<WaifuUpdate> WaifuUpdates { get; set; }
public DbSet<Warning> Warnings { get; set; }
public DbSet<UserXpStats> UserXpStats { get; set; }
public DbSet<ClubInfo> Clubs { get; set; }
//logging
public DbSet<LogSetting> LogSettings { get; set; }
public DbSet<IgnoredLogChannel> IgnoredLogChannels { get; set; }
public DbSet<IgnoredVoicePresenceChannel> IgnoredVoicePresenceCHannels { get; set; }
//orphans xD
public DbSet<EightBallResponse> EightBallResponses { get; set; }
public DbSet<RaceAnimal> RaceAnimals { get; set; }
public DbSet<ModulePrefix> ModulePrefixes { get; set; }
public DbSet<RewardedUser> RewardedUsers { get; set; }
public NadekoContext(DbContextOptions<NadekoContext> options) : base(options)
{
}
public void EnsureSeedData()
{
if (!BotConfig.Any())
{
var bc = new BotConfig();
bc.RaceAnimals.AddRange(new HashSet<RaceAnimal>
{
new RaceAnimal { Icon = "🐼", Name = "Panda" },
new RaceAnimal { Icon = "🐻", Name = "Bear" },
new RaceAnimal { Icon = "🐧", Name = "Pengu" },
new RaceAnimal { Icon = "🐨", Name = "Koala" },
new RaceAnimal { Icon = "🐬", Name = "Dolphin" },
new RaceAnimal { Icon = "🐞", Name = "Ladybird" },
new RaceAnimal { Icon = "🦀", Name = "Crab" },
new RaceAnimal { Icon = "🦄", Name = "Unicorn" }
});
bc.EightBallResponses.AddRange(new HashSet<EightBallResponse>
{
new EightBallResponse() { Text = "Most definitely yes" },
new EightBallResponse() { Text = "For sure" },
new EightBallResponse() { Text = "Totally!" },
new EightBallResponse() { Text = "Of course!" },
new EightBallResponse() { Text = "As I see it, yes" },
new EightBallResponse() { Text = "My sources say yes" },
new EightBallResponse() { Text = "Yes" },
new EightBallResponse() { Text = "Most likely" },
new EightBallResponse() { Text = "Perhaps" },
new EightBallResponse() { Text = "Maybe" },
new EightBallResponse() { Text = "Not sure" },
new EightBallResponse() { Text = "It is uncertain" },
new EightBallResponse() { Text = "Ask me again later" },
new EightBallResponse() { Text = "Don't count on it" },
new EightBallResponse() { Text = "Probably not" },
new EightBallResponse() { Text = "Very doubtful" },
new EightBallResponse() { Text = "Most likely no" },
new EightBallResponse() { Text = "Nope" },
new EightBallResponse() { Text = "No" },
new EightBallResponse() { Text = "My sources say no" },
new EightBallResponse() { Text = "Dont even think about it" },
new EightBallResponse() { Text = "Definitely no" },
new EightBallResponse() { Text = "NO - It may cause disease contraction" }
});
BotConfig.Add(bc);
this.SaveChanges();
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region QUOTES
//var quoteEntity = modelBuilder.Entity<Quote>();
#endregion
#region Donators
var donatorEntity = modelBuilder.Entity<Donator>();
donatorEntity
.HasIndex(d => d.UserId)
.IsUnique();
#endregion
#region GuildConfig
var configEntity = modelBuilder.Entity<GuildConfig>();
configEntity
.HasIndex(c => c.GuildId)
.IsUnique();
modelBuilder.Entity<AntiSpamSetting>()
.HasOne(x => x.GuildConfig)
.WithOne(x => x.AntiSpamSetting);
modelBuilder.Entity<AntiRaidSetting>()
.HasOne(x => x.GuildConfig)
.WithOne(x => x.AntiRaidSetting);
modelBuilder.Entity<FeedSub>()
.HasAlternateKey(x => new { x.GuildConfigId, x.Url });
//modelBuilder.Entity<ProtectionIgnoredChannel>()
// .HasAlternateKey(c => new { c.ChannelId, c.ProtectionType });
#endregion
#region streamrole
modelBuilder.Entity<StreamRoleSettings>()
.HasOne(x => x.GuildConfig)
.WithOne(x => x.StreamRole);
#endregion
#region BotConfig
var botConfigEntity = modelBuilder.Entity<BotConfig>();
botConfigEntity.Property(x => x.XpMinutesTimeout)
.HasDefaultValue(5);
botConfigEntity.Property(x => x.XpPerMessage)
.HasDefaultValue(3);
//botConfigEntity
// .HasMany(c => c.ModulePrefixes)
// .WithOne(mp => mp.BotConfig)
// .HasForeignKey(mp => mp.BotConfigId);
#endregion
#region ClashOfClans
var callersEntity = modelBuilder.Entity<ClashCaller>();
callersEntity
.HasOne(c => c.ClashWar)
.WithMany(c => c.Bases);
#endregion
#region Self Assignable Roles
var selfassignableRolesEntity = modelBuilder.Entity<SelfAssignedRole>();
selfassignableRolesEntity
.HasIndex(s => new { s.GuildId, s.RoleId })
.IsUnique();
#endregion
#region Currency
var currencyEntity = modelBuilder.Entity<Currency>();
currencyEntity
.HasIndex(c => c.UserId)
.IsUnique();
#endregion
#region Permission
var permissionEntity = modelBuilder.Entity<Permission>();
permissionEntity
.HasOne(p => p.Next)
.WithOne(p => p.Previous)
.IsRequired(false);
#endregion
#region LogSettings
//var logSettingEntity = modelBuilder.Entity<LogSetting>();
//logSettingEntity
// .HasMany(ls => ls.IgnoredChannels)
// .WithOne(ls => ls.LogSetting)
// .HasPrincipalKey(ls => ls.id;
//logSettingEntity
// .HasMany(ls => ls.IgnoredVoicePresenceChannelIds)
// .WithOne(ls => ls.LogSetting);
#endregion
#region MusicPlaylists
var musicPlaylistEntity = modelBuilder.Entity<MusicPlaylist>();
musicPlaylistEntity
.HasMany(p => p.Songs)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
#endregion
#region PokeGame
var pokeGameEntity = modelBuilder.Entity<UserPokeTypes>();
pokeGameEntity
.HasIndex(pt => pt.UserId)
.IsUnique();
#endregion
#region CommandPrice
//well, i failed
modelBuilder.Entity<CommandPrice>()
.HasIndex(cp => cp.Price)
.IsUnique();
//modelBuilder.Entity<CommandCost>()
// .HasIndex(cp => cp.CommandName)
// .IsUnique();
#endregion
#region Waifus
var wi = modelBuilder.Entity<WaifuInfo>();
wi.HasOne(x => x.Waifu)
.WithOne();
// //.HasForeignKey<WaifuInfo>(w => w.WaifuId)
// //.IsRequired(true);
//wi.HasOne(x => x.Claimer)
// .WithOne();
// //.HasForeignKey<WaifuInfo>(w => w.ClaimerId)
// //.IsRequired(false);
#endregion
#region DiscordUser
var du = modelBuilder.Entity<DiscordUser>();
du.HasAlternateKey(w => w.UserId);
du.HasOne(x => x.Club)
.WithMany(x => x.Users)
.IsRequired(false);
modelBuilder.Entity<DiscordUser>()
.Property(x => x.LastLevelUp)
.HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 305, DateTimeKind.Local));
#endregion
#region Warnings
var warn = modelBuilder.Entity<Warning>();
#endregion
#region PatreonRewards
var pr = modelBuilder.Entity<RewardedUser>();
pr.HasIndex(x => x.UserId)
.IsUnique();
#endregion
#region XpStats
modelBuilder.Entity<UserXpStats>()
.HasIndex(x => new { x.UserId, x.GuildId })
.IsUnique();
modelBuilder.Entity<UserXpStats>()
.Property(x => x.LastLevelUp)
.HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 307, DateTimeKind.Local));
#endregion
#region XpSettings
modelBuilder.Entity<XpSettings>()
.HasOne(x => x.GuildConfig)
.WithOne(x => x.XpSettings);
#endregion
//todo major bug
#region XpRoleReward
modelBuilder.Entity<XpRoleReward>()
.HasIndex(x => new { x.XpSettingsId, x.Level })
.IsUnique();
#endregion
#region Club
var ci = modelBuilder.Entity<ClubInfo>();
ci.HasOne(x => x.Owner)
.WithOne()
.HasForeignKey<ClubInfo>(x => x.OwnerId);
ci.HasAlternateKey(x => new { x.Name, x.Discrim });
#endregion
#region ClubManytoMany
modelBuilder.Entity<ClubApplicants>()
.HasKey(t => new { t.ClubId, t.UserId });
modelBuilder.Entity<ClubApplicants>()
.HasOne(pt => pt.User)
.WithMany();
modelBuilder.Entity<ClubApplicants>()
.HasOne(pt => pt.Club)
.WithMany(x => x.Applicants);
modelBuilder.Entity<ClubBans>()
.HasKey(t => new { t.ClubId, t.UserId });
modelBuilder.Entity<ClubBans>()
.HasOne(pt => pt.User)
.WithMany();
modelBuilder.Entity<ClubBans>()
.HasOne(pt => pt.Club)
.WithMany(x => x.Bans);
#endregion
}
}
}

View File

@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
using System;
using System.Linq;
namespace NadekoBot.Services.Database.Repositories
{
public interface IBotConfigRepository : IRepository<BotConfig>
{
BotConfig GetOrCreate(Func<DbSet<BotConfig>, IQueryable<BotConfig>> includes = null);
}
}

View File

@ -0,0 +1,10 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Repositories
{
public interface IClashOfClansRepository : IRepository<ClashWar>
{
IEnumerable<ClashWar> GetAllWars(List<long> guilds);
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
using System;
using System.Linq;
namespace NadekoBot.Services.Database.Repositories
{
public interface IClubRepository : IRepository<ClubInfo>
{
int GetNextDiscrim(string clubName);
ClubInfo GetByName(string v, int discrim, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> func = null);
ClubInfo GetByOwner(ulong userId, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> func = null);
ClubInfo GetByOwnerOrAdmin(ulong userId);
ClubInfo GetByMember(ulong userId, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> func = null);
ClubInfo[] GetClubLeaderboardPage(int page);
}
}

View File

@ -0,0 +1,13 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Repositories
{
public interface ICurrencyRepository : IRepository<Currency>
{
Currency GetOrCreate(ulong userId);
long GetUserCurrency(ulong userId);
bool TryUpdateState(ulong userId, long change);
IEnumerable<Currency> GetTopRichest(int count, int skip);
}
}

View File

@ -0,0 +1,8 @@
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Database.Repositories
{
public interface ICurrencyTransactionsRepository : IRepository<CurrencyTransaction>
{
}
}

View File

@ -0,0 +1,9 @@
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Database.Repositories
{
public interface ICustomReactionRepository : IRepository<CustomReaction>
{
}
}

View File

@ -0,0 +1,12 @@
using Discord;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Database.Repositories
{
public interface IDiscordUserRepository : IRepository<DiscordUser>
{
DiscordUser GetOrCreate(IUser original);
int GetUserGlobalRanking(ulong id);
DiscordUser[] GetUsersXpLeaderboardFor(int page);
}
}

View File

@ -0,0 +1,11 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Repositories
{
public interface IDonatorsRepository : IRepository<Donator>
{
IEnumerable<Donator> GetDonatorsOrdered();
Donator AddOrUpdateDonator(ulong userId, string name, int amount);
}
}

View File

@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NadekoBot.Services.Database.Repositories
{
public interface IGuildConfigRepository : IRepository<GuildConfig>
{
GuildConfig For(ulong guildId, Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes = null);
GuildConfig LogSettingsFor(ulong guildId);
IEnumerable<GuildConfig> OldPermissionsForAll();
IEnumerable<GuildConfig> GetAllGuildConfigs(List<long> availableGuilds);
IEnumerable<FollowedStream> GetAllFollowedStreams(List<long> included);
void SetCleverbotEnabled(ulong id, bool cleverbotEnabled);
IEnumerable<GuildConfig> Permissionsv2ForAll(List<long> include);
GuildConfig GcWithPermissionsv2For(ulong guildId);
XpSettings XpSettingsFor(ulong guildId);
}
}

View File

@ -0,0 +1,11 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Repositories
{
public interface IMusicPlaylistRepository : IRepository<MusicPlaylist>
{
List<MusicPlaylist> GetPlaylistsOnPage(int num);
MusicPlaylist GetWithSongs(int id);
}
}

View File

@ -0,0 +1,9 @@
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Database.Repositories
{
public interface IPokeGameRepository : IRepository<UserPokeTypes>
{
//List<UserPokeTypes> GetAllPokeTypes();
}
}

View File

@ -0,0 +1,15 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Repositories
{
public interface IQuoteRepository : IRepository<Quote>
{
IEnumerable<Quote> GetAllQuotesByKeyword(ulong guildId, string keyword);
Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string keyword);
Task<Quote> SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text);
IEnumerable<Quote> GetGroup(ulong guildId, int skip, int take);
void RemoveAllByKeyword(ulong guildId, string keyword);
}
}

View File

@ -0,0 +1,10 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Repositories
{
public interface IReminderRepository : IRepository<Reminder>
{
IEnumerable<Reminder> GetIncludedReminders(IEnumerable<long> guildIds);
}
}

View File

@ -0,0 +1,21 @@
using System.Collections.Generic;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Database.Repositories
{
public interface IRepository<T> where T : DbEntity
{
T Get(int id);
IEnumerable<T> GetAll();
void Add(T obj);
void AddRange(params T[] objs);
void Remove(int id);
void Remove(T obj);
void RemoveRange(params T[] objs);
void Update(T obj);
void UpdateRange(params T[] objs);
}
}

View File

@ -0,0 +1,11 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Repositories
{
public interface ISelfAssignedRolesRepository : IRepository<SelfAssignedRole>
{
bool DeleteByGuildAndRoleId(ulong guildId, ulong roleId);
IEnumerable<SelfAssignedRole> GetFromGuild(ulong guildId);
}
}

View File

@ -0,0 +1,11 @@
using NadekoBot.Services.Database.Models;
using System;
namespace NadekoBot.Services.Database.Repositories
{
public interface IUnitConverterRepository : IRepository<ConvertUnit>
{
void AddOrUpdate(Func<ConvertUnit, bool> check, ConvertUnit toAdd, Func<ConvertUnit, ConvertUnit> toUpdate);
bool Empty();
}
}

View File

@ -0,0 +1,12 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
namespace NadekoBot.Services.Database.Repositories
{
public interface IWaifuRepository : IRepository<WaifuInfo>
{
IList<WaifuInfo> GetTop(int count, int skip = 0);
WaifuInfo ByWaifuUserId(ulong userId);
IList<WaifuInfo> ByClaimerUserId(ulong userId);
}
}

View File

@ -0,0 +1,12 @@
using NadekoBot.Services.Database.Models;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Repositories
{
public interface IWarningsRepository : IRepository<Warning>
{
Warning[] For(ulong guildId, ulong userId);
Task ForgiveAll(ulong guildId, ulong userId, string mod);
Warning[] GetForGuild(ulong id);
}
}

View File

@ -0,0 +1,11 @@
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Database.Repositories
{
public interface IXpRepository : IRepository<UserXpStats>
{
UserXpStats GetOrCreateUser(ulong guildId, ulong userId);
int GetUserGuildRanking(ulong userId, ulong guildId);
UserXpStats[] GetUsersFor(ulong guildId, int page);
}
}

View File

@ -0,0 +1,40 @@
using NadekoBot.Services.Database.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class BotConfigRepository : Repository<BotConfig>, IBotConfigRepository
{
public BotConfigRepository(DbContext context) : base(context)
{
}
public BotConfig GetOrCreate(Func<DbSet<BotConfig>, IQueryable<BotConfig>> includes = null)
{
BotConfig config;
if (includes == null)
config = _set.Include(bc => bc.RotatingStatusMessages)
.Include(bc => bc.RaceAnimals)
.Include(bc => bc.Blacklist)
.Include(bc => bc.EightBallResponses)
.Include(bc => bc.StartupCommands)
.Include(bc => bc.BlockedCommands)
.Include(bc => bc.BlockedModules)
.Include(bc => bc.Blacklist)
//.Include(bc => bc.CommandCosts)
.FirstOrDefault();
else
config = includes(_set).FirstOrDefault();
if (config == null)
{
_set.Add(config = new BotConfig());
_context.SaveChanges();
}
return config;
}
}
}

View File

@ -0,0 +1,24 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class ClashOfClansRepository : Repository<ClashWar>, IClashOfClansRepository
{
public ClashOfClansRepository(DbContext context) : base(context)
{
}
public IEnumerable<ClashWar> GetAllWars(List<long> guilds)
{
var toReturn = _set
.Where(cw => guilds.Contains((long)cw.GuildId))
.Include(cw => cw.Bases)
.ToList();
toReturn.ForEach(cw => cw.Bases = cw.Bases.Where(w => w.SequenceNumber != null).OrderBy(w => w.SequenceNumber).ToList());
return toReturn;
}
}
}

View File

@ -0,0 +1,95 @@
using NadekoBot.Services.Database.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class ClubRepository : Repository<ClubInfo>, IClubRepository
{
public ClubRepository(DbContext context) : base(context)
{
}
public ClubInfo GetByOwner(ulong userId, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> func = null)
{
if (func == null)
return _set
.Include(x => x.Bans)
.Include(x => x.Applicants)
.Include(x => x.Users)
.Include(x => x.Owner)
.FirstOrDefault(x => x.Owner.UserId == userId);
return func(_set).FirstOrDefault(x => x.Owner.UserId == userId);
}
public ClubInfo GetByOwnerOrAdmin(ulong userId)
{
return _set
.Include(x => x.Bans)
.ThenInclude(x => x.User)
.Include(x => x.Applicants)
.ThenInclude(x => x.User)
.Include(x => x.Owner)
.Include(x => x.Users)
.FirstOrDefault(x => x.Owner.UserId == userId) ??
_context.Set<DiscordUser>()
.Include(x => x.Club)
.ThenInclude(x => x.Users)
.Include(x => x.Club)
.ThenInclude(x => x.Bans)
.ThenInclude(x => x.User)
.Include(x => x.Club)
.ThenInclude(x => x.Applicants)
.ThenInclude(x => x.User)
.Include(x => x.Club)
.ThenInclude(x => x.Owner)
.FirstOrDefault(x => x.UserId == userId && x.IsClubAdmin)
?.Club;
}
public ClubInfo GetByName(string name, int discrim, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> func = null)
{
if (func == null)
return _set
.Where(x => x.Name == name && x.Discrim == discrim)
.Include(x => x.Users)
.Include(x => x.Bans)
.Include(x => x.Applicants)
.FirstOrDefault();
return func(_set).FirstOrDefault(x => x.Name == name && x.Discrim == discrim);
}
public int GetNextDiscrim(string clubName)
{
return _set
.Where(x => x.Name.ToLowerInvariant() == clubName.ToLowerInvariant())
.Select(x => x.Discrim)
.DefaultIfEmpty()
.Max() + 1;
}
public ClubInfo GetByMember(ulong userId, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> func = null)
{
if (func == null)
return _set
.Include(x => x.Users)
.Include(x => x.Bans)
.Include(x => x.Applicants)
.FirstOrDefault(x => x.Users.Any(y => y.UserId == userId));
return func(_set).FirstOrDefault(x => x.Users.Any(y => y.UserId == userId));
}
public ClubInfo[] GetClubLeaderboardPage(int page)
{
return _set
.OrderByDescending(x => x.Xp)
.Skip(page * 9)
.Take(9)
.ToArray();
}
}
}

View File

@ -0,0 +1,57 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class CurrencyRepository : Repository<Currency>, ICurrencyRepository
{
public CurrencyRepository(DbContext context) : base(context)
{
}
public Currency GetOrCreate(ulong userId)
{
var cur = _set.FirstOrDefault(c => c.UserId == userId);
if (cur == null)
{
_set.Add(cur = new Currency()
{
UserId = userId,
Amount = 0
});
_context.SaveChanges();
}
return cur;
}
public IEnumerable<Currency> GetTopRichest(int count, int skip = 0) =>
_set.OrderByDescending(c => c.Amount).Skip(skip).Take(count).ToList();
public long GetUserCurrency(ulong userId) =>
GetOrCreate(userId).Amount;
public bool TryUpdateState(ulong userId, long change)
{
var cur = GetOrCreate(userId);
if (change == 0)
return true;
if (change > 0)
{
cur.Amount += change;
return true;
}
//change is negative
if (cur.Amount + change >= 0)
{
cur.Amount += change;
return true;
}
return false;
}
}
}

View File

@ -0,0 +1,12 @@
using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class CurrencyTransactionsRepository : Repository<CurrencyTransaction>, ICurrencyTransactionsRepository
{
public CurrencyTransactionsRepository(DbContext context) : base(context)
{
}
}
}

View File

@ -0,0 +1,12 @@
using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class CustomReactionsRepository : Repository<CustomReaction>, ICustomReactionRepository
{
public CustomReactionsRepository(DbContext context) : base(context)
{
}
}
}

View File

@ -0,0 +1,63 @@
using NadekoBot.Services.Database.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Discord;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class DiscordUserRepository : Repository<DiscordUser>, IDiscordUserRepository
{
public DiscordUserRepository(DbContext context) : base(context)
{
}
public DiscordUser GetOrCreate(IUser original)
{
DiscordUser toReturn;
toReturn = _set.Include(x => x.Club)
.FirstOrDefault(u => u.UserId == original.Id);
if (toReturn != null)
{
toReturn.AvatarId = original.AvatarId;
toReturn.Username = original.Username;
toReturn.Discriminator = original.Discriminator;
}
if (toReturn == null)
_set.Add(toReturn = new DiscordUser()
{
AvatarId = original.AvatarId,
Discriminator = original.Discriminator,
UserId = original.Id,
Username = original.Username,
Club = null,
});
return toReturn;
}
public int GetUserGlobalRanking(ulong id)
{
if (!_set.Where(y => y.UserId == id).Any())
{
return _set.Count() + 1;
}
return _set.Count(x => x.TotalXp >=
_set.Where(y => y.UserId == id)
.DefaultIfEmpty()
.Sum(y => y.TotalXp));
}
public DiscordUser[] GetUsersXpLeaderboardFor(int page)
{
return _set
.OrderByDescending(x => x.TotalXp)
.Skip(page * 9)
.Take(9)
.AsEnumerable()
.ToArray();
}
}
}

View File

@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class DonatorsRepository : Repository<Donator>, IDonatorsRepository
{
public DonatorsRepository(DbContext context) : base(context)
{
}
public Donator AddOrUpdateDonator(ulong userId, string name, int amount)
{
var donator = _set.Where(d => d.UserId == userId).FirstOrDefault();
if (donator == null)
{
_set.Add(donator = new Donator
{
Amount = amount,
UserId = userId,
Name = name
});
}
else
{
donator.Amount += amount;
donator.Name = name;
_set.Update(donator);
}
return donator;
}
public IEnumerable<Donator> GetDonatorsOrdered() =>
_set.OrderByDescending(d => d.Amount).ToList();
}
}

View File

@ -0,0 +1,211 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class GuildConfigRepository : Repository<GuildConfig>, IGuildConfigRepository
{
public GuildConfigRepository(DbContext context) : base(context)
{
}
private List<WarningPunishment> DefaultWarnPunishments =>
new List<WarningPunishment>() {
new WarningPunishment() {
Count = 3,
Punishment = PunishmentAction.Kick
},
new WarningPunishment() {
Count = 5,
Punishment = PunishmentAction.Ban
}
};
public IEnumerable<GuildConfig> GetAllGuildConfigs(List<long> availableGuilds) =>
_set
.Where(gc => availableGuilds.Contains((long)gc.GuildId))
.Include(gc => gc.LogSetting)
.ThenInclude(ls => ls.IgnoredChannels)
.Include(gc => gc.MutedUsers)
.Include(gc => gc.CommandAliases)
.Include(gc => gc.UnmuteTimers)
.Include(gc => gc.VcRoleInfos)
.Include(gc => gc.GenerateCurrencyChannelIds)
.Include(gc => gc.FilterInvitesChannelIds)
.Include(gc => gc.FilterWordsChannelIds)
.Include(gc => gc.FilteredWords)
.Include(gc => gc.CommandCooldowns)
.Include(gc => gc.GuildRepeaters)
.Include(gc => gc.AntiRaidSetting)
.Include(gc => gc.SlowmodeIgnoredRoles)
.Include(gc => gc.SlowmodeIgnoredUsers)
.Include(gc => gc.AntiSpamSetting)
.ThenInclude(x => x.IgnoredChannels)
.Include(gc => gc.FeedSubs)
.ThenInclude(x => x.GuildConfig)
.Include(gc => gc.FollowedStreams)
.Include(gc => gc.StreamRole)
.Include(gc => gc.NsfwBlacklistedTags)
.Include(gc => gc.XpSettings)
.ThenInclude(x => x.ExclusionList)
.ToList();
/// <summary>
/// Gets and creates if it doesn't exist a config for a guild.
/// </summary>
/// <param name="guildId">For which guild</param>
/// <param name="includes">Use to manipulate the set however you want</param>
/// <returns>Config for the guild</returns>
public GuildConfig For(ulong guildId, Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes = null)
{
GuildConfig config;
if (includes == null)
{
config = _set
.Include(gc => gc.FollowedStreams)
.Include(gc => gc.LogSetting)
.ThenInclude(ls => ls.IgnoredChannels)
.Include(gc => gc.FilterInvitesChannelIds)
.Include(gc => gc.FilterWordsChannelIds)
.Include(gc => gc.FilteredWords)
.Include(gc => gc.GenerateCurrencyChannelIds)
.Include(gc => gc.CommandCooldowns)
.FirstOrDefault(c => c.GuildId == guildId);
}
else
{
var set = includes(_set);
config = set.FirstOrDefault(c => c.GuildId == guildId);
}
if (config == null)
{
_set.Add((config = new GuildConfig
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist,
WarningsInitialized = true,
WarnPunishments = DefaultWarnPunishments,
}));
_context.SaveChanges();
}
if (!config.WarningsInitialized)
{
config.WarningsInitialized = true;
config.WarnPunishments = DefaultWarnPunishments;
}
return config;
}
public GuildConfig LogSettingsFor(ulong guildId)
{
var config = _set.Include(gc => gc.LogSetting)
.ThenInclude(gc => gc.IgnoredChannels)
.FirstOrDefault(x => x.GuildId == guildId);
if (config == null)
{
_set.Add((config = new GuildConfig
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist,
WarningsInitialized = true,
WarnPunishments = DefaultWarnPunishments,
}));
_context.SaveChanges();
}
if (!config.WarningsInitialized)
{
config.WarningsInitialized = true;
config.WarnPunishments = DefaultWarnPunishments;
}
return config;
}
public IEnumerable<GuildConfig> OldPermissionsForAll()
{
var query = _set
.Where(gc => gc.RootPermission != null)
.Include(gc => gc.RootPermission);
for (int i = 0; i < 60; i++)
{
query = query.ThenInclude(gc => gc.Next);
}
return query.ToList();
}
public IEnumerable<GuildConfig> Permissionsv2ForAll(List<long> include)
{
var query = _set
.Where(x => include.Contains((long)x.GuildId))
.Include(gc => gc.Permissions);
return query.ToList();
}
public GuildConfig GcWithPermissionsv2For(ulong guildId)
{
var config = _set
.Where(gc => gc.GuildId == guildId)
.Include(gc => gc.Permissions)
.FirstOrDefault();
if (config == null) // if there is no guildconfig, create new one
{
_set.Add((config = new GuildConfig
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist
}));
_context.SaveChanges();
}
else if (config.Permissions == null || !config.Permissions.Any()) // if no perms, add default ones
{
config.Permissions = Permissionv2.GetDefaultPermlist;
_context.SaveChanges();
}
return config;
}
public IEnumerable<FollowedStream> GetAllFollowedStreams(List<long> included) =>
_set
.Where(gc => included.Contains((long)gc.GuildId))
.Include(gc => gc.FollowedStreams)
.SelectMany(gc => gc.FollowedStreams)
.ToList();
public void SetCleverbotEnabled(ulong id, bool cleverbotEnabled)
{
var conf = _set.FirstOrDefault(gc => gc.GuildId == id);
if (conf == null)
return;
conf.CleverbotEnabled = cleverbotEnabled;
}
public XpSettings XpSettingsFor(ulong guildId)
{
var gc = For(guildId,
set => set.Include(x => x.XpSettings)
.ThenInclude(x => x.RoleRewards)
.Include(x => x.XpSettings)
.ThenInclude(x => x.ExclusionList));
if (gc.XpSettings == null)
gc.XpSettings = new XpSettings();
return gc.XpSettings;
}
}
}

View File

@ -0,0 +1,30 @@
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class MusicPlaylistRepository : Repository<MusicPlaylist>, IMusicPlaylistRepository
{
public MusicPlaylistRepository(DbContext context) : base(context)
{
}
public List<MusicPlaylist> GetPlaylistsOnPage(int num)
{
if (num < 1)
throw new IndexOutOfRangeException();
return _set.Skip((num - 1) * 20)
.Take(20)
.Include(pl => pl.Songs)
.ToList();
}
public MusicPlaylist GetWithSongs(int id) =>
_set.Include(mpl => mpl.Songs)
.FirstOrDefault(mpl => mpl.Id == id);
}
}

View File

@ -0,0 +1,20 @@
using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class PokeGameRepository : Repository<UserPokeTypes>, IPokeGameRepository
{
public PokeGameRepository(DbContext context) : base(context)
{
}
//List<UserPokeTypes> GetAllPokeTypes()
//{
// var toReturn = _set.Include(pt => pt.UserId).ToList();
// toReturn.ForEach(pt => pt.).ToList();
// return toReturn;
//}
}
}

View File

@ -0,0 +1,39 @@
using NadekoBot.Services.Database.Models;
using NadekoBot.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Common;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class QuoteRepository : Repository<Quote>, IQuoteRepository
{
public QuoteRepository(DbContext context) : base(context)
{
}
public IEnumerable<Quote> GetAllQuotesByKeyword(ulong guildId, string keyword) =>
_set.Where(q => q.GuildId == guildId && q.Keyword == keyword);
public IEnumerable<Quote> GetGroup(ulong guildId, int skip, int take) =>
_set.Where(q=>q.GuildId == guildId).OrderBy(q => q.Keyword).Skip(skip).Take(take).ToList();
public Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string keyword)
{
var rng = new NadekoRandom();
return _set.Where(q => q.GuildId == guildId && q.Keyword == keyword).OrderBy(q => rng.Next()).FirstOrDefaultAsync();
}
public Task<Quote> SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text)
{
var rngk = new NadekoRandom();
return _set.Where(q => q.Text.ContainsNoCase(text, StringComparison.OrdinalIgnoreCase) && q.GuildId == guildId && q.Keyword == keyword).OrderBy(q => rngk.Next()).FirstOrDefaultAsync();
}
public void RemoveAllByKeyword(ulong guildId, string keyword) =>
_set.RemoveRange(_set.Where(x => x.GuildId == guildId && x.Keyword.ToUpper() == keyword));
}
}

View File

@ -0,0 +1,19 @@
using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class ReminderRepository : Repository<Reminder>, IReminderRepository
{
public ReminderRepository(DbContext context) : base(context)
{
}
public IEnumerable<Reminder> GetIncludedReminders(IEnumerable<long> guildIds)
{
return _set.Where(x => guildIds.Contains((long)x.ServerId)).ToList();
}
}
}

View File

@ -0,0 +1,46 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using System.Linq;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class Repository<T> : IRepository<T> where T : DbEntity
{
protected DbContext _context;
protected DbSet<T> _set;
public Repository(DbContext context)
{
_context = context;
_set = context.Set<T>();
}
public void Add(T obj) =>
_set.Add(obj);
public void AddRange(params T[] objs) =>
_set.AddRange(objs);
public T Get(int id) =>
_set.FirstOrDefault(e => e.Id == id);
public IEnumerable<T> GetAll() =>
_set.ToList();
public void Remove(int id) =>
_set.Remove(this.Get(id));
public void Remove(T obj) =>
_set.Remove(obj);
public void RemoveRange(params T[] objs) =>
_set.RemoveRange(objs);
public void Update(T obj) =>
_set.Update(obj);
public void UpdateRange(params T[] objs) =>
_set.UpdateRange(objs);
}
}

View File

@ -0,0 +1,28 @@
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class SelfAssignedRolesRepository : Repository<SelfAssignedRole>, ISelfAssignedRolesRepository
{
public SelfAssignedRolesRepository(DbContext context) : base(context)
{
}
public bool DeleteByGuildAndRoleId(ulong guildId, ulong roleId)
{
var role = _set.FirstOrDefault(s => s.GuildId == guildId && s.RoleId == roleId);
if (role == null)
return false;
_set.Remove(role);
return true;
}
public IEnumerable<SelfAssignedRole> GetFromGuild(ulong guildId) =>
_set.Where(s => s.GuildId == guildId).ToList();
}
}

View File

@ -0,0 +1,26 @@
using NadekoBot.Services.Database.Models;
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class UnitConverterRepository : Repository<ConvertUnit>, IUnitConverterRepository
{
public UnitConverterRepository(DbContext context) : base(context)
{
}
public void AddOrUpdate(Func<ConvertUnit, bool> check, ConvertUnit toAdd, Func<ConvertUnit, ConvertUnit> toUpdate)
{
var existing = _set.FirstOrDefault(check);
if (existing != null)
{
existing = toUpdate.Invoke(existing);
}
else _set.Add(toAdd);
}
public bool Empty() => !_set.Any();
}
}

View File

@ -0,0 +1,50 @@
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class WaifuRepository : Repository<WaifuInfo>, IWaifuRepository
{
public WaifuRepository(DbContext context) : base(context)
{
}
public WaifuInfo ByWaifuUserId(ulong userId)
{
return _set.Include(wi => wi.Waifu)
.Include(wi => wi.Affinity)
.Include(wi => wi.Claimer)
.Include(wi => wi.Items)
.FirstOrDefault(wi => wi.Waifu.UserId == userId);
}
public IList<WaifuInfo> ByClaimerUserId(ulong userId)
{
return _set.Include(wi => wi.Waifu)
.Include(wi => wi.Affinity)
.Include(wi => wi.Claimer)
.Include(wi => wi.Items)
.Where(wi => wi.Claimer != null && wi.Claimer.UserId == userId)
.ToList();
}
public IList<WaifuInfo> GetTop(int count, int skip = 0)
{
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
if (count == 0)
return new List<WaifuInfo>();
return _set.Include(wi => wi.Waifu)
.Include(wi => wi.Affinity)
.Include(wi => wi.Claimer)
.OrderByDescending(wi => wi.Price)
.Skip(skip)
.Take(count)
.ToList();
}
}
}

View File

@ -0,0 +1,41 @@
using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class WarningsRepository : Repository<Warning>, IWarningsRepository
{
public WarningsRepository(DbContext context) : base(context)
{
}
public Warning[] For(ulong guildId, ulong userId)
{
var query = _set.Where(x => x.GuildId == guildId && x.UserId == userId)
.OrderByDescending(x => x.DateAdded);
return query.ToArray();
}
public async Task ForgiveAll(ulong guildId, ulong userId, string mod)
{
await _set.Where(x => x.GuildId == guildId && x.UserId == userId)
.ForEachAsync(x =>
{
if (x.Forgiven != true)
{
x.Forgiven = true;
x.ForgivenBy = mod;
}
})
.ConfigureAwait(false);
}
public Warning[] GetForGuild(ulong id)
{
return _set.Where(x => x.GuildId == id).ToArray();
}
}
}

View File

@ -0,0 +1,60 @@
using NadekoBot.Services.Database.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class XpRepository : Repository<UserXpStats>, IXpRepository
{
public XpRepository(DbContext context) : base(context)
{
}
public UserXpStats GetOrCreateUser(ulong guildId, ulong userId)
{
var usr = _set.FirstOrDefault(x => x.UserId == userId && x.GuildId == guildId);
if (usr == null)
{
_context.Add(usr = new UserXpStats()
{
Xp = 0,
UserId = userId,
NotifyOnLevelUp = XpNotificationType.None,
GuildId = guildId,
});
}
return usr;
}
public UserXpStats[] GetUsersFor(ulong guildId, int page)
{
return _set.Where(x => x.GuildId == guildId)
.OrderByDescending(x => x.Xp + x.AwardedXp)
.Skip(page * 9)
.Take(9)
.ToArray();
}
public int GetUserGuildRanking(ulong userId, ulong guildId)
{
if (!_set.Where(x => x.GuildId == guildId && x.UserId == userId).Any())
{
var cnt = _set.Count(x => x.GuildId == guildId);
if (cnt == 0)
return 1;
else
return cnt;
}
return _set
.Where(x => x.GuildId == guildId)
.Count(x => x.Xp > (_set
.Where(y => y.UserId == userId && y.GuildId == guildId)
.Select(y => y.Xp)
.DefaultIfEmpty()
.Sum())) + 1;
}
}
}

View File

@ -0,0 +1,93 @@
using NadekoBot.Services.Database.Repositories;
using NadekoBot.Services.Database.Repositories.Impl;
using System;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database
{
public class UnitOfWork : IUnitOfWork
{
public NadekoContext _context { get; }
private IQuoteRepository _quotes;
public IQuoteRepository Quotes => _quotes ?? (_quotes = new QuoteRepository(_context));
private IGuildConfigRepository _guildConfigs;
public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context));
private IDonatorsRepository _donators;
public IDonatorsRepository Donators => _donators ?? (_donators = new DonatorsRepository(_context));
private IClashOfClansRepository _clashOfClans;
public IClashOfClansRepository ClashOfClans => _clashOfClans ?? (_clashOfClans = new ClashOfClansRepository(_context));
private IReminderRepository _reminders;
public IReminderRepository Reminders => _reminders ?? (_reminders = new ReminderRepository(_context));
private ISelfAssignedRolesRepository _selfAssignedRoles;
public ISelfAssignedRolesRepository SelfAssignedRoles => _selfAssignedRoles ?? (_selfAssignedRoles = new SelfAssignedRolesRepository(_context));
private IBotConfigRepository _botConfig;
public IBotConfigRepository BotConfig => _botConfig ?? (_botConfig = new BotConfigRepository(_context));
private ICurrencyRepository _currency;
public ICurrencyRepository Currency => _currency ?? (_currency = new CurrencyRepository(_context));
private ICurrencyTransactionsRepository _currencyTransactions;
public ICurrencyTransactionsRepository CurrencyTransactions => _currencyTransactions ?? (_currencyTransactions = new CurrencyTransactionsRepository(_context));
private IUnitConverterRepository _conUnits;
public IUnitConverterRepository ConverterUnits => _conUnits ?? (_conUnits = new UnitConverterRepository(_context));
private IMusicPlaylistRepository _musicPlaylists;
public IMusicPlaylistRepository MusicPlaylists => _musicPlaylists ?? (_musicPlaylists = new MusicPlaylistRepository(_context));
private ICustomReactionRepository _customReactions;
public ICustomReactionRepository CustomReactions => _customReactions ?? (_customReactions = new CustomReactionsRepository(_context));
private IPokeGameRepository _pokegame;
public IPokeGameRepository PokeGame => _pokegame ?? (_pokegame = new PokeGameRepository(_context));
private IWaifuRepository _waifus;
public IWaifuRepository Waifus => _waifus ?? (_waifus = new WaifuRepository(_context));
private IDiscordUserRepository _discordUsers;
public IDiscordUserRepository DiscordUsers => _discordUsers ?? (_discordUsers = new DiscordUserRepository(_context));
private IWarningsRepository _warnings;
public IWarningsRepository Warnings => _warnings ?? (_warnings = new WarningsRepository(_context));
private IXpRepository _xp;
public IXpRepository Xp => _xp ?? (_xp = new XpRepository(_context));
private IClubRepository _clubs;
public IClubRepository Clubs => _clubs ?? (_clubs = new ClubRepository(_context));
public UnitOfWork(NadekoContext context)
{
_context = context;
}
public int Complete() =>
_context.SaveChanges();
public Task<int> CompleteAsync() =>
_context.SaveChangesAsync();
private bool disposed = false;
protected void Dispose(bool disposing)
{
if (!this.disposed)
if (disposing)
_context.Dispose();
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}