2017-02-15 10:03:40 +00:00
using System ;
using Discord ;
2016-03-25 11:48:22 +00:00
using Discord.Commands ;
2016-08-20 14:10:37 +00:00
using NadekoBot.Attributes ;
2016-03-25 11:48:22 +00:00
using NadekoBot.Extensions ;
using System.Linq ;
2016-08-20 14:10:37 +00:00
using System.Threading.Tasks ;
using NadekoBot.Services ;
2016-08-29 22:00:19 +00:00
using NadekoBot.Services.Database.Models ;
using System.Collections.Generic ;
2016-03-25 11:48:22 +00:00
namespace NadekoBot.Modules.Gambling
{
2016-09-10 19:45:12 +00:00
[NadekoModule("Gambling", "$")]
2017-02-20 22:46:34 +00:00
public partial class Gambling : NadekoTopLevelModule
2016-03-25 11:48:22 +00:00
{
2016-08-28 00:46:36 +00:00
public static string CurrencyName { get ; set ; }
public static string CurrencyPluralName { get ; set ; }
public static string CurrencySign { get ; set ; }
2017-01-01 15:39:24 +00:00
2016-12-29 06:50:05 +00:00
static Gambling ( )
2016-03-25 11:48:22 +00:00
{
2017-01-12 00:21:32 +00:00
CurrencyName = NadekoBot . BotConfig . CurrencyName ;
CurrencyPluralName = NadekoBot . BotConfig . CurrencyPluralName ;
CurrencySign = NadekoBot . BotConfig . CurrencySign ;
2016-03-25 11:48:22 +00:00
}
2016-11-26 09:54:33 +00:00
public static long GetCurrency ( ulong id )
{
using ( var uow = DbHandler . UnitOfWork ( ) )
{
return uow . Currency . GetUserCurrency ( id ) ;
}
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-20 14:10:37 +00:00
[RequireContext(ContextType.Guild)]
2016-12-17 00:16:14 +00:00
public async Task Raffle ( [ Remainder ] IRole role = null )
2016-08-20 14:10:37 +00:00
{
2016-12-17 00:16:14 +00:00
role = role ? ? Context . Guild . EveryoneRole ;
2016-08-20 14:10:37 +00:00
2016-11-11 19:46:47 +00:00
var members = role . Members ( ) . Where ( u = > u . Status ! = UserStatus . Offline & & u . Status ! = UserStatus . Unknown ) ;
2016-08-20 14:10:37 +00:00
var membersArray = members as IUser [ ] ? ? members . ToArray ( ) ;
2016-09-10 19:40:25 +00:00
var usr = membersArray [ new NadekoRandom ( ) . Next ( 0 , membersArray . Length ) ] ;
2017-02-15 10:03:40 +00:00
await Context . Channel . SendConfirmAsync ( "🎟 " + GetText ( "raffled_user" ) , $"**{usr.Username}#{usr.Discriminator}**" , footer : $"ID: {usr.Id}" ) . ConfigureAwait ( false ) ;
2016-08-20 14:10:37 +00:00
}
2016-10-08 18:24:34 +00:00
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-10-19 05:16:47 +00:00
[Priority(0)]
2016-12-17 00:16:14 +00:00
public async Task Cash ( [ Remainder ] IUser user = null )
2016-08-29 22:00:19 +00:00
{
2017-02-17 13:53:13 +00:00
if ( user = = null )
await ConfirmLocalized ( "has" , Format . Bold ( Context . User . ToString ( ) ) , $"{GetCurrency(Context.User.Id)} {CurrencySign}" ) . ConfigureAwait ( false ) ;
else
await ReplyConfirmLocalized ( "has" , Format . Bold ( user . ToString ( ) ) , $"{GetCurrency(user.Id)} {CurrencySign}" ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
}
2016-10-08 18:24:34 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-10-19 05:16:47 +00:00
[Priority(1)]
2016-12-17 00:16:14 +00:00
public async Task Cash ( ulong userId )
2016-10-08 18:24:34 +00:00
{
2017-02-15 10:03:40 +00:00
await ReplyConfirmLocalized ( "has" , Format . Code ( userId . ToString ( ) ) , $"{GetCurrency(userId)} {CurrencySign}" ) . ConfigureAwait ( false ) ;
2016-10-08 18:24:34 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-29 22:00:19 +00:00
[RequireContext(ContextType.Guild)]
2016-12-17 00:16:14 +00:00
public async Task Give ( long amount , [ Remainder ] IGuildUser receiver )
2016-08-29 22:00:19 +00:00
{
2016-12-16 18:43:57 +00:00
if ( amount < = 0 | | Context . User . Id = = receiver . Id )
2016-08-29 22:00:19 +00:00
return ;
2017-01-23 19:41:48 +00:00
var success = await CurrencyHandler . RemoveCurrencyAsync ( ( IGuildUser ) Context . User , $"Gift to {receiver.Username} ({receiver.Id})." , amount , false ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
if ( ! success )
{
2017-02-15 10:03:40 +00:00
await ReplyErrorLocalized ( "not_enough" , CurrencyPluralName ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
return ;
}
2016-12-16 18:43:57 +00:00
await CurrencyHandler . AddCurrencyAsync ( receiver , $"Gift from {Context.User.Username} ({Context.User.Id})." , amount , true ) . ConfigureAwait ( false ) ;
2017-02-15 10:03:40 +00:00
await ReplyConfirmLocalized ( "gifted" , amount + CurrencySign , Format . Bold ( receiver . ToString ( ) ) )
. ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
}
2016-08-20 14:33:18 +00:00
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-09-14 12:23:09 +00:00
[RequireContext(ContextType.Guild)]
2016-09-30 02:20:09 +00:00
[OwnerOnly]
2016-10-24 11:59:16 +00:00
[Priority(2)]
2016-12-17 00:16:14 +00:00
public Task Award ( int amount , [ Remainder ] IGuildUser usr ) = >
Award ( amount , usr . Id ) ;
2016-08-20 14:33:18 +00:00
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-09-30 02:20:09 +00:00
[OwnerOnly]
2016-10-24 11:59:16 +00:00
[Priority(1)]
2016-12-17 00:16:14 +00:00
public async Task Award ( int amount , ulong usrId )
2016-09-14 12:23:09 +00:00
{
if ( amount < = 0 )
return ;
2016-08-20 14:33:18 +00:00
2016-12-16 18:43:57 +00:00
await CurrencyHandler . AddCurrencyAsync ( usrId , $"Awarded by bot owner. ({Context.User.Username}/{Context.User.Id})" , amount ) . ConfigureAwait ( false ) ;
2017-02-15 10:03:40 +00:00
await ReplyConfirmLocalized ( "awarded" , amount + CurrencySign , $"<@{usrId}>" ) . ConfigureAwait ( false ) ;
2016-09-14 12:23:09 +00:00
}
2016-10-24 11:59:16 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
[Priority(0)]
2016-12-17 00:16:14 +00:00
public async Task Award ( int amount , [ Remainder ] IRole role )
2016-10-24 11:59:16 +00:00
{
2016-12-17 04:09:04 +00:00
var users = ( await Context . Guild . GetUsersAsync ( ) )
. Where ( u = > u . GetRoles ( ) . Contains ( role ) )
2016-10-24 11:59:16 +00:00
. ToList ( ) ;
await Task . WhenAll ( users . Select ( u = > CurrencyHandler . AddCurrencyAsync ( u . Id ,
2016-12-16 18:43:57 +00:00
$"Awarded by bot owner to **{role.Name}** role. ({Context.User.Username}/{Context.User.Id})" ,
2016-10-24 11:59:16 +00:00
amount ) ) )
. ConfigureAwait ( false ) ;
2017-02-15 10:03:40 +00:00
await ReplyConfirmLocalized ( "mass_award" ,
amount + CurrencySign ,
Format . Bold ( users . Count . ToString ( ) ) ,
Format . Bold ( role . Name ) ) . ConfigureAwait ( false ) ;
2016-10-24 11:59:16 +00:00
}
2017-01-01 15:39:24 +00:00
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-09-30 02:20:09 +00:00
[RequireContext(ContextType.Guild)]
[OwnerOnly]
2016-12-17 00:16:14 +00:00
public async Task Take ( long amount , [ Remainder ] IGuildUser user )
2016-09-30 02:20:09 +00:00
{
if ( amount < = 0 )
return ;
2017-01-01 15:39:24 +00:00
if ( await CurrencyHandler . RemoveCurrencyAsync ( user , $"Taken by bot owner.({Context.User.Username}/{Context.User.Id})" , amount , true ) . ConfigureAwait ( false ) )
2017-02-15 10:03:40 +00:00
await ReplyConfirmLocalized ( "take" , amount + CurrencySign , Format . Bold ( user . ToString ( ) ) ) . ConfigureAwait ( false ) ;
2016-10-08 18:24:34 +00:00
else
2017-02-15 10:03:40 +00:00
await ReplyErrorLocalized ( "take_fail" , amount + CurrencySign , Format . Bold ( user . ToString ( ) ) , CurrencyPluralName ) . ConfigureAwait ( false ) ;
2016-09-30 02:20:09 +00:00
}
2016-08-20 14:33:18 +00:00
2016-09-30 02:20:09 +00:00
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-09-30 02:20:09 +00:00
[OwnerOnly]
2016-12-17 00:16:14 +00:00
public async Task Take ( long amount , [ Remainder ] ulong usrId )
2016-09-30 02:20:09 +00:00
{
if ( amount < = 0 )
return ;
2017-01-01 15:39:24 +00:00
if ( await CurrencyHandler . RemoveCurrencyAsync ( usrId , $"Taken by bot owner.({Context.User.Username}/{Context.User.Id})" , amount ) . ConfigureAwait ( false ) )
2017-02-15 10:03:40 +00:00
await ReplyConfirmLocalized ( "take" , amount + CurrencySign , $"<@{usrId}>" ) . ConfigureAwait ( false ) ;
2016-10-08 18:24:34 +00:00
else
2017-02-15 10:03:40 +00:00
await ReplyErrorLocalized ( "take_fail" , amount + CurrencySign , Format . Code ( usrId . ToString ( ) ) , CurrencyPluralName ) . ConfigureAwait ( false ) ;
2016-09-30 02:20:09 +00:00
}
2016-08-20 14:33:18 +00:00
2017-01-22 20:29:41 +00:00
//[NadekoCommand, Usage, Description, Aliases]
//[OwnerOnly]
//public Task BrTest(int tests = 1000)
//{
// var t = Task.Run(async () =>
// {
// if (tests <= 0)
// return;
// //multi vs how many times it occured
// var dict = new Dictionary<int, int>();
// var generator = new NadekoRandom();
// for (int i = 0; i < tests; i++)
// {
// var rng = generator.Next(0, 101);
// var mult = 0;
// if (rng < 67)
// {
// mult = 0;
// }
// else if (rng < 91)
// {
// mult = 2;
// }
// else if (rng < 100)
// {
// mult = 4;
// }
// else
// mult = 10;
// if (dict.ContainsKey(mult))
// dict[mult] += 1;
// else
// dict.Add(mult, 1);
// }
// var sb = new StringBuilder();
// const int bet = 1;
// int payout = 0;
// foreach (var key in dict.Keys.OrderByDescending(x => x))
// {
// sb.AppendLine($"x{key} occured {dict[key]} times. {dict[key] * 1.0f / tests * 100}%");
// payout += key * dict[key];
// }
// try
// {
// await Context.Channel.SendConfirmAsync("BetRoll Test Results", sb.ToString(),
// footer: $"Total Bet: {tests * bet} | Payout: {payout * bet} | {payout * 1.0f / tests * 100}%");
// }
// catch { }
// });
// return Task.CompletedTask;
//}
2017-01-19 18:36:57 +00:00
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-12-17 00:16:14 +00:00
public async Task BetRoll ( long amount )
2016-08-29 22:00:19 +00:00
{
if ( amount < 1 )
return ;
2016-08-20 14:33:18 +00:00
2017-02-15 10:03:40 +00:00
if ( ! await CurrencyHandler . RemoveCurrencyAsync ( Context . User , "Betroll Gamble" , amount , false ) . ConfigureAwait ( false ) )
2016-08-29 22:00:19 +00:00
{
2017-02-15 10:03:40 +00:00
await ReplyErrorLocalized ( "not_enough" , CurrencyPluralName ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
return ;
}
2017-02-15 10:03:40 +00:00
var rnd = new NadekoRandom ( ) . Next ( 0 , 101 ) ;
var str = Context . User . Mention + Format . Code ( GetText ( "roll" , rnd ) ) ;
if ( rnd < 67 )
2016-08-29 22:00:19 +00:00
{
2017-02-15 10:03:40 +00:00
str + = GetText ( "better_luck" ) ;
2016-08-29 22:00:19 +00:00
}
else
{
2017-02-15 10:03:40 +00:00
if ( rnd < 91 )
{
str + = GetText ( "br_win" , ( amount * NadekoBot . BotConfig . Betroll67Multiplier ) + CurrencySign , 66 ) ;
await CurrencyHandler . AddCurrencyAsync ( Context . User , "Betroll Gamble" ,
( int ) ( amount * NadekoBot . BotConfig . Betroll67Multiplier ) , false ) . ConfigureAwait ( false ) ;
}
else if ( rnd < 100 )
{
str + = GetText ( "br_win" , ( amount * NadekoBot . BotConfig . Betroll91Multiplier ) + CurrencySign , 90 ) ;
await CurrencyHandler . AddCurrencyAsync ( Context . User , "Betroll Gamble" ,
( int ) ( amount * NadekoBot . BotConfig . Betroll91Multiplier ) , false ) . ConfigureAwait ( false ) ;
}
else
{
str + = GetText ( "br_win" , ( amount * NadekoBot . BotConfig . Betroll100Multiplier ) + CurrencySign , 100 ) + " 👑" ;
await CurrencyHandler . AddCurrencyAsync ( Context . User , "Betroll Gamble" ,
( int ) ( amount * NadekoBot . BotConfig . Betroll100Multiplier ) , false ) . ConfigureAwait ( false ) ;
}
2016-08-29 22:00:19 +00:00
}
2016-12-16 21:44:26 +00:00
await Context . Channel . SendConfirmAsync ( str ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2017-03-09 01:17:18 +00:00
public async Task Leaderboard ( int page = 1 )
2016-08-29 22:00:19 +00:00
{
2017-02-15 10:03:40 +00:00
List < Currency > richest ;
2016-08-29 22:00:19 +00:00
using ( var uow = DbHandler . UnitOfWork ( ) )
{
2017-03-09 01:17:18 +00:00
richest = uow . Currency . GetTopRichest ( 9 , 9 * ( page - 1 ) ) . ToList ( ) ;
2016-08-29 22:00:19 +00:00
}
2017-01-23 00:33:07 +00:00
var embed = new EmbedBuilder ( )
. WithOkColor ( )
2017-03-09 01:17:18 +00:00
. WithTitle ( NadekoBot . BotConfig . CurrencySign +
" " + GetText ( "leaderboard" ) )
. WithFooter ( efb = > efb . WithText ( GetText ( "page" , page ) ) ) ;
if ( ! richest . Any ( ) )
{
embed . WithDescription ( GetText ( "no_users_found" ) ) ;
await Context . Channel . EmbedAsync ( embed ) . ConfigureAwait ( false ) ;
return ;
}
2017-01-23 00:33:07 +00:00
for ( var i = 0 ; i < richest . Count ; i + + )
{
var x = richest [ i ] ;
var usr = await Context . Guild . GetUserAsync ( x . UserId ) . ConfigureAwait ( false ) ;
2017-02-15 10:03:40 +00:00
var usrStr = usr = = null
? x . UserId . ToString ( )
: usr . Username ? . TrimTo ( 20 , true ) ;
var j = i ;
embed . AddField ( efb = > efb . WithName ( "#" + ( j + 1 ) + " " + usrStr )
. WithValue ( x . Amount . ToString ( ) + " " + NadekoBot . BotConfig . CurrencySign )
. WithIsInline ( true ) ) ;
2017-01-23 00:33:07 +00:00
}
await Context . Channel . EmbedAsync ( embed ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
}
2016-08-20 14:43:44 +00:00
}
2016-03-25 11:48:22 +00:00
}