#TMCBMC-6 work 2021-01-18 1h
^TMCBMC-6 State Done
This commit is contained in:
parent
cbc97c8e3c
commit
c945756839
@ -12,15 +12,14 @@ password = open("../sqlPass.txt", 'r').read()
|
||||
scheduler = AsyncIOScheduler({
|
||||
'apscheduler.jobstores.default': {
|
||||
'type': 'sqlalchemy',
|
||||
'url': f'mysql+pymysql://Quentin:{password}@192.168.1.52:5618/discord_bot?charset=utf8mb4'
|
||||
'url': f'mysql+pymysql://Quentin:{password}@192.168.1.52:5618/discord_bot?charset=utf8mb4',
|
||||
},
|
||||
'apscheduler.job_defaults.coalesce': 'true',
|
||||
'apscheduler.timezone': 'America/Chicago',
|
||||
'apscheduler.job_defaults.coalesce': 'True',
|
||||
'apscheduler.timezone': 'America/Chicago'
|
||||
})
|
||||
|
||||
scheduler.start()
|
||||
choices: dict
|
||||
getPollResultsReactions: collections.Iterable
|
||||
|
||||
mcSelf = None
|
||||
|
||||
|
||||
@ -34,10 +33,10 @@ def get_con() -> pymysql.Connection:
|
||||
cursorclass=pymysql.cursors.DictCursor)
|
||||
|
||||
|
||||
async def get_poll_results():
|
||||
async def get_poll_results(choices: dict, pollMessageId: int, channelId: int):
|
||||
print("run")
|
||||
global getPollResultsReactions
|
||||
getPollResultsReactions = (await update_poll_message(mcSelf.pollMessage)).reactions
|
||||
|
||||
getPollResultsReactions = (await McRoll.get_message(mcSelf, pollMessageId, channelId)).reactions
|
||||
for reaction in getPollResultsReactions:
|
||||
async for user in reaction.users():
|
||||
serverIP = reaction_to_serverip(reaction)
|
||||
@ -46,11 +45,11 @@ async def get_poll_results():
|
||||
|
||||
choices[serverIP]['score'] = choices[serverIP]['votes'] * round(
|
||||
2 * math.log10(choices[serverIP]['lastActivated'] + 1) + 1, 2)
|
||||
prune_results()
|
||||
prune_results(choices)
|
||||
if len(choices) >= 3:
|
||||
await McRoll.main_poll_recursion(mcSelf)
|
||||
await McRoll.main_poll_recursion(mcSelf, choices, channelId)
|
||||
else:
|
||||
await McRoll.declare_winner(mcSelf)
|
||||
await McRoll.declare_winner(mcSelf, choices, channelId)
|
||||
|
||||
|
||||
def reaction_to_serverip(reaction: Union[discord.Reaction, str]) -> int:
|
||||
@ -81,11 +80,7 @@ def get_choices() -> dict:
|
||||
return choices
|
||||
|
||||
|
||||
async def update_poll_message(pollmessage: discord.Message) -> discord.Message:
|
||||
return await pollmessage.channel.fetch_message(pollmessage.id)
|
||||
|
||||
|
||||
def pop_lowest():
|
||||
def pop_lowest(choices: dict):
|
||||
lowest = {'score': 10}
|
||||
pop = False
|
||||
for choice in list(choices):
|
||||
@ -96,15 +91,15 @@ def pop_lowest():
|
||||
choices.pop(lowest['serverIP'])
|
||||
|
||||
|
||||
def prune_results():
|
||||
def prune_results(choices: dict):
|
||||
for serverIp, choice in list(choices.items()):
|
||||
if choice['votes'] <= 0:
|
||||
del choices[serverIp]
|
||||
if len(choices) >= 4:
|
||||
pop_lowest()
|
||||
pop_lowest()
|
||||
pop_lowest(choices)
|
||||
pop_lowest(choices)
|
||||
elif len(choices) == 3:
|
||||
pop_lowest()
|
||||
pop_lowest(choices)
|
||||
|
||||
|
||||
class McRoll(commands.Cog):
|
||||
@ -114,33 +109,30 @@ class McRoll(commands.Cog):
|
||||
rolled. When this poll is over all servers with 0 votes will be disregarded. If there are 4 or more servers
|
||||
left the two with the lowest votes will be disregarded. Poll is rerun until there are only 2 servers,
|
||||
these two are the winners. Ties reroll the poll. You can only vote for one server per poll. """
|
||||
self.pollMessage = None
|
||||
self.channel = None
|
||||
self.pollMessageId = None
|
||||
global mcSelf
|
||||
mcSelf = self # This is a bad way of doing things but it works
|
||||
|
||||
@commands.command()
|
||||
async def mc_roll(self, ctx):
|
||||
if ctx.message.author.id != 305589587215122432:
|
||||
return
|
||||
global choices
|
||||
choices = get_choices()
|
||||
self.channel = ctx.channel
|
||||
await self.main_poll_recursion()
|
||||
await self.main_poll_recursion(choices, ctx.channel.id)
|
||||
|
||||
async def main_poll_recursion(self):
|
||||
global choices
|
||||
self.pollMessage = await self.poll(choices, self.channel)
|
||||
choices = choices
|
||||
global mcSelf
|
||||
mcSelf = self
|
||||
async def main_poll_recursion(self, choices, channelId):
|
||||
self.pollMessageId = (await self.poll(choices, channelId)).id
|
||||
scheduler.add_job(get_poll_results, 'date',
|
||||
run_date=(datetime.now() + timedelta(days=1)))
|
||||
run_date=(datetime.now() + timedelta(seconds=10)),
|
||||
args=[choices, self.pollMessageId, channelId],
|
||||
coalesce=True)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_reaction_add(self, reaction, user):
|
||||
if self.pollMessage is None:
|
||||
if self.pollMessageId is None:
|
||||
return
|
||||
reactedUsers = {}
|
||||
if reaction.message.id == self.pollMessage.id and user.id != 533427166193385494:
|
||||
if reaction.message.id == self.pollMessageId and user.id != 533427166193385494:
|
||||
for iReaction in reaction.message.reactions:
|
||||
async for iUser in iReaction.users():
|
||||
if iUser.id != 533427166193385494:
|
||||
@ -148,14 +140,15 @@ class McRoll(commands.Cog):
|
||||
reactedUsers[iUser.id] = False
|
||||
if reactedUsers[iUser.id]:
|
||||
await reaction.remove(user)
|
||||
self.pollMessage = reaction.message
|
||||
self.pollMessageId = reaction.message.id
|
||||
await user.send(
|
||||
"You can only vote for one option, please remove your previous vote to change it.")
|
||||
return
|
||||
self.pollMessage = reaction.message
|
||||
self.pollMessageId = reaction.message.id
|
||||
reactedUsers[iUser.id] = True
|
||||
|
||||
async def poll(self, choices: dict, channel: discord.TextChannel) -> discord.Message:
|
||||
async def poll(self, choices: dict, channel: int) -> discord.Message:
|
||||
channel = await self.client.fetch_channel(channel)
|
||||
embed = discord.Embed(title="Poll", description=self.embedInfo, timestamp=(datetime.utcnow() + timedelta(days=1)))
|
||||
for choice in choices.values():
|
||||
embed.add_field(
|
||||
@ -174,7 +167,7 @@ class McRoll(commands.Cog):
|
||||
else bytes(choice['reaction'], "utf-8").decode("unicode_escape"))
|
||||
return pollMessage
|
||||
|
||||
async def declare_winner(self):
|
||||
async def declare_winner(self, choices, channelId):
|
||||
embed = discord.Embed(title="Winner", description="Congratulations")
|
||||
for item in list(choices):
|
||||
winner = choices[item]
|
||||
@ -185,9 +178,13 @@ class McRoll(commands.Cog):
|
||||
"Votes : " + str(winner['votes']) + '\n' +
|
||||
"Calculated Votes: " + str(winner['votes'] * round(2 * math.log10(winner['lastActivated'] + 1) + 1, 2))
|
||||
)
|
||||
channel = await self.client.fetch_channel(channelId)
|
||||
await channel.send(embed=embed)
|
||||
await channel.send("@everyone")
|
||||
|
||||
await self.channel.send(embed=embed)
|
||||
await self.channel.send("@everyone")
|
||||
async def get_message(self, messageId, channelId):
|
||||
channel = await self.client.fetch_channel(channelId)
|
||||
return await channel.fetch_message(messageId)
|
||||
|
||||
|
||||
def setup(client):
|
||||
|
Loading…
x
Reference in New Issue
Block a user