Added linked role and unlink method

This commit is contained in:
2020-10-01 19:10:45 +02:00
parent 412fc61810
commit e50f56a7e2
3 changed files with 38 additions and 16 deletions

View File

@@ -3,6 +3,7 @@ from discord.ext import commands
import random
import string
import socket
from data import constants
def get_random_string(length):
# Random string with the combination of lower and upper case
@@ -13,13 +14,13 @@ def get_random_string(length):
class PlayerError(Exception):
pass
def send_chat(minecraftname, code):
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
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.send(bytes("{}\t{}\n".format(minecraftname, code), encoding="ascii"))
s.send(bytes("{}\t{}\t{}\n".format(discordname, minecraftname, code), encoding="ascii"))
data = s.recv(1024)
data_string = data.decode("utf-8").strip('\n')
if data_string == "PlayerError":
@@ -30,6 +31,10 @@ class PlayerLink(commands.Cog):
def __init__(self, client):
self.client = client
def get_linked_role(self):
return discord.utils.get(self.client.get_guild(constants.WorldCraft).roles, id=constants.roleLinked)
@commands.command(name="Link")
async def link(self, ctx, arg):
@@ -41,19 +46,24 @@ class PlayerLink(commands.Cog):
return message.channel.id == channelid and message.author.id == authorid
try:
if send_chat(arg, code):
await ctx.send("A code has been sent to your minecraft chat in the WorldCraft server.\nSend it in this channel.")
msg = await self.client.wait_for('message', check=check)
if msg.content == code:
await ctx.send("The link was successfull")
else:
await ctx.send("Wrong code")
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.")
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")
else:
await ctx.send("Wrong code")
except PlayerError:
await ctx.send("Player '" + arg + "' not found")
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")
def setup(client):