-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathcashdrop.py
240 lines (219 loc) · 8.34 KB
/
cashdrop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import asyncio
import datetime
import operator
import random
import discord
from redbot.core import Config, bank, commands
from redbot.core.utils.predicates import MessagePredicate
class Cashdrop(commands.Cog):
__version__ = "0.2.2"
__author__ = "flare(flare#0001)"
def format_help_for_context(self, ctx):
pre_processed = super().format_help_for_context(ctx)
return f"{pre_processed}\nCog Version: {self.__version__}\nAuthor: {self.__author__}"
def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(self, identifier=95932766180343808)
self.config.register_guild(
active=False,
maths=True,
chance=1,
interval=60,
timestamp=None,
credits_max=550,
credits_min=50,
channel=None,
)
self.cache = {}
asyncio.create_task(self.init_loop())
def random_calc(self):
ops = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
#'/':operator.truediv
}
num1 = random.randint(0, 12)
num2 = random.randint(1, 10)
op = random.choice(list(ops.keys()))
answer = ops.get(op)(num1, num2)
return "What is {} {} {}?\n".format(num1, op, num2), answer
async def init_loop(self):
await self.bot.wait_until_ready()
await self.generate_cache()
# while True:
# await asyncio.sleep(60)
# await self.save()
def cog_unload(self):
self.bg_config_loop.cancel()
asyncio.create_task(self.save_triggers())
async def generate_cache(self):
self.cache = await self.config.all_guilds()
@commands.Cog.listener()
async def on_message(self, message):
if message.author.bot:
return
if message.guild is None:
return
if message.guild.id not in self.cache:
return
if not self.cache[message.guild.id]["active"]:
return
if random.randint(0, 100) > self.cache[message.guild.id]["chance"]:
return
if self.cache[message.guild.id]["timestamp"] is None:
self.cache[message.guild.id]["timestamp"] = datetime.datetime.now(
tz=datetime.timezone.utc
)
if (
datetime.datetime.now(tz=datetime.timezone.utc)
- self.cache[message.guild.id]["timestamp"]
).total_seconds() < self.cache[message.guild.id]["interval"]:
return
self.cache[message.guild.id]["timestamp"] = datetime.datetime.now(tz=datetime.timezone.utc)
if self.cache[message.guild.id]["channel"] is not None:
channel = message.guild.get_channel(self.cache[message.guild.id]["channel"])
if channel is None:
channel = message.channel
else:
channel = message.channel
if self.cache[message.guild.id]["maths"]:
string, answer = self.random_calc()
msg = await channel.send(string)
try:
pred = MessagePredicate.equal_to(str(answer), channel=channel, user=None)
answer_msg: discord.Message = await self.bot.wait_for(
"message", check=pred, timeout=10
)
except asyncio.TimeoutError:
await msg.edit(content="Too slow!")
return
if not pred.result:
creds = random.randint(
self.cache[message.guild.id]["credits_min"],
self.cache[message.guild.id]["credits_max"],
)
await msg.edit(
content=f"Correct! {answer_msg.author.mention} got {creds} {await bank.get_currency_name(guild=message.guild)}!"
)
await bank.deposit_credits(answer_msg.author, creds)
else:
msg = await channel.send(
f"Some {await bank.get_currency_name(guild=message.guild)} have fallen, type `pickup` to pick them up!"
)
pred = MessagePredicate.equal_to("pickup", channel=channel, user=None)
try:
pickup_msg: discord.Message = await self.bot.wait_for(
"message", check=pred, timeout=10
)
except asyncio.TimeoutError:
await msg.edit(content="Too slow!")
return
if not pred.result:
creds = random.randint(
self.cache[message.guild.id]["credits_min"],
self.cache[message.guild.id]["credits_max"],
)
await msg.edit(
content=f"{pickup_msg.author.mention} picked up {creds} {await bank.get_currency_name(guild=message.guild)}!"
)
await bank.deposit_credits(pickup_msg.author, creds)
@commands.group(name="cashdrop", aliases=["cd"])
@commands.guild_only()
@commands.has_permissions(manage_guild=True)
async def _cashdrop(self, ctx):
"""
Manage the cashdrop
"""
@_cashdrop.command(name="toggle")
async def _toggle(self, ctx):
"""
Toggle the cashdrop
"""
guild = ctx.guild
active = await self.config.guild(guild).active()
if active:
await self.config.guild(guild).active.set(False)
await ctx.send("Cashdrop is now disabled")
else:
await self.config.guild(guild).active.set(True)
await ctx.send("Cashdrop is now enabled")
await self.generate_cache()
@_cashdrop.command(name="chance")
async def _chance(self, ctx, chance: int):
"""
Set the chance percent of the cashdrop
"""
if chance < 0 or chance > 100:
await ctx.send("Chance must be between 0 and 100")
return
guild = ctx.guild
await self.config.guild(guild).chance.set(chance)
await ctx.send(f"Chance set to {chance}%")
await self.generate_cache()
@_cashdrop.command(name="interval")
async def _interval(self, ctx, interval: int):
"""
Set the interval in seconds between cashdrops
"""
if interval < 0:
await ctx.send("Interval must be greater than 0")
return
guild = ctx.guild
await self.config.guild(guild).interval.set(interval)
await ctx.send(f"Interval set to {interval} seconds")
await self.generate_cache()
@_cashdrop.command(name="max")
async def _max(self, ctx, max: int):
"""
Set the max credits
"""
if max < 0:
await ctx.send("Max must be greater than 0")
return
mincredits = await self.config.guild(ctx.guild).credits_min()
if max < mincredits:
await ctx.send("Max must be greater than min")
return
guild = ctx.guild
await self.config.guild(guild).credits_max.set(max)
await ctx.send(f"Max credits set to {max}")
await self.generate_cache()
@_cashdrop.command(name="min")
async def _min(self, ctx, min: int):
"""
Set the min credits
"""
if min < 0:
await ctx.send("Min must be greater than 0")
return
maxcredits = await self.config.guild(ctx.guild).credits_max()
if maxcredits < min:
await ctx.send("Min must be less than min")
return
guild = ctx.guild
await self.config.guild(guild).credits_min.set(min)
await ctx.send(f"Min credits set to {min}")
await self.generate_cache()
@_cashdrop.command(name="maths")
async def _maths(self, ctx, toggle: bool):
"""
Toggle maths mode
"""
guild = ctx.guild
if toggle:
await self.config.guild(guild).maths.set(True)
await ctx.send("Maths mode is now enabled")
else:
await self.config.guild(guild).maths.set(False)
await ctx.send("Maths mode is now disabled")
await self.generate_cache()
@_cashdrop.command(name="channel")
async def _channel(self, ctx, channel: discord.TextChannel):
"""
Set the channel for the cashdrop
"""
guild = ctx.guild
await self.config.guild(guild).channel.set(channel.id)
await ctx.send(f"Channel set to {channel.mention}")
await self.generate_cache()