clean code for #23

This commit is contained in:
Milka64 2020-12-13 16:51:19 +01:00
parent 283a3919f0
commit 29de2a539a

View file

@ -46,7 +46,10 @@ logger.addHandler(stream_handler)
class Mybot(commands.Cog): class Mybot(commands.Cog):
#Fonctions necesaires pour Kabot. #Fonctions necesaires pour Kabot.
def __init__(self, bot): def __init__(self, bot, gl_url=None, gl_token=None, gif_token=None):
self.gl_url = gl_url
self.gl_token = gl_token
self.gif_token = gif_token
self.bot = bot self.bot = bot
self.sounds = [] self.sounds = []
self.sounds_history = [] self.sounds_history = []
@ -74,14 +77,15 @@ class Mybot(commands.Cog):
@commands.command(help='list des commits') @commands.command(help='list des commits')
async def commits(self, ctx, number = 5): async def commits(self, ctx, number = 5):
number = int(number) if self.gl_url and self.gl_token:
gl = gitlab.Gitlab('https://git.0w.tf/', private_token='s-e3HXBUTroL52cNkAbe') number = int(number)
gl.auth() gl = gitlab.gitlab(self.gl_url, self.gl_token)
projects = gl.projects.list(search='Kabot')[0] gl.auth()
commits = projects.commits.list(all=True)[:number] projects = gl.projects.list(search='Kabot')[0]
for commit in commits: commits = projects.commits.list(all=True)[:number]
detail = commit.attributes for commit in commits:
await ctx.channel.send("__" + detail['author_name'] + "__: " + detail['title'] + '\n' + detail['web_url']) detail = commit.attributes
await ctx.channel.send("__" + detail['author_name'] + "__: " + detail['title'] + '\n' + detail['web_url'])
@commands.command(help='clear cache youtube dl') @commands.command(help='clear cache youtube dl')
async def cache_clear(self, ctx): async def cache_clear(self, ctx):
@ -97,34 +101,35 @@ class Mybot(commands.Cog):
@commands.command(help="Interrogation issues \n Args: list, search[mot clé] et add[nom de l'issue]") @commands.command(help="Interrogation issues \n Args: list, search[mot clé] et add[nom de l'issue]")
async def issue(self, ctx, *args): async def issue(self, ctx, *args):
if args: if self.gl_url and self.gl_token:
args = list(args) if args:
gl = gitlab.Gitlab('https://git.0w.tf/', private_token='s-e3HXBUTroL52cNkAbe') args = list(args)
gl.auth() gl = gitlab.gitlab(self.gl_url, self.gl_token)
if args[0] == 'list': gl.auth()
projects = gl.projects.list(search='Kabot')[0] if args[0] == 'list':
issues = projects.issues.list() projects = gl.projects.list(search='Kabot')[0]
for issue in issues: issues = projects.issues.list()
if "closed" == issue.state: for issue in issues:
pass if "closed" == issue.state:
else: pass
await ctx.channel.send('#' + str(issue.id) + ": " + issue.title + '\n' + issue.web_url) else:
elif args[0] == 'search': await ctx.channel.send('#' + str(issue.id) + ": " + issue.title + '\n' + issue.web_url)
query = ''.join(args[1:]) elif args[0] == 'search':
project = gl.projects.list(search='Kabot')[0] query = ''.join(args[1:])
find_issues = project.search("issues", query) project = gl.projects.list(search='Kabot')[0]
for issue in find_issues: find_issues = project.search("issues", query)
await ctx.channel.send("#" + str(issue['id']) + ": " + issue['title'] + '\n' + issue['web_url']) for issue in find_issues:
elif args[0] == 'add': await ctx.channel.send("#" + str(issue['id']) + ": " + issue['title'] + '\n' + issue['web_url'])
title = ' '.join(args[1:]) elif args[0] == 'add':
author = title + ' - By ' + ctx.message.author.name title = ' '.join(args[1:])
projects = gl.projects.list() author = title + ' - By ' + ctx.message.author.name
for project in projects: projects = gl.projects.list()
if "Kabot" == project.name: for project in projects:
issue = project.issues.create({'title': author}) if "Kabot" == project.name:
await ctx.channel.send(issue.web_url) issue = project.issues.create({'title': author})
else: await ctx.channel.send(issue.web_url)
await ctx.channel.send('unknown command') else:
await ctx.channel.send('unknown command')
@commands.command() @commands.command()
async def join(self, ctx): async def join(self, ctx):
@ -205,16 +210,20 @@ class Mybot(commands.Cog):
query = ctx.message.content.replace('!gif ', '') query = ctx.message.content.replace('!gif ', '')
print(query) print(query)
api_instance = giphy_client.DefaultApi() api_instance = giphy_client.DefaultApi()
api_key = "udhL9Rgdme3HpAoFhWVZoYM0l0XwkzLz" api_key = self.gif_token
lang = 'fr' lang = 'fr'
try: if api_key:
api_response = api_instance.gifs_search_get(api_key, query, lang=lang, limit=15) try:
api_response.to_dict()['data'][0] api_response = api_instance.gifs_search_get(api_key, query, lang=lang, limit=15)
get_url = random.choice(api_response.to_dict()['data']) api_response.to_dict()['data'][0]
get_url['url'] get_url = random.choice(api_response.to_dict()['data'])
await ctx.channel.send(get_url['url']) get_url['url']
except ApiException as e: await ctx.channel.send(get_url['url'])
await ctx.channel.send("Exception when calling DefaultApi->gifs_search_get: %s\n" % e) except ApiException as e:
await ctx.channel.send("Exception when calling DefaultApi->gifs_search_get: %s\n" % e)
else:
await ctx.channel.send("Exception : No api key found")
@commands.guild_only() @commands.guild_only()
@commands.command() @commands.command()
@ -526,7 +535,15 @@ def get_word(word_type):
def main(): def main():
logger.info("Initialisation de Kabot") logger.info("Initialisation de Kabot")
token = "NjI3MTM3NDY1MDA5ODMxOTQ2.Xxl5dg.-eXs6op39SyB1psp4QZIXv7kPuY" token = "NjI3MTM3NDY1MDA5ODMxOTQ2.Xxl5dg.-eXs6op39SyB1psp4QZIXv7kPuY"
bot = commands.Bot(command_prefix='!') gl_url = 'https://git.0w.tf/'
gl_token='s-e3HXBUTroL52cNkAbe'
gif_token = "udhL9Rgdme3HpAoFhWVZoYM0l0XwkzLz"
bot = commands.Bot(
command_prefix='!',
gl_url=gl_url,
gl_token=gl_token,
gif_token=git_token
)
test = False test = False
if "test" in argv[-1]: if "test" in argv[-1]: