mirror of
https://github.com/Floriansylvain/Risitas_BOT.git
synced 2026-08-19 11:43:16 +02:00
🎨 - Syntax improvement and compliance with the PEP 8 convention
This commit is contained in:
@@ -1,17 +1,19 @@
|
|||||||
import asyncio, socket, re, discord
|
import asyncio
|
||||||
from discord.ext import tasks, commands
|
import socket
|
||||||
|
import re
|
||||||
|
import discord
|
||||||
from private import *
|
from private import *
|
||||||
from riot_api import rank_track, watcher, region
|
from discord.ext import tasks, commands
|
||||||
|
from riot_api import rank_track, WATCHER, REGION
|
||||||
from riotwatcher import ApiError
|
from riotwatcher import ApiError
|
||||||
from emoji import demojize
|
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(object):
|
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)
|
||||||
self.twitch_chan = twitch_chan
|
self.twitch_chan = twitch_chan
|
||||||
@@ -20,14 +22,14 @@ class ChatObj(object):
|
|||||||
|
|
||||||
def chat_init(self):
|
def chat_init(self):
|
||||||
try:
|
try:
|
||||||
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
|
||||||
except OSError as test:
|
except OSError:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@tasks.loop(seconds=1)
|
@tasks.loop(seconds=1)
|
||||||
@@ -43,14 +45,18 @@ class ChatObj(object):
|
|||||||
|
|
||||||
elif len(resp) > 0:
|
elif len(resp) > 0:
|
||||||
try:
|
try:
|
||||||
username, channel, message = re.search(':(.*)\!.*@.*\.tmi\.twitch\.tv PRIVMSG #(.*) :(.*)', demojize(resp)).groups()
|
chain = re.search(r':(.*)\!.*@.*\.tmi\.twitch\.tv PRIVMSG #(.*) :(.*)', demojize(resp))
|
||||||
|
username = chain.group(1)
|
||||||
|
message = chain.group(3)
|
||||||
msg = f"**__{username}__ :** {message}"
|
msg = f"**__{username}__ :** {message}"
|
||||||
await self.discord_chan.send(msg)
|
await self.discord_chan.send(msg)
|
||||||
except Exception:
|
except discord.DiscordException:
|
||||||
|
pass
|
||||||
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@twitch.after_loop
|
@twitch.after_loop
|
||||||
async def after_twitch():
|
async def after_twitch(self):
|
||||||
self.sock.shutdown(1)
|
self.sock.shutdown(1)
|
||||||
try:
|
try:
|
||||||
self.sock.close()
|
self.sock.close()
|
||||||
@@ -65,12 +71,13 @@ def check_user(name):
|
|||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=f"$help"))
|
default_activity = discord.Activity(type=discord.ActivityType.listening, name="$help")
|
||||||
|
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()
|
||||||
chan = ctx.channel
|
chan = ctx.channel
|
||||||
if chan in list(chats):
|
if chan in list(chats):
|
||||||
await ctx.send("```A chat is already active, you have to stop it before with the cmd $chat_stop !```")
|
await ctx.send("```A chat is already active, you have to stop it before with the cmd $chat_stop !```")
|
||||||
@@ -83,10 +90,11 @@ 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('```This twitch channel doesn\'t seems to exist.```')
|
await ctx.send(r'```This twitch channel doesn\'t seems to exist.```')
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def chat_stop(ctx):
|
async def chat_stop(ctx):
|
||||||
|
await ctx.message.delete()
|
||||||
chan = ctx.channel
|
chan = ctx.channel
|
||||||
if chan in list(chats):
|
if chan in list(chats):
|
||||||
chats[chan].twitch.stop()
|
chats[chan].twitch.stop()
|
||||||
@@ -98,17 +106,17 @@ async def chat_stop(ctx):
|
|||||||
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):
|
||||||
if argF is None:
|
await ctx.message.delete()
|
||||||
|
if argf is None:
|
||||||
try:
|
try:
|
||||||
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 type(ranks) is list:
|
if isinstance(ranks, list):
|
||||||
for i in range(len(ranks)):
|
for rank_s in ranks:
|
||||||
text = str(ranks[i][1]) + ' ' + str(ranks[i][2]) + ' ' + str(ranks[i][3]) + ' LP' + '\n' + str(ranks[i][4]) + 'W/' + str(ranks[i][5]) + 'L'
|
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 = ranks[i][0], value = text, inline = False)
|
|
||||||
else:
|
else:
|
||||||
embed.description = ranks
|
embed.description = ranks
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
@@ -129,15 +137,13 @@ async def issou(ctx, arg1:discord.User=None):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
voice_channel = None
|
voice_channel = None
|
||||||
|
|
||||||
channel = None
|
|
||||||
if voice_channel is not None:
|
if voice_channel is not None:
|
||||||
channel = voice_channel.name
|
voice = await voice_channel.connect()
|
||||||
vc = await voice_channel.connect()
|
voice.play(discord.FFmpegPCMAudio('issou.mp3'))
|
||||||
vc.play(discord.FFmpegPCMAudio('issou.mp3'))
|
if voice.is_playing():
|
||||||
if vc.is_playing():
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
await vc.disconnect()
|
await voice.disconnect()
|
||||||
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)
|
||||||
|
|||||||
+5
-4
@@ -1,11 +1,12 @@
|
|||||||
from riotwatcher import LolWatcher
|
from riotwatcher import LolWatcher
|
||||||
from private import token_riot
|
from private import TOKEN_RIOT
|
||||||
|
|
||||||
watcher = LolWatcher(token_riot)
|
WATCHER = LolWatcher(TOKEN_RIOT)
|
||||||
region = 'EUW1'
|
REGION = 'EUW1'
|
||||||
|
|
||||||
def rank_track(player):
|
def rank_track(player):
|
||||||
ranked_stats = watcher.league.by_summoner(region, player['id'])
|
'''function that communicate with Riot API'''
|
||||||
|
ranked_stats = WATCHER.league.by_summoner(REGION, player['id'])
|
||||||
|
|
||||||
if not ranked_stats:
|
if not ranked_stats:
|
||||||
return 'Unranked cette saison.'
|
return 'Unranked cette saison.'
|
||||||
|
|||||||
Reference in New Issue
Block a user