Created embed message and added timer
This commit is contained in:
@@ -3,6 +3,8 @@ from discord.ext import commands
|
||||
import random
|
||||
import string
|
||||
import socket
|
||||
import asyncio
|
||||
import threading
|
||||
from data import constants
|
||||
|
||||
def get_random_string(length):
|
||||
@@ -13,6 +15,8 @@ def get_random_string(length):
|
||||
|
||||
class PlayerError(Exception):
|
||||
pass
|
||||
class WrongCodeError(Exception):
|
||||
pass
|
||||
|
||||
def send_chat(discordname, minecraftname, code):
|
||||
HOST = '127.0.0.1' # The server's hostname or IP address
|
||||
@@ -45,26 +49,77 @@ class PlayerLink(commands.Cog):
|
||||
def check(message: discord.Message):
|
||||
return message.channel.id == channelid and message.author.id == authorid
|
||||
|
||||
def create_embed(discord_author, minecraftname, timer, error=None, success=False):
|
||||
embed = discord.Embed(title="WorldCraft Linker")
|
||||
embed.add_field(name="How to:", value= "A code has been sent to your minecraft chat in the WorldCraft server.\nSend the code in this channel.")
|
||||
embed.add_field(name="Minecraft Name:", value=f"{minecraftname}", inline=False)
|
||||
embed.add_field(name="Discord Name:", value=f"{discord_author.mention}", inline=False)
|
||||
|
||||
if isinstance(error, WrongCodeError):
|
||||
embed.add_field(name="Error", value=f"The code is wrong!", inline=False)
|
||||
embed.colour = discord.Colour.red()
|
||||
elif (timer.minutes == 0 and timer.seconds == 0 and timer.ended):
|
||||
embed.add_field(name="Timer", value=f"The code is expired!", inline=False)
|
||||
embed.colour = discord.Colour.red()
|
||||
elif (success == True):
|
||||
embed.add_field(name="Success", value=f"The link was successfull!", inline=False)
|
||||
embed.colour = discord.Colour.green()
|
||||
else:
|
||||
embed.add_field(name="Timer", value=f"The code will expire in {str(timer.minutes).zfill(2)}:{str(timer.seconds).zfill(2)}", inline=False)
|
||||
embed.colour = discord.Colour.orange()
|
||||
return embed
|
||||
|
||||
async def update_timer(timer, message):
|
||||
while (not timer.ended):
|
||||
await asyncio.sleep(1)
|
||||
timer.update()
|
||||
await message.edit(embed=create_embed(ctx.author, arg, timer))
|
||||
|
||||
try:
|
||||
send_chat(ctx.author.name, arg, code)
|
||||
await ctx.send("A code has been sent to your minecraft chat in the WorldCraft server.\nSend it in this channel.")
|
||||
timer = Timer()
|
||||
message = await ctx.send(embed=create_embed(ctx.author, arg, timer))
|
||||
|
||||
# Start timer in background
|
||||
task = asyncio.create_task(update_timer(timer, message))
|
||||
|
||||
msg = await self.client.wait_for('message', check=check)
|
||||
|
||||
if msg.content == code:
|
||||
await ctx.author.add_roles(self.get_linked_role())
|
||||
await ctx.send("The link was successfull")
|
||||
timer.ended = True
|
||||
await message.edit(embed=create_embed(ctx.author, arg, timer, success=True))
|
||||
else:
|
||||
await ctx.send("Wrong code")
|
||||
# this stops the timer loop
|
||||
task.cancel()
|
||||
timer.ended = True
|
||||
await message.edit(embed=create_embed(ctx.author, arg, timer, WrongCodeError()))
|
||||
|
||||
except PlayerError:
|
||||
await ctx.send("Player '" + arg + "' not found")
|
||||
except:
|
||||
await ctx.send("Something went wrong")
|
||||
#except:
|
||||
# await ctx.send("Something went wrong")
|
||||
|
||||
@commands.command(name="Unlink")
|
||||
async def unlink(self, ctx):
|
||||
await ctx.author.remove_roles(self.get_linked_role())
|
||||
await ctx.send("Unlinked your account")
|
||||
|
||||
class Timer():
|
||||
def __init__(self):
|
||||
self.minutes = 2
|
||||
self.seconds = 0
|
||||
self.ended = False
|
||||
|
||||
def update(self):
|
||||
if (self.minutes > 0 and self.seconds == 0):
|
||||
self.minutes -= 1
|
||||
self.seconds = 59
|
||||
elif (self.seconds > 0):
|
||||
self.seconds -= 1
|
||||
if (self.minutes == 0 and self.seconds == 0):
|
||||
self.ended = True
|
||||
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(PlayerLink(client))
|
||||
|
||||
Reference in New Issue
Block a user