[Pupper] Delete timer, exc on loop

This commit is contained in:
aikaterna
2020-05-25 10:18:11 -07:00
committed by GitHub
parent 64a05597bd
commit cb30ed5bb0

View File

@@ -1,9 +1,14 @@
import asyncio import asyncio
import datetime import datetime
import discord import discord
import logging
import random import random
from redbot.core import commands, checks, Config, bank from redbot.core import commands, checks, Config, bank
from redbot.core.utils.chat_formatting import box, humanize_list from redbot.core.errors import BalanceTooHigh
from redbot.core.utils.chat_formatting import box, humanize_list, pagify
log = logging.getLogger("red.aikaterna.pupper")
class Pupper(commands.Cog): class Pupper(commands.Cog):
@@ -20,6 +25,7 @@ class Pupper(commands.Cog):
"hello_msg": "Hi! Can someone pet me?", "hello_msg": "Hi! Can someone pet me?",
"last_pet": "2019-08-01 00:00:00.000001", "last_pet": "2019-08-01 00:00:00.000001",
"toggle": False, "toggle": False,
"delete_after": 10,
} }
self.config.register_guild(**default_guild) self.config.register_guild(**default_guild)
@@ -29,14 +35,59 @@ class Pupper(commands.Cog):
@commands.group() @commands.group()
async def pets(self, ctx): async def pets(self, ctx):
"""Manage your pet.""" """Manage your pet."""
pass if ctx.invoked_subcommand is None:
guild_data = await self.config.guild(ctx.guild).all()
if not guild_data["channel"]:
channel_names = ["No channels set."]
else:
channel_names = []
for channel_id in guild_data["channel"]:
channel_obj = self.bot.get_channel(channel_id)
channel_names.append(channel_obj.name)
space = "\N{EN SPACE}"
toggle = "Active" if guild_data["toggle"] else "Inactive"
delete_after = (
"No deletion" if not guild_data["delete_after"] else guild_data["delete_after"]
)
msg = f"[Channels]: {humanize_list(channel_names)}\n"
msg += f"[Cooldown]: {guild_data['cooldown']} seconds\n"
msg += f"[Credit range]: {guild_data['credits'][0]} - {guild_data['credits'][1]} credits\n"
msg += f"[Delete after]: {delete_after}\n"
msg += f"[Toggle]: {toggle}\n"
msg += f"{space}\n"
msg += f"[Hello message]: {guild_data['hello_msg']}\n"
msg += f"[Thanks message]: {guild_data['borf_msg']}\n"
for page in pagify(msg, delims=["\n"]):
await ctx.send(box(page, lang="ini"))
@pets.command() @pets.command()
async def toggle(self, ctx): async def toggle(self, ctx):
"""Toggle pets on the server.""" """Toggle pets on the server."""
toggle = await self.config.guild(ctx.guild).toggle() toggle = await self.config.guild(ctx.guild).toggle()
msg = f"Pets active: {not toggle}.\n"
await self.config.guild(ctx.guild).toggle.set(not toggle) await self.config.guild(ctx.guild).toggle.set(not toggle)
await ctx.send(f"The pet is now {'' if not toggle else 'in'}active.")
@pets.command()
async def delete(self, ctx, amount: int = 0):
"""
Set how long to wait before deleting the thanks message.
To leave the thanks message with no deletion, use 0 as the amount.
10 is the default.
Max is 5 minutes (300).
"""
if amount < 0:
return await ctx.send("Use a positive number.")
if 1 <= amount <= 5:
return await ctx.send("Use a slightly larger number, greater than 5.")
if amount > 300:
return await ctx.send("Use a smaller number, less than or equal to 300.")
set_amount = None if amount == 0 else amount
await self.config.guild(ctx.guild).delete_after.set(set_amount)
msg = f"Timer set to {amount}." if set_amount else "Delete timer has been turned off."
await ctx.send(msg) await ctx.send(msg)
@pets.command() @pets.command()
@@ -95,13 +146,13 @@ class Pupper(commands.Cog):
"""Channel management for pet appearance.""" """Channel management for pet appearance."""
await ctx.send_help() await ctx.send_help()
channel_list = await self.config.guild(ctx.guild).channel() channel_list = await self.config.guild(ctx.guild).channel()
channel_msg = "Petting Channels:\n" channel_msg = "[Petting Channels]:\n"
if not channel_list: if not channel_list:
channel_msg += "None." channel_msg += "None."
for chan in channel_list: for chan in channel_list:
channel_obj = self.bot.get_channel(chan) channel_obj = self.bot.get_channel(chan)
channel_msg += f"{channel_obj.name}\n" channel_msg += f"{channel_obj.name}\n"
await ctx.send(box(channel_msg)) await ctx.send(box(channel_msg, lang="ini"))
@channel.command() @channel.command()
async def add(self, ctx, channel: discord.TextChannel): async def add(self, ctx, channel: discord.TextChannel):
@@ -172,54 +223,68 @@ class Pupper(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_message(self, message): async def on_message(self, message):
if isinstance(message.channel, discord.abc.PrivateChannel): try:
return if isinstance(message.channel, discord.abc.PrivateChannel):
if message.author.bot: return
return if message.author.bot:
guild_data = await self.config.guild(message.guild).all() return
if not guild_data["toggle"]: guild_data = await self.config.guild(message.guild).all()
return if not guild_data["toggle"]:
if not guild_data["channel"]: return
return if not guild_data["channel"]:
self.pets.setdefault(message.guild.id, False) return
if self.pets[message.guild.id]: self.pets.setdefault(message.guild.id, False)
return if self.pets[message.guild.id]:
return
last_time = datetime.datetime.strptime(str(guild_data["last_pet"]), "%Y-%m-%d %H:%M:%S.%f") last_time = datetime.datetime.strptime(
now = datetime.datetime.now(datetime.timezone.utc) str(guild_data["last_pet"]), "%Y-%m-%d %H:%M:%S.%f"
now = now.replace(tzinfo=None) )
if ( now = datetime.datetime.now(datetime.timezone.utc)
int((now - last_time).total_seconds()) now = now.replace(tzinfo=None)
> await self.config.guild(message.guild).cooldown() if (
): int((now - last_time).total_seconds())
self._pet_lock(message.guild.id, True) > await self.config.guild(message.guild).cooldown()
rando_channel = random.choice(guild_data["channel"]) ):
await asyncio.sleep(random.randint(60, 480)) self._pet_lock(message.guild.id, True)
rando_channel_obj = self.bot.get_channel(rando_channel) rando_channel = random.choice(guild_data["channel"])
borf_msg = await rando_channel_obj.send(guild_data["hello_msg"]) await asyncio.sleep(random.randint(60, 480))
pets = "👋" rando_channel_obj = self.bot.get_channel(rando_channel)
pets_action = {"veryfastpats": "👋"} borf_msg = await rando_channel_obj.send(guild_data["hello_msg"])
pets = "👋"
pets_action = {"veryfastpats": "👋"}
def check(r, u): def check(r, u):
return r.message.id == borf_msg.id and any(e in str(r.emoji) for e in pets) return r.message.id == borf_msg.id and any(e in str(r.emoji) for e in pets)
try: try:
r, u = await self.bot.wait_for("reaction_add", check=check, timeout=300.0) r, u = await self.bot.wait_for("reaction_add", check=check, timeout=300.0)
except asyncio.TimeoutError: except asyncio.TimeoutError:
return await borf_msg.delete() return await borf_msg.delete()
reacts = {v: k for k, v in pets_action.items()} reacts = {v: k for k, v in pets_action.items()}
react = reacts[r.emoji] react = reacts[r.emoji]
if react == "veryfastpats": if react == "veryfastpats":
await borf_msg.delete() await borf_msg.delete()
deposit = random.randint(guild_data["credits"][0], guild_data["credits"][1]) deposit = random.randint(guild_data["credits"][0], guild_data["credits"][1])
await bank.deposit_credits(u, deposit) try:
credits_name = await bank.get_currency_name(message.guild) large_bank = False
await rando_channel_obj.send( await bank.deposit_credits(u, deposit)
content=f"{guild_data['borf_msg']} (`+{deposit}` {credits_name})", except BalanceTooHigh as e:
delete_after=10, large_bank = True
) await bank.set_balance(u, e.max_balance)
else: credits_name = await bank.get_currency_name(message.guild)
pass msg = (
self._pet_lock(message.guild.id, False) f"{guild_data['borf_msg']} (`+{deposit}` {credits_name})"
await self.config.guild(message.guild).last_pet.set(str(now)) if not large_bank
else guild_data["borf_msg"]
)
await rando_channel_obj.send(
content=msg, delete_after=guild_data["delete_after"]
)
else:
pass
self._pet_lock(message.guild.id, False)
await self.config.guild(message.guild).last_pet.set(str(now))
except Exception:
log.error("Error in pupper loop.", exc_info=True)