Skip to content

Instantly share code, notes, and snippets.

@bensonchow123
Created July 6, 2022 06:19
Show Gist options
  • Save bensonchow123/2acfdba8d722b1b5643638aec1836f93 to your computer and use it in GitHub Desktop.
Save bensonchow123/2acfdba8d722b1b5643638aec1836f93 to your computer and use it in GitHub Desktop.
view_class
class AdvertisementConfirmButton(ui.View):
def __init__(self, author, skybies_cost, num_messages):
super().__init__(timeout=10)
self.reply = None
self.author = author
self.skybies_cost = skybies_cost
self.num_messages = num_messages
async def on_timeout(self):
if self.reply.reference:
replied_to = self.reply.reference.resolved
await replied_to.delete()
await self.reply.delete()
async def interaction_check(self, interaction):
if self.author.id != interaction.user.id:
await interaction.response.send_message(
"This confirmation is not for you, you cannot confirm other's advertisement"
)
async def _status_embed(self, status, author):
if status == "accepted":
description = (
f"Message is sent!\n"
f"This is your {self.num_messages} message in the past 7 days,\n"
f"so {self.skybies_cost} skybies is deduced from your skybie count"
)
else:
description = (
f"Message is deleted, as you canceled your advertisement"
)
colour = 0x4bb543 if status == "accepted" else 0xff0033
status_embed = Embed(
description=description,
colour=colour
).set_author(
name=author.display_name,
icon_url=author.avatar.url
).set_thumbnail(
url="https://cdn.discordapp.com/attachments/850019796014858280/992247149767164026/promotion.png"
)
return status_embed
@ui.button(label="Confirm", style=ButtonStyle.success)
async def confirm(self, button: ui.Button, interaction: Interaction):
skybies = interaction.client.get_cog("Skybies")
skybies._take_skybies(interaction.user, self.skybies_cost)
status_embed = await self._status_embed("accepted", interaction.user)
await interaction.response.send_message(
embed=status_embed,
ephemeral=True
)
await interaction.message.delete()
@ui.button(label="Cancel", style=ButtonStyle.danger,)
async def decline(self, button: ui.Button, interaction: Interaction):
if interaction.message.reference:
await interaction.message.reference.resolved.delete()
status_embed = await self._status_embed("canceled", interaction.user)
await interaction.response.send_message(
embed=status_embed,
ephemeral=True
)
await interaction.message.delete()
class SelfPromotion(commands.Cog):
def __init__(self, client):
self.client = client
sync def _find_weekly_restart_date(self):
last_restart = restart_date_db.find_one({"type": "weekly"})["last_restart"]
last_restart_date = datetime.strptime(last_restart, "%S:%M:%H:%d:%m:%Y:%z")
return last_restart_date
async def _determine_message_amount_in_last_7_days(self, message):
last_restart_date = await self._find_weekly_restart_date()
messages_in_last_7_days = await message.channel.history(after=last_restart_date).flatten()
messages_send_last_7_days_by_author = []
for last_7_day_message in messages_in_last_7_days:
if message.author.id == last_7_day_message.author.id:
messages_send_last_7_days_by_author.append(last_7_day_message)
return len(messages_send_last_7_days_by_author)
async def _evaluate_cost(self, message):
new_message_count = await self._determine_message_amount_in_last_7_days(message) + 1
cost = round(log2((new_message_count + 1) * 0.8 + 0.3))
return cost, new_message_count
@commands.Cog.listener("on_message")
async def message_cost_system(self, message):
if message.author.bot:
return
if message.channel != self.self_promotion_channel:
return
cost, new_message_count = await self._evaluate_cost(message)
skybies_aquired, _ = await self.skybies._get_skybies(message.author)
if (skybies_aquired - cost) >= 0:
confirm_embed = Embed(
description="Testing good"
)
confirm_button = AdvertisementConfirmButton(message.author, cost, new_message_count)
confirm_button.reply = await message.reply(
embed=confirm_embed,
view=AdvertisementConfirmButton
)
return
await message.reply(
f"You need {cost} skybies to send this ad,\n"
f"but you only have {skybies_aquired}"
)
@commands.Cog.listener()
async def on_ready(self):
self.guild = self.client.get_guild(844231449014960160)
self.self_promotion_channel = utils.get(self.guild.text_channels, name="📺self-promotion")
self.sky_promoter = utils.get(self.guild.roles, name="📢Sky Promoter📢")
self.skybies = self.client.get_cog("Skybies")
def setup(client):
client.add_cog(SelfPromotion(client))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment