2019-09-04 12:48:49 -07:00
|
|
|
import random
|
2019-08-31 20:28:21 -07:00
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
|
|
|
import asyncio
|
|
|
|
|
2020-01-26 22:23:17 -08:00
|
|
|
async def take_vote(ctx, question:str, vote_time, min_voters):
|
2019-08-31 20:28:21 -07:00
|
|
|
|
|
|
|
"""
|
|
|
|
take_vote(ctx, question:str) - Collects votes
|
|
|
|
ctx: pass from command function
|
|
|
|
question: what to ask
|
|
|
|
|
|
|
|
returns [<all who want>, <all who don't want>].
|
|
|
|
It's up to the context/use case to decide how these should be used.
|
|
|
|
"""
|
2020-01-26 22:23:17 -08:00
|
|
|
|
2019-08-31 20:28:21 -07:00
|
|
|
embed_title="NEW VOTE"
|
|
|
|
votey_message = await ctx.send(
|
|
|
|
embed=discord.Embed(
|
|
|
|
type="rich",
|
|
|
|
title=embed_title,
|
|
|
|
# description="{}\n\n✅ - Yes\n\n❌ - No".format(question)
|
|
|
|
description="{0}\n\n✅ - Yes\n❌ - No\nMinimum {1} votes required.".format(question, min_voters)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
await votey_message.add_reaction('✅')
|
|
|
|
await votey_message.add_reaction('❌')
|
|
|
|
|
2020-01-26 22:23:17 -08:00
|
|
|
# TODO: Short-circuit eval loop, old stuff temp commented
|
|
|
|
# await asyncio.sleep(vote_time)
|
2019-08-31 20:28:21 -07:00
|
|
|
|
|
|
|
|
|
|
|
passed = False
|
2020-01-26 22:23:17 -08:00
|
|
|
curr_time = 0
|
|
|
|
while curr_time < vote_time:
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
all_in_favor = 0
|
|
|
|
not_in_favor = 0
|
|
|
|
finished_votey = await votey_message.channel.fetch_message(votey_message.id)
|
|
|
|
for reaction in finished_votey.reactions:
|
|
|
|
if str(reaction.emoji) == '✅':
|
|
|
|
all_in_favor += reaction.count-1 # don't include bot's reaction
|
|
|
|
elif str(reaction.emoji) == '❌':
|
|
|
|
not_in_favor += reaction.count-1
|
|
|
|
|
|
|
|
print("check ({0} ✅, {1} ❌)".format(all_in_favor, not_in_favor))
|
|
|
|
if all_in_favor > not_in_favor and all_in_favor >= min_voters:
|
|
|
|
passed = True
|
|
|
|
break
|
|
|
|
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
curr_time += 5
|
|
|
|
|
|
|
|
question += "\nVERDICT: **Vote passed!**" if passed else "\nVERDICT: **Vote failed!**"
|
2019-08-31 20:28:21 -07:00
|
|
|
|
|
|
|
await votey_message.edit(embed=discord.Embed(
|
|
|
|
type="rich",
|
|
|
|
title=embed_title,
|
|
|
|
description=question
|
|
|
|
))
|
|
|
|
return passed
|
|
|
|
|
|
|
|
async def improper_usage(ctx):
|
2019-09-04 12:48:49 -07:00
|
|
|
await ctx.send("Improper command usage! See `>>help` for more.")
|
|
|
|
|
|
|
|
def imgfun(msg:str, img_url:str):
|
|
|
|
return discord.Embed(
|
|
|
|
title=msg, color=random.randint(0, 16777215)
|
|
|
|
).set_image(url=img_url)
|