glmn
glmn

Reputation: 75

How to remove code repeatings in this situation

I'm looking for a solution to remove code repeatings. I have a lot of duplicates of this line:

await msg.delete(delay=config['delay']['delete'])

In my code

async def untrack(ctx, playerName=None):
  author = ctx.message.author
  channel = ctx.message.channel
  await ctx.message.delete(delay=config['delay']['delete'])

  if playerName is None: 
    if config['bot']['track_only_one']:
      players = db.getAuthorTrackedPlayers(author, channel)
      if len(players) > 0:
        playerId = players[0]
        playerName = db.getPlayerNameById(playerId)
      else:
        msg = await ctx.send('{}, your track list already empty'.format(author.mention))
        await msg.delete(delay=config['delay']['delete'])
        return False
    else:
      msg = await ctx.send('{}, type !pdb-untrack \'player_name\''.format(author.mention))
      await msg.delete(delay=config['delay']['delete'])
      return False

  playerId = db.getPlayerIdByName(playerName)
  if playerId == -1:
    msg = await ctx.send('{}, {} doesn\'t found in tracked players'.format(author.mention, playerName))
    await msg.delete(delay=config['delay']['delete'])
    return False

  if db.removePlayerFromAuthor(author, channel, playerId):
    msg = await ctx.send('{}, {} removed from your track list'.format(author.mention, playerName))
    await msg.delete(delay=config['delay']['delete'])
  else:
    msg = await ctx.send('{}, {} is not in your track list'.format(author.mention, playerName))
    await msg.delete(delay=config['delay']['delete'])

Maybe somebody can help me to understand how to fix this problem.

UPDATE New code after your suggestions

async def send_destruct_message(ctx, message=None):
  if message:
    msg = await ctx.send(message)
  else:
    msg = ctx.message
  await msg.delete(delay = config['delay']['delete'])

async def untrack(ctx, player_name=None):
  author = ctx.message.author
  channel = ctx.message.channel
  await ctx.message.delete(delay=config['delay']['delete'])

  if player_name is not None:
    player_id = db.get_player_id_by_name(player_name)
  else: 
    if not config['bot']['track_only_one']:
      await send_destruct_message(ctx, '{}, type !pdb-untrack \'player_name\''.format(author.mention))
      return False  
    try:
      player_id = db.get_author_tracked_players(author, channel)[0]
      player_name = db.get_player_name_by_id(player_id)  
    except IndexError:
      await send_destruct_message(ctx, '{}, your track list already empty'.format(author.mention))
      return False

  if player_id == -1:
    await send_destruct_message(ctx, '{}, {} doesn\'t found in tracked players'.format(author.mention, player_name))
    return False

  if not db.remove_player_from_author(author, channel, player_id):
    await send_destruct_message(ctx, '{}, {} is not in your track list'.format(author.mention, player_name))
    return False

  await send_destruct_message(ctx, '{}, {} removed from your track list'.format(author.mention, player_name))
  return True

Upvotes: 0

Views: 81

Answers (2)

Waket Zheng
Waket Zheng

Reputation: 6361

How about this, and I strongly recommend to use player_name instead of playerName

async def delete_message(ctx, message=None):
    if message:
        msg = await ctx.send(message)
    else:
        msg = ctx.message
    await msg.delete(delay = config['delay']['delete'])

async def untrack(ctx, player_name=None):
  author, channel = ctx.message.author, ctx.message.channel
  await delete_message(ctx)

  if player_name is None: 
    if config['bot']['track_only_one']:
      players = db.getAuthorTrackedPlayers(author, channel)
      if len(players) > 0:
        player_id = players[0]
        player_name = db.getPlayerNameById(playerId)
      else:
        await delete_message(ctx, '{}, your track list already empty'.format(author.mention))
        return False
   ...

Upvotes: 2

Barmar
Barmar

Reputation: 781761

Define a function that does what those two lines do.

async def send_message (ctx, config, message):
    msg = await ctx.send(message)
    await msg.delete(delay = config['delay']['delete'])

Then you can call it like this:

if playerid == 1:
    await send_message(ctx, config, '{}, {} doesn\'t found in tracked players'.format(author.mention, playerName)
    return False

if db.removePlayerFromAuthor(author, channel, playerId):
    await send_message(ctx, config, '{}, {} removed from your track list'.format(author.mention, playerName))
else:
    await send_message(ctx, config, '{}, {} is not in your track list'.format(author.mention, playerName))

Upvotes: 0

Related Questions