在这种情况下如何删除代码重复
How to remove code repeatings in this situation
我正在寻找删除代码重复的解决方案。
我有很多重复的这一行:
await msg.delete(delay=config['delay']['delete'])
在我的代码中
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'])
也许有人可以帮助我了解如何解决这个问题。
更新
根据您的建议新代码
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
定义一个函数来执行这两行的操作。
async def send_message (ctx, config, message):
msg = await ctx.send(message)
await msg.delete(delay = config['delay']['delete'])
那么你可以这样称呼它:
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))
这个怎么样,我强烈建议使用player_name
而不是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
...
我正在寻找删除代码重复的解决方案。 我有很多重复的这一行:
await msg.delete(delay=config['delay']['delete'])
在我的代码中
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'])
也许有人可以帮助我了解如何解决这个问题。
更新 根据您的建议新代码
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
定义一个函数来执行这两行的操作。
async def send_message (ctx, config, message):
msg = await ctx.send(message)
await msg.delete(delay = config['delay']['delete'])
那么你可以这样称呼它:
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))
这个怎么样,我强烈建议使用player_name
而不是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
...