2016-03-25 11:48:22 +00:00
using Discord ;
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-05-11 09:37:19 +00:00
using System.Text ;
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", "$")]
2016-08-20 14:10:37 +00:00
public partial class Gambling : DiscordModule
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 ) ] ;
2016-12-16 21:44:26 +00:00
await Context . Channel . SendConfirmAsync ( "🎟 Raffled user" , $"**{usr.Username}#{usr.Discriminator}** 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
{
2016-12-16 18:43:57 +00:00
user = user ? ? Context . User ;
2016-08-20 14:33:18 +00:00
2016-12-16 21:44:26 +00:00
await Context . Channel . SendConfirmAsync ( $"{user.Username} has {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
{
2016-12-16 21:44:26 +00:00
await Context . Channel . SendConfirmAsync ( $"`{userId}` has {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 ;
2016-12-16 18:43:57 +00:00
var success = await CurrencyHandler . RemoveCurrencyAsync ( ( IGuildUser ) Context . User , $"Gift to {receiver.Username} ({receiver.Id})." , amount , true ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
if ( ! success )
{
2017-01-12 00:26:47 +00:00
await Context . Channel . SendErrorAsync ( $"{Context.User.Mention} You don't have 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-01-12 00:26:47 +00:00
await Context . Channel . SendConfirmAsync ( $"{Context.User.Mention} successfully sent {amount} {(amount == 1 ? CurrencyName : CurrencyPluralName)} to {receiver.Mention}!" ) . 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 ) ;
2016-08-20 14:33:18 +00:00
2017-01-12 00:26:47 +00:00
await Context . Channel . SendConfirmAsync ( $"{Context.User.Mention} awarded {amount} {(amount == 1 ? CurrencyName : CurrencyPluralName)} to <@{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-16 18:43:57 +00:00
var channel = ( ITextChannel ) Context . Channel ;
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-01-12 00:26:47 +00:00
await Context . Channel . SendConfirmAsync ( $"Awarded `{amount}` {CurrencyPluralName} to `{users.Count}` users from `{role.Name}` role." )
2016-10-24 11:59:16 +00:00
. ConfigureAwait ( false ) ;
}
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-01-12 00:26:47 +00:00
await Context . Channel . SendConfirmAsync ( $"{Context.User.Mention} successfully took {amount} {(amount == 1 ? CurrencyName : CurrencyPluralName)} from {user}!" ) . ConfigureAwait ( false ) ;
2016-10-08 18:24:34 +00:00
else
2017-01-12 00:26:47 +00:00
await Context . Channel . SendErrorAsync ( $"{Context.User.Mention} was unable to take {amount} {(amount == 1 ? CurrencyName : CurrencyPluralName)} from {user} because the user doesn't have that much {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-01-12 00:26:47 +00:00
await Context . Channel . SendConfirmAsync ( $"{Context.User.Mention} successfully took {amount} {(amount == 1 ? CurrencyName : CurrencyPluralName)} from <@{usrId}>!" ) . ConfigureAwait ( false ) ;
2016-10-08 18:24:34 +00:00
else
2017-01-12 00:26:47 +00:00
await Context . Channel . SendErrorAsync ( $"{Context.User.Mention} was unable to take {amount} {(amount == 1 ? CurrencyName : CurrencyPluralName)} from `{usrId}` because the user doesn't have that much {CurrencyPluralName}!" ) . ConfigureAwait ( false ) ;
2016-09-30 02:20:09 +00:00
}
2016-08-20 14:33:18 +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
2016-08-29 22:00:19 +00:00
long userFlowers ;
using ( var uow = DbHandler . UnitOfWork ( ) )
{
2016-12-16 18:43:57 +00:00
userFlowers = uow . Currency . GetOrCreate ( Context . User . Id ) . Amount ;
2016-08-29 22:00:19 +00:00
}
2016-08-20 14:43:44 +00:00
2016-08-29 22:00:19 +00:00
if ( userFlowers < amount )
{
2017-01-12 00:26:47 +00:00
await Context . Channel . SendErrorAsync ( $"{Context.User.Mention} You don't have enough {CurrencyPluralName}. You only have {userFlowers}{CurrencySign}." ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
return ;
}
2017-01-01 15:39:24 +00:00
await CurrencyHandler . RemoveCurrencyAsync ( Context . User , "Betroll Gamble" , amount , false ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
2016-09-10 19:40:25 +00:00
var rng = new NadekoRandom ( ) . Next ( 0 , 101 ) ;
2017-01-01 15:39:24 +00:00
var str = $"{Context.User.Mention} `You rolled {rng}.` " ;
2016-08-29 22:00:19 +00:00
if ( rng < 67 )
{
2016-11-29 01:17:00 +00:00
str + = "Better luck next time." ;
2016-08-29 22:00:19 +00:00
}
2016-11-11 00:54:53 +00:00
else if ( rng < 91 )
2016-08-29 22:00:19 +00:00
{
2017-01-12 19:19:56 +00:00
str + = $"Congratulations! You won {amount * NadekoBot.BotConfig.Betroll67Multiplier}{CurrencySign} for rolling above 66" ;
2017-01-13 12:32:48 +00:00
await CurrencyHandler . AddCurrencyAsync ( Context . User , "Betroll Gamble" , ( int ) ( amount * NadekoBot . BotConfig . Betroll67Multiplier ) , false ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
}
else if ( rng < 100 )
{
2017-01-12 19:19:56 +00:00
str + = $"Congratulations! You won {amount * NadekoBot.BotConfig.Betroll91Multiplier}{CurrencySign} for rolling above 90." ;
2017-01-13 12:32:48 +00:00
await CurrencyHandler . AddCurrencyAsync ( Context . User , "Betroll Gamble" , ( int ) ( amount * NadekoBot . BotConfig . Betroll91Multiplier ) , false ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
}
else
{
2017-01-12 19:19:56 +00:00
str + = $"👑 Congratulations! You won {amount * NadekoBot.BotConfig.Betroll100Multiplier}{CurrencySign} for rolling **100**. 👑" ;
2017-01-13 12:32:48 +00:00
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]
2016-12-16 18:43:57 +00:00
public async Task Leaderboard ( )
2016-08-29 22:00:19 +00:00
{
2016-12-15 18:28:08 +00:00
IEnumerable < Currency > richest = new List < Currency > ( ) ;
2016-08-29 22:00:19 +00:00
using ( var uow = DbHandler . UnitOfWork ( ) )
{
richest = uow . Currency . GetTopRichest ( 10 ) ;
}
if ( ! richest . Any ( ) )
return ;
2016-12-16 21:44:26 +00:00
await Context . Channel . SendMessageAsync (
2016-08-29 22:00:19 +00:00
richest . Aggregate ( new StringBuilder (
$ @ "```xl
2016-10-03 02:19:14 +00:00
┏ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ┳ ━ ━ ━ ━ ━ ━ ━ ━ ┓
┃ Id ┃ $ $ $ ┃
"),
( cur , cs ) = > cur . AppendLine ( $ @ "┣━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━┫
2016-12-17 04:09:04 +00:00
┃ { ( Context . Guild . GetUserAsync ( cs . UserId ) . GetAwaiter ( ) . GetResult ( ) ? . Username ? . TrimTo ( 18 , true ) ? ? cs . UserId . ToString ( ) ) , - 20 } ┃ { cs . Amount , 6 } ┃ ")
2016-10-03 02:19:14 +00:00
) . ToString ( ) + "┗━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━┛```" ) . 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
}