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-20 20:35:27 +00:00
using Discord.WebSocket ;
2016-08-29 22:00:19 +00:00
using NadekoBot.Services.Database.Models ;
using System.Collections.Generic ;
2016-11-26 09:54:33 +00:00
using NadekoBot.Services.Database ;
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 ; }
2016-12-08 17:35:34 +00:00
public Gambling ( ) : base ( )
2016-03-25 11:48:22 +00:00
{
2016-08-28 00:46:36 +00:00
using ( var uow = DbHandler . UnitOfWork ( ) )
{
var conf = uow . BotConfig . GetOrCreate ( ) ;
CurrencyName = conf . CurrencyName ;
CurrencySign = conf . CurrencySign ;
CurrencyPluralName = conf . CurrencyPluralName ;
}
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-08-28 02:33:09 +00:00
public async Task Raffle ( IUserMessage umsg , [ Remainder ] IRole role = null )
2016-08-20 14:10:37 +00:00
{
2016-08-28 02:33:09 +00:00
var channel = ( ITextChannel ) umsg . Channel ;
2016-08-20 14:10:37 +00:00
role = role ? ? channel . Guild . EveryoneRole ;
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-11 16:18:25 +00:00
await 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-08-29 22:00:19 +00:00
public async Task Cash ( IUserMessage umsg , [ Remainder ] IUser user = null )
{
2016-09-30 17:43:48 +00:00
var channel = umsg . Channel ;
2016-08-20 14:33:18 +00:00
2016-08-29 22:00:19 +00:00
user = user ? ? umsg . Author ;
2016-08-20 14:33:18 +00:00
2016-12-11 16:18:25 +00:00
await 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-10-08 18:24:34 +00:00
public async Task Cash ( IUserMessage umsg , ulong userId )
{
var channel = umsg . Channel ;
2016-12-11 16:18:25 +00:00
await 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-09-08 21:47:08 +00:00
public async Task Give ( IUserMessage umsg , long amount , [ Remainder ] IGuildUser receiver )
2016-08-29 22:00:19 +00:00
{
var channel = ( ITextChannel ) umsg . Channel ;
2016-10-15 00:58:15 +00:00
if ( amount < = 0 | | umsg . Author . Id = = receiver . Id )
2016-08-29 22:00:19 +00:00
return ;
2016-09-08 21:47:08 +00:00
var success = await CurrencyHandler . RemoveCurrencyAsync ( ( IGuildUser ) umsg . Author , $"Gift to {receiver.Username} ({receiver.Id})." , amount , true ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
if ( ! success )
{
2016-12-11 16:18:25 +00:00
await channel . SendErrorAsync ( $"{umsg.Author.Mention} You don't have enough {Gambling.CurrencyPluralName}." ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
return ;
}
2016-09-08 21:47:08 +00:00
await CurrencyHandler . AddCurrencyAsync ( receiver , $"Gift from {umsg.Author.Username} ({umsg.Author.Id})." , amount , true ) . ConfigureAwait ( false ) ;
2016-12-11 16:18:25 +00:00
await channel . SendConfirmAsync ( $"{umsg.Author.Mention} successfully sent {amount} {(amount == 1 ? Gambling.CurrencyName : Gambling.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)]
public Task Award ( IUserMessage umsg , int amount , [ Remainder ] IGuildUser usr ) = >
2016-09-14 12:23:09 +00:00
Award ( umsg , 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-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(1)]
public async Task Award ( IUserMessage umsg , int amount , ulong usrId )
2016-09-14 12:23:09 +00:00
{
var channel = ( ITextChannel ) umsg . Channel ;
2016-08-20 14:33:18 +00:00
2016-09-14 12:23:09 +00:00
if ( amount < = 0 )
return ;
2016-08-20 14:33:18 +00:00
2016-10-24 11:59:16 +00:00
await CurrencyHandler . AddCurrencyAsync ( usrId , $"Awarded by bot owner. ({umsg.Author.Username}/{umsg.Author.Id})" , amount ) . ConfigureAwait ( false ) ;
2016-08-20 14:33:18 +00:00
2016-12-11 16:18:25 +00:00
await channel . SendConfirmAsync ( $"{umsg.Author.Mention} successfully awarded {amount} {(amount == 1 ? Gambling.CurrencyName : Gambling.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)]
public async Task Award ( IUserMessage umsg , int amount , [ Remainder ] IRole role )
{
var channel = ( ITextChannel ) umsg . Channel ;
var users = channel . Guild . GetUsers ( )
. Where ( u = > u . Roles . Contains ( role ) )
. ToList ( ) ;
await Task . WhenAll ( users . Select ( u = > CurrencyHandler . AddCurrencyAsync ( u . Id ,
$"Awarded by bot owner to **{role.Name}** role. ({umsg.Author.Username}/{umsg.Author.Id})" ,
amount ) ) )
. ConfigureAwait ( false ) ;
2016-12-11 16:18:25 +00:00
await channel . SendConfirmAsync ( $"Awarded `{amount}` {Gambling.CurrencyPluralName} to `{users.Count}` users from `{role.Name}` role." )
2016-10-24 11:59:16 +00:00
. ConfigureAwait ( false ) ;
}
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
[RequireContext(ContextType.Guild)]
[OwnerOnly]
public async Task Take ( IUserMessage umsg , long amount , [ Remainder ] IGuildUser user )
{
var channel = ( ITextChannel ) umsg . Channel ;
if ( amount < = 0 )
return ;
2016-10-08 18:24:34 +00:00
if ( await CurrencyHandler . RemoveCurrencyAsync ( user , $"Taken by bot owner.({umsg.Author.Username}/{umsg.Author.Id})" , amount , true ) . ConfigureAwait ( false ) )
2016-12-11 16:18:25 +00:00
await channel . SendConfirmAsync ( $"{umsg.Author.Mention} successfully took {amount} {(amount == 1? Gambling.CurrencyName : Gambling.CurrencyPluralName)} from {user}!" ) . ConfigureAwait ( false ) ;
2016-10-08 18:24:34 +00:00
else
2016-12-11 16:18:25 +00:00
await channel . SendErrorAsync ( $"{umsg.Author.Mention} was unable to take {amount} {(amount == 1 ? Gambling.CurrencyName : Gambling.CurrencyPluralName)} from {user} because the user doesn't have that much {Gambling.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
[RequireContext(ContextType.Guild)]
[OwnerOnly]
public async Task Take ( IUserMessage umsg , long amount , [ Remainder ] ulong usrId )
{
var channel = ( ITextChannel ) umsg . Channel ;
if ( amount < = 0 )
return ;
2016-10-08 18:24:34 +00:00
if ( await CurrencyHandler . RemoveCurrencyAsync ( usrId , $"Taken by bot owner.({umsg.Author.Username}/{umsg.Author.Id})" , amount ) . ConfigureAwait ( false ) )
2016-12-11 16:18:25 +00:00
await channel . SendConfirmAsync ( $"{umsg.Author.Mention} successfully took {amount} {(amount == 1 ? Gambling.CurrencyName : Gambling.CurrencyPluralName)} from <@{usrId}>!" ) . ConfigureAwait ( false ) ;
2016-10-08 18:24:34 +00:00
else
2016-12-11 16:18:25 +00:00
await channel . SendErrorAsync ( $"{umsg.Author.Mention} was unable to take {amount} {(amount == 1 ? Gambling.CurrencyName : Gambling.CurrencyPluralName)} from `{usrId}` because the user doesn't have that much {Gambling.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-08-29 22:00:19 +00:00
[RequireContext(ContextType.Guild)]
public async Task BetRoll ( IUserMessage umsg , long amount )
{
var channel = ( ITextChannel ) umsg . Channel ;
2016-08-20 14:33:18 +00:00
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
var guildUser = ( IGuildUser ) umsg . Author ;
2016-03-25 11:48:22 +00:00
2016-08-29 22:00:19 +00:00
long userFlowers ;
using ( var uow = DbHandler . UnitOfWork ( ) )
{
2016-09-08 22:22:55 +00:00
userFlowers = uow . Currency . GetOrCreate ( umsg . Author . 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 )
{
2016-12-11 16:18:25 +00:00
await channel . SendErrorAsync ( $"{guildUser.Mention} You don't have enough {Gambling.CurrencyPluralName}. You only have {userFlowers}{Gambling.CurrencySign}." ) . ConfigureAwait ( false ) ;
2016-08-29 22:00:19 +00:00
return ;
}
await CurrencyHandler . RemoveCurrencyAsync ( guildUser , "Betroll Gamble" , amount , false ) . ConfigureAwait ( false ) ;
2016-09-10 19:40:25 +00:00
var rng = new NadekoRandom ( ) . Next ( 0 , 101 ) ;
2016-08-29 22:00:19 +00:00
var str = $"{guildUser.Mention} `You rolled {rng}.` " ;
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
{
str + = $"Congratulations! You won {amount * 2}{Gambling.CurrencySign} for rolling above 66" ;
await CurrencyHandler . AddCurrencyAsync ( guildUser , "Betroll Gamble" , amount * 2 , false ) . ConfigureAwait ( false ) ;
}
else if ( rng < 100 )
{
str + = $"Congratulations! You won {amount * 3}{Gambling.CurrencySign} for rolling above 90." ;
await CurrencyHandler . AddCurrencyAsync ( guildUser , "Betroll Gamble" , amount * 3 , false ) . ConfigureAwait ( false ) ;
}
else
{
str + = $"👑 Congratulations! You won {amount * 10}{Gambling.CurrencySign} for rolling **100**. 👑" ;
await CurrencyHandler . AddCurrencyAsync ( guildUser , "Betroll Gamble" , amount * 10 , false ) . ConfigureAwait ( false ) ;
}
2016-12-11 16:18:25 +00:00
await 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-08-29 22:00:19 +00:00
[RequireContext(ContextType.Guild)]
public async Task Leaderboard ( IUserMessage umsg )
{
var channel = ( ITextChannel ) umsg . Channel ;
IEnumerable < Currency > richest ;
using ( var uow = DbHandler . UnitOfWork ( ) )
{
richest = uow . Currency . GetTopRichest ( 10 ) ;
}
if ( ! richest . Any ( ) )
return ;
await channel . SendMessageAsync (
richest . Aggregate ( new StringBuilder (
$ @ "```xl
2016-10-03 02:19:14 +00:00
┏ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ┳ ━ ━ ━ ━ ━ ━ ━ ━ ┓
┃ Id ┃ $ $ $ ┃
"),
( cur , cs ) = > cur . AppendLine ( $ @ "┣━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━┫
┃ { ( channel . Guild . GetUser ( cs . UserId ) ? . Username . TrimTo ( 18 , true ) ? ? cs . UserId . ToString ( ) ) , - 20 } ┃ { cs . Amount , 6 } ┃ ")
) . 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
}