Added unlink support

This commit is contained in:
2020-10-22 22:39:56 +02:00
parent 72a54ff402
commit a4d8abf48c
3 changed files with 109 additions and 14 deletions

68
cogs/playerlink.py Normal file → Executable file
View File

@@ -6,6 +6,10 @@ import socket
import asyncio
import threading
from data import constants
from data.DatabaseConnection import *
# 25216 --> uuid server
# 25224 --> send chat server
def get_random_string(length):
# Random string with the combination of lower and upper case
@@ -18,9 +22,22 @@ class PlayerError(Exception):
class WrongCodeError(Exception):
pass
def get_player_uuid(minecraftname):
HOST = '192.168.1.214' # The server's hostname or IP address
PORT = 25216 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.send(bytes("{}\n".format(minecraftname), encoding="ascii"))
data = s.recv(1024)
data_string = data.decode("utf-8").strip('\n')
if data_string == "PlayerError":
raise PlayerError()
return data_string
def send_chat(discordname, minecraftname, code):
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 3333 # The port used by the server
HOST = '192.168.1.214' # The server's hostname or IP address
PORT = 25224 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
@@ -29,7 +46,7 @@ def send_chat(discordname, minecraftname, code):
data_string = data.decode("utf-8").strip('\n')
if data_string == "PlayerError":
raise PlayerError()
return True
return data_string
class PlayerLink(commands.Cog):
def __init__(self, client):
@@ -46,6 +63,7 @@ class PlayerLink(commands.Cog):
authorid = ctx.author.id
code = get_random_string(8)
def check(message: discord.Message):
return message.channel.id == channelid and message.author.id == authorid
@@ -56,15 +74,18 @@ class PlayerLink(commands.Cog):
embed.add_field(name="Minecraft Name:", value=f"{minecraftname}", inline=False)
embed.add_field(name="Discord Name:", value=f"{discord_author.mention}", inline=False)
# use dictionary and set color string before the message: example => r(ed):message
if isinstance(error, WrongCodeError):
embed.add_field(name="Error", value=f"The code is wrong!", inline=False)
embed.colour = discord.Colour.red()
elif (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()
elif (timer.ended):
embed.add_field(name="Timer", value=f"The code is expired!", inline=False)
embed.colour = discord.Colour.red()
elif (timer.minutes == 2):
embed.add_field(name="Timer", value=f"The code will expire in { str(timer.minutes)} minutes", inline=False)
embed.colour = discord.Colour.orange()
@@ -86,6 +107,8 @@ class PlayerLink(commands.Cog):
try:
uuid = get_player_uuid(arg)
send_chat(ctx.author.name, arg, code)
#message_send_time =
timer = Timer()
@@ -98,9 +121,17 @@ class PlayerLink(commands.Cog):
msg = await self.client.wait_for('message', check=check)
if msg.content == code:
await ctx.author.add_roles(self.get_linked_role())
timer.ended = True
await message.edit(embed=create_embed(ctx.author, arg, timer, success=True))
dbLinker = PlayerDBLinker()
try:
dbLinker.linkPlayer(uuid, authorid)
await ctx.author.add_roles(self.get_linked_role())
timer.ended = True
await message.edit(embed=create_embed(ctx.author, arg, timer, success=True))
except SQLInsertError:
await message.edit(embed=create_embed(ctx.author, arg, timer))
finally:
dbLinker.closeconnections()
else:
# this stops the timer task
task.cancel()
@@ -113,9 +144,22 @@ class PlayerLink(commands.Cog):
# 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")
async def unlink(self, ctx, arg):
authorid = ctx.author.id
dbLinker = PlayerDBLinker()
try:
dbLinker.unlinkPlayer(arg, authorid)
await ctx.author.remove_roles(self.get_linked_role())
await ctx.send("Unlinked your account")
except UnLinkError:
await ctx.send("The unlink was unsuccessfull")
except WrongMinecraftName:
await ctx.send("Wrong minecraft name!")
except PlayerNotLinked:
await ctx.send("Player not linked!")
finally:
dbLinker.closeconnections()
class Timer():
def __init__(self):