mirror of
				https://github.com/keanuplayz/TravBot-v3.git
				synced 2024-08-15 02:33:12 +00:00 
			
		
		
		
	Added hacky persistence and bug fixes on eco bet
This commit is contained in:
		
							parent
							
								
									c71406a8d0
								
							
						
					
					
						commit
						1351f3250b
					
				
					 3 changed files with 37 additions and 12 deletions
				
			
		|  | @ -2,6 +2,7 @@ import Command from "../../../core/command"; | ||||||
| import $ from "../../../core/lib"; | import $ from "../../../core/lib"; | ||||||
| import {Storage} from "../../../core/structures"; | import {Storage} from "../../../core/structures"; | ||||||
| import {isAuthorized, getMoneyEmbed, getSendEmbed, ECO_EMBED_COLOR} from "./eco-utils"; | import {isAuthorized, getMoneyEmbed, getSendEmbed, ECO_EMBED_COLOR} from "./eco-utils"; | ||||||
|  | import {User} from "discord.js"; | ||||||
| 
 | 
 | ||||||
| export const BetCommand = new Command({ | export const BetCommand = new Command({ | ||||||
|     description: "Bet your Mons with other people [TBD]", |     description: "Bet your Mons with other people [TBD]", | ||||||
|  | @ -27,8 +28,8 @@ export const BetCommand = new Command({ | ||||||
|             async run({args, author, channel, guild}): Promise<any> { |             async run({args, author, channel, guild}): Promise<any> { | ||||||
|                 if (isAuthorized(guild, channel)) { |                 if (isAuthorized(guild, channel)) { | ||||||
|                     const sender = Storage.getUser(author.id); |                     const sender = Storage.getUser(author.id); | ||||||
|                     const target = args[0]; |                     const target = args[0] as User; | ||||||
|                     const receiver = Storage.getUser(target); |                     const receiver = Storage.getUser(target.id); | ||||||
|                     const amount = Math.floor(args[1]); |                     const amount = Math.floor(args[1]); | ||||||
| 
 | 
 | ||||||
|                     // handle invalid target
 |                     // handle invalid target
 | ||||||
|  | @ -49,15 +50,15 @@ export const BetCommand = new Command({ | ||||||
|                 } |                 } | ||||||
|             }, |             }, | ||||||
|             any: new Command({ |             any: new Command({ | ||||||
|                 async run({client, args, author, message, channel, guild}): Promise<any> { |                 async run({client, args, author, message, channel, guild, askYesOrNo}): Promise<any> { | ||||||
|                     if (isAuthorized(guild, channel)) { |                     if (isAuthorized(guild, channel)) { | ||||||
|                         // [Pertinence to make configurable on the fly.]
 |                         // [Pertinence to make configurable on the fly.]
 | ||||||
|                         // Lower and upper bounds for bet
 |                         // Lower and upper bounds for bet
 | ||||||
|                         const durationBounds = { min:"1m", max:"1d" }; |                         const durationBounds = { min:"1m", max:"1d" }; | ||||||
| 
 | 
 | ||||||
|                         const sender = Storage.getUser(author.id); |                         const sender = Storage.getUser(author.id); | ||||||
|                         const target = args[0]; |                         const target = args[0] as User; | ||||||
|                         const receiver = Storage.getUser(target); |                         const receiver = Storage.getUser(target.id); | ||||||
|                         const amount = Math.floor(args[1]); |                         const amount = Math.floor(args[1]); | ||||||
|                         const duration = parseDuration(args[2].trim()); |                         const duration = parseDuration(args[2].trim()); | ||||||
| 
 | 
 | ||||||
|  | @ -94,16 +95,22 @@ export const BetCommand = new Command({ | ||||||
|                             // Remove amount money from both parts at the start to avoid duplication of money.
 |                             // Remove amount money from both parts at the start to avoid duplication of money.
 | ||||||
|                             sender.money -= amount; |                             sender.money -= amount; | ||||||
|                             receiver.money -= amount; |                             receiver.money -= amount; | ||||||
|  |                             // Very hacky solution for persistence but better than no solution, backup returns runs during the bot's setup code.
 | ||||||
|  |                             sender.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled += amount; | ||||||
|  |                             receiver.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled += amount; | ||||||
|                             Storage.save(); |                             Storage.save(); | ||||||
| 
 | 
 | ||||||
|                             // Notify both users.
 |                             // Notify both users.
 | ||||||
|                             await channel.send(`<@${target.id}> has taken <@${author.id}>'s bet, the bet amount of ${$(amount).pluralise("Mon", "s")} has been deducted from each of them.`); |                             await channel.send(`<@${target.id}> has taken <@${author.id}>'s bet, the bet amount of ${$(amount).pluralise("Mon", "s")} has been deducted from each of them.`); | ||||||
| 
 | 
 | ||||||
|                             // Wait for the duration of the bet. 
 |                             // Wait for the duration of the bet.
 | ||||||
|                             client.setTimeout(async () => { |                             client.setTimeout(async () => { | ||||||
|  |                                 // In debug mode, saving the storage will break the references, so you have to redeclare sender and receiver for it to actually save.
 | ||||||
|  |                                 const sender = Storage.getUser(author.id); | ||||||
|  |                                 const receiver = Storage.getUser(target.id); | ||||||
|                                 // [TODO: when D.JSv13 comes out, inline reply to clean up.]
 |                                 // [TODO: when D.JSv13 comes out, inline reply to clean up.]
 | ||||||
|                                 // When bet is over, give a vote to ask people their thoughts.
 |                                 // When bet is over, give a vote to ask people their thoughts.
 | ||||||
|                                 const voteMsg = await channel.send(`VOTE: do you think that <@${target.id}> has won the bet?\nhttps://discord.com/channels/${guild.id}/${channel.id}/${message.id}`); |                                 const voteMsg = await channel.send(`VOTE: do you think that <@${target.id}> has won the bet?\nhttps://discord.com/channels/${guild!.id}/${channel.id}/${message.id}`); | ||||||
|                                 await voteMsg.react("✅"); |                                 await voteMsg.react("✅"); | ||||||
|                                 await voteMsg.react("❌"); |                                 await voteMsg.react("❌"); | ||||||
| 
 | 
 | ||||||
|  | @ -115,9 +122,11 @@ export const BetCommand = new Command({ | ||||||
|                                     // [Pertinence to make configurable on the fly.]
 |                                     // [Pertinence to make configurable on the fly.]
 | ||||||
|                                     { time: parseDuration("2m") } |                                     { time: parseDuration("2m") } | ||||||
|                                 ).then(reactions => { |                                 ).then(reactions => { | ||||||
|                                     // Count votes 
 |                                     // Count votes
 | ||||||
|                                     const ok = reactions.filter(reaction => reaction.emoji.name === "✅").size; |                                     const okReaction = reactions.get("✅"); | ||||||
|                                     const no = reactions.filter(reaction => reaction.emoji.name === "❌").size; |                                     const noReaction = reactions.get("❌"); | ||||||
|  |                                     const ok = okReaction ? (okReaction.count ?? 1) - 1 : 0; | ||||||
|  |                                     const no = noReaction ? (noReaction.count ?? 1) - 1 : 0; | ||||||
| 
 | 
 | ||||||
|                                     if (ok > no) { |                                     if (ok > no) { | ||||||
|                                         receiver.money += amount * 2; |                                         receiver.money += amount * 2; | ||||||
|  | @ -132,11 +141,13 @@ export const BetCommand = new Command({ | ||||||
|                                         receiver.money += amount; |                                         receiver.money += amount; | ||||||
|                                         channel.send(`By the people's votes, <@${target.id}> couldn't be determined to have won or lost the bet that <@${author.id}> had sent them.`); |                                         channel.send(`By the people's votes, <@${target.id}> couldn't be determined to have won or lost the bet that <@${author.id}> had sent them.`); | ||||||
|                                     } |                                     } | ||||||
|  |                                     sender.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled -= amount; | ||||||
|  |                                     receiver.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled -= amount; | ||||||
|                                     Storage.save(); |                                     Storage.save(); | ||||||
|                                 }); |                                 }); | ||||||
|                             }, duration); |                             }, duration); | ||||||
|                         } |                         } | ||||||
|                         else  |                         else | ||||||
|                             await channel.send(`<@${target.id}> has rejected your bet, <@${author.id}>`); |                             await channel.send(`<@${target.id}> has rejected your bet, <@${author.id}>`); | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|  | @ -26,6 +26,7 @@ class User { | ||||||
|     public lastMonday: number; |     public lastMonday: number; | ||||||
|     public timezone: number | null; // This is for the standard timezone only, not the daylight savings timezone
 |     public timezone: number | null; // This is for the standard timezone only, not the daylight savings timezone
 | ||||||
|     public daylightSavingsRegion: "na" | "eu" | "sh" | null; |     public daylightSavingsRegion: "na" | "eu" | "sh" | null; | ||||||
|  |     public quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled: number; | ||||||
| 
 | 
 | ||||||
|     constructor(data?: GenericJSON) { |     constructor(data?: GenericJSON) { | ||||||
|         this.money = select(data?.money, 0, Number); |         this.money = select(data?.money, 0, Number); | ||||||
|  | @ -35,6 +36,7 @@ class User { | ||||||
|         this.daylightSavingsRegion = /^((na)|(eu)|(sh))$/.test(data?.daylightSavingsRegion) |         this.daylightSavingsRegion = /^((na)|(eu)|(sh))$/.test(data?.daylightSavingsRegion) | ||||||
|             ? data?.daylightSavingsRegion |             ? data?.daylightSavingsRegion | ||||||
|             : null; |             : null; | ||||||
|  |         this.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled = select(data?.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled, 0, Number); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| import Event from "../core/event"; | import Event from "../core/event"; | ||||||
| import {client} from "../index"; | import {client} from "../index"; | ||||||
| import $ from "../core/lib"; | import $ from "../core/lib"; | ||||||
| import {Config} from "../core/structures"; | import {Config, Storage} from "../core/structures"; | ||||||
| import {updateGlobalEmoteRegistry} from "../core/lib"; | import {updateGlobalEmoteRegistry} from "../core/lib"; | ||||||
| 
 | 
 | ||||||
| export default new Event<"ready">({ | export default new Event<"ready">({ | ||||||
|  | @ -14,5 +14,17 @@ export default new Event<"ready">({ | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
|         updateGlobalEmoteRegistry(); |         updateGlobalEmoteRegistry(); | ||||||
|  | 
 | ||||||
|  |         // Run this setup block once to restore eco bet money in case the bot went down. (And I guess search the client for those users to let them know too.)
 | ||||||
|  |         for (const id in Storage.users) { | ||||||
|  |             const user = Storage.users[id]; | ||||||
|  | 
 | ||||||
|  |             if(user.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled > 0) { | ||||||
|  |                 client.users.cache.get(id)?.send(`Because my system either crashed or restarted while you had a pending bet, the total amount of money that you bet, which was \`${user.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled}\`, has been restored.`); | ||||||
|  |                 user.money += user.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled; | ||||||
|  |                 user.quoteUnquoteSoCalledInsuranceForEcoBetIfItEvenCanBeSoCalled = 0; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         Storage.save(); | ||||||
|     } |     } | ||||||
| }); | }); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue