Invite tracker
This commit is contained in:
parent
f01fc28a0c
commit
64a288bf21
17
Bot/Bot.fs
17
Bot/Bot.fs
@ -58,6 +58,18 @@ stealCommands.RegisterCommands<StealGame>(guild);
|
||||
hackerBattleBot.add_ComponentInteractionCreated(AsyncEventHandler(HackerBattle.handleButtonEvent))
|
||||
storeBot.add_ComponentInteractionCreated(AsyncEventHandler(Store.handleStoreEvents))
|
||||
stealBot.add_ComponentInteractionCreated(AsyncEventHandler(Thief.handleStealButton))
|
||||
hackerBattleBot.add_InviteCreated((fun client args ->
|
||||
task {
|
||||
do! InviteTracker.createInvite args.Invite.Inviter.Id args.Invite.Code |> Async.Ignore
|
||||
}))
|
||||
hackerBattleBot.add_GuildMemberAdded(AsyncEventHandler(fun client ea ->
|
||||
task {
|
||||
let! guildInvites = ea.Guild.GetInvitesAsync()
|
||||
let! cachedInvites = InviteTracker.getInvites()
|
||||
for invite in guildInvites do
|
||||
if invite.Uses < (snd cachedInvites.[invite.Code]) then
|
||||
do! InviteTracker.addInvitedUser ea.Member.Id invite.Code |> Async.Ignore
|
||||
}))
|
||||
|
||||
let asdf (_ : DiscordClient) (event : DSharpPlus.EventArgs.InteractionCreateEventArgs) =
|
||||
async {
|
||||
@ -86,6 +98,11 @@ GuildEnvironment.botUserArmory <- Some storeBot.CurrentUser
|
||||
|
||||
stealBot.ConnectAsync() |> Async.AwaitTask |> Async.RunSynchronously
|
||||
|
||||
//let channel = hackerBattleBot.GetChannelAsync(1234uL) |> Async.AwaitTask |> Async.RunSynchronously
|
||||
//channel.invi
|
||||
|
||||
|
||||
|
||||
//async {
|
||||
// let! user = hackerBattleBot.GetUserAsync(GuildEnvironment.botIdHackerBattle) |> Async.AwaitTask
|
||||
// if user <> null then
|
||||
|
@ -18,6 +18,7 @@
|
||||
<Compile Include="GameHelpers.fs" />
|
||||
<Compile Include="DbService.fs" />
|
||||
<Compile Include="PlayerInteractions.fs" />
|
||||
<Compile Include="InviteTracker.fs" />
|
||||
<Compile Include="XP.fs" />
|
||||
<Compile Include="Embeds.fs" />
|
||||
<Compile Include="Games\SlotMachine.fs" />
|
||||
|
58
Bot/InviteTracker.fs
Normal file
58
Bot/InviteTracker.fs
Normal file
@ -0,0 +1,58 @@
|
||||
module Degenz.InviteTracker
|
||||
|
||||
|
||||
open System
|
||||
open Npgsql.FSharp
|
||||
|
||||
let connStr = GuildEnvironment.connectionString
|
||||
|
||||
type Invite = {
|
||||
Code : string
|
||||
Inviter : uint64
|
||||
Count : int
|
||||
}
|
||||
|
||||
let getInvites () = async {
|
||||
let! invites =
|
||||
connStr
|
||||
|> Sql.connect
|
||||
|> Sql.query "SELECT code, inviter, count FROM invite"
|
||||
|> Sql.executeAsync (fun read -> {
|
||||
Code = read.string "code"
|
||||
Inviter = read.string "inviter" |> uint64
|
||||
Count = read.int "count"
|
||||
})
|
||||
|> Async.AwaitTask
|
||||
return
|
||||
invites
|
||||
|> List.map (fun inv -> (inv.Code , (inv.Inviter , inv.Count)))
|
||||
|> Map.ofList
|
||||
}
|
||||
|
||||
let createInvite inviter code =
|
||||
connStr
|
||||
|> Sql.connect
|
||||
|> Sql.parameters [ "code" , Sql.string code ; "inviter" , Sql.string (string inviter) ]
|
||||
|> Sql.query "INSERT INTO invite (code, inviter) VALUES (@code, @inviter)"
|
||||
|> Sql.executeNonQueryAsync
|
||||
|> Async.AwaitTask
|
||||
|
||||
let addInvitedUser did code =
|
||||
connStr
|
||||
|> Sql.connect
|
||||
|> Sql.executeTransactionAsync [
|
||||
"""
|
||||
WITH invite AS (SELECT id FROM invite WHERE code = @code)
|
||||
INSERT INTO invited_user (discord_id, invite_id) SELECT @discord_id, invite.id FROM invite;
|
||||
""" , [ [ "@discord_id" , Sql.string (string did) ] ; [ "@code" , Sql.string code ] ]
|
||||
"UPDATE invite SET count = count + 1 WHERE code = @code" , [ [ "@code" , Sql.string code ] ]
|
||||
]
|
||||
|> Async.AwaitTask
|
||||
|
||||
let getInviteAttributions user =
|
||||
connStr
|
||||
|> Sql.connect
|
||||
|> Sql.parameters [ "did" , Sql.string (string user.DiscordId) ]
|
||||
|> Sql.query "SELECT sum(count) AS total FROM invite WHERE inviter = @did"
|
||||
|> Sql.executeAsync (fun read -> read.int "total")
|
||||
|> Async.AwaitTask
|
Loading…
x
Reference in New Issue
Block a user