🎨 - Overall code cleaning

This commit is contained in:
Florian SYLVAIN
2021-02-08 22:30:58 +01:00
parent 2c2f4bc81e
commit 7a4b3d68aa
+17 -7
View File
@@ -10,9 +10,10 @@ from emoji import demojize
from twitch import TwitchClient from twitch import TwitchClient
bot = commands.Bot(command_prefix='$') bot = commands.Bot(command_prefix='$')
twitch_client = TwitchClient(client_id = ID_TWITCH, oauth_token = TOKEN_TWITCH) twitch_client = TwitchClient(client_id=ID_TWITCH, oauth_token=TOKEN_TWITCH)
chats = dict() chats = dict()
class ChatObj: class ChatObj:
def __init__(self, twitch_chan, discord_chan): def __init__(self, twitch_chan, discord_chan):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -25,7 +26,7 @@ class ChatObj:
self.sock.connect((SERVER, PORT)) self.sock.connect((SERVER, PORT))
self.sock.send(f"PASS {TOKEN_TWITCH}\n".encode('utf-8')) self.sock.send(f"PASS {TOKEN_TWITCH}\n".encode('utf-8'))
self.sock.send(f"NICK {NICKNAME}\n".encode('utf-8')) self.sock.send(f"NICK {NICKNAME}\n".encode('utf-8'))
self.sock.send(f"JOIN {'#'+self.twitch_chan}\n".encode('utf-8')) self.sock.send(f"JOIN {'#' + self.twitch_chan}\n".encode('utf-8'))
self.sock.settimeout(0.0) self.sock.settimeout(0.0)
self.sock.setblocking(0) self.sock.setblocking(0)
return 1 return 1
@@ -63,18 +64,21 @@ class ChatObj:
except OSError: except OSError:
pass pass
def check_user(name): def check_user(name):
user = twitch_client.users.translate_usernames_to_ids([name]) user = twitch_client.users.translate_usernames_to_ids([name])
if user == []: if not user:
return False return False
return True return True
@bot.event @bot.event
async def on_ready(): async def on_ready():
default_activity = discord.Activity(type=discord.ActivityType.listening, name="$help") default_activity = discord.Activity(type=discord.ActivityType.listening, name="$help")
await bot.change_presence(activity=default_activity) await bot.change_presence(activity=default_activity)
print('We have logged in as {0.user}'.format(bot)) print('We have logged in as {0.user}'.format(bot))
@bot.command() @bot.command()
async def chat_set(ctx, arg1): async def chat_set(ctx, arg1):
await ctx.message.delete() await ctx.message.delete()
@@ -90,7 +94,8 @@ async def chat_set(ctx, arg1):
else: else:
await ctx.send('```Something went wrong when trying to access Twitch IRC.```') await ctx.send('```Something went wrong when trying to access Twitch IRC.```')
else: else:
await ctx.send(r'```This twitch channel doesn\'t seems to exist.```') await ctx.send('```This twitch channel doesn\'t seems to exist.```')
@bot.command() @bot.command()
async def chat_stop(ctx): async def chat_stop(ctx):
@@ -105,6 +110,7 @@ async def chat_stop(ctx):
message = '```There is no active chat in this channel.```' message = '```There is no active chat in this channel.```'
await ctx.send(message) await ctx.send(message)
@bot.command() @bot.command()
async def rank(ctx, arg, argf=None): async def rank(ctx, arg, argf=None):
await ctx.message.delete() await ctx.message.delete()
@@ -113,10 +119,12 @@ async def rank(ctx, arg, argf=None):
player = WATCHER.summoner.by_name(REGION, arg) player = WATCHER.summoner.by_name(REGION, arg)
ranks = rank_track(player) ranks = rank_track(player)
embed = discord.Embed(title=arg, url="https://www.youtube.com/watch?v=dQw4w9WgXcQ") embed = discord.Embed(title=arg, url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
embed.set_thumbnail(url="http://ddragon.leagueoflegends.com/cdn/11.2.1/img/profileicon/" + str(player['profileIconId']) + ".png") embed.set_thumbnail(url="http://ddragon.leagueoflegends.com/cdn/11.2.1/img/profileicon/" + str(
player['profileIconId']) + ".png")
if isinstance(ranks, list): if isinstance(ranks, list):
for rank_s in ranks: for rank_s in ranks:
embed.add_field(name=rank_s[0], value=str(rank_s[1]) + ' ' + str(rank_s[2]) + ' ' + str(rank_s[3]) + ' LP' + '\n' + str(rank_s[4]) + 'W/' + str(rank_s[5]) + 'L', inline = False) embed.add_field(name=rank_s[0], value=str(rank_s[1]) + ' ' + str(rank_s[2]) + ' ' + str(
rank_s[3]) + ' LP' + '\n' + str(rank_s[4]) + 'W/' + str(rank_s[5]) + 'L', inline=False)
else: else:
embed.description = ranks embed.description = ranks
await ctx.send(embed=embed) await ctx.send(embed=embed)
@@ -125,8 +133,9 @@ async def rank(ctx, arg, argf=None):
else: else:
await ctx.send("If you are trying to use a nickname with spaces, please surround it with quotes.") await ctx.send("If you are trying to use a nickname with spaces, please surround it with quotes.")
@bot.command() @bot.command()
async def issou(ctx, arg1:discord.User=None): async def issou(ctx, arg1: discord.User = None):
if arg1 is not None: if arg1 is not None:
user = arg1 user = arg1
else: else:
@@ -146,4 +155,5 @@ async def issou(ctx, arg1:discord.User=None):
else: else:
await ctx.send('You or the targeted person are not connected to any channel.') await ctx.send('You or the targeted person are not connected to any channel.')
bot.run(TOKEN_BOT) bot.run(TOKEN_BOT)