我怎样才能使这段代码更短(copy/pasted 相同的行多次)?
How can I make this code shorter (copy/pasted the same lines many times over)?
所以我有一段代码看起来像这样:
import discord, asyncio
client = discord.Client()
@client.event
async def on_member_update(preupd, postupd):
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('5')).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#5>")
await client.send_message(client.get_channel('5'), embed=embed)
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('6')).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#6>")
await client.send_message(client.get_channel('6'), embed=embed)
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('7')).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#7>")
await client.send_message(client.get_channel('7'), embed=embed)
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('8')).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#8>")
await client.send_message(client.get_channel('8'), embed=embed)
client.run('ClientTokenHere')
如您所见,我 copy/pasted 几乎相同的东西 4 次,但细节略有不同。所以我试图通过制作这样的函数来缩短代码:
import discord, asyncio
client = discord.Client()
@client.event
async def on_member_update(preupd, postupd):
async def shorter(numba):
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel(str(numba))).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#%s>" % numba)
await client.send_message(client.get_channel(str(numba)), embed=embed)
shorter('5')
shorter('6')
shorter('7')
shorter('8')
client.run('ClientTokenHere')
然而,这给了我一个错误:myfile.py:35: RuntimeWarning: coroutine 'on_member_update.<locals>.perms' was never awaited
,即使我 运行 顶部的 "longer" 版本代码,一切 运行 都很好。显然我以某种方式错误地定义了函数。我怎样才能解决这个问题?或者,还有更好的方法?谢谢
您也可以这样做:
import discord, asyncio
client = discord.Client()
@client.event
async def on_member_update(preupd, postupd):
for numba in ('5', '6', '7', '8'):
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel(str(numba))).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#%s>" % numba)
await client.send_message(client.get_channel(str(numba)), embed=embed)
client.run('ClientTokenHere')
所以我有一段代码看起来像这样:
import discord, asyncio
client = discord.Client()
@client.event
async def on_member_update(preupd, postupd):
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('5')).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#5>")
await client.send_message(client.get_channel('5'), embed=embed)
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('6')).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#6>")
await client.send_message(client.get_channel('6'), embed=embed)
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('7')).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#7>")
await client.send_message(client.get_channel('7'), embed=embed)
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('8')).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#8>")
await client.send_message(client.get_channel('8'), embed=embed)
client.run('ClientTokenHere')
如您所见,我 copy/pasted 几乎相同的东西 4 次,但细节略有不同。所以我试图通过制作这样的函数来缩短代码:
import discord, asyncio
client = discord.Client()
@client.event
async def on_member_update(preupd, postupd):
async def shorter(numba):
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel(str(numba))).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#%s>" % numba)
await client.send_message(client.get_channel(str(numba)), embed=embed)
shorter('5')
shorter('6')
shorter('7')
shorter('8')
client.run('ClientTokenHere')
然而,这给了我一个错误:myfile.py:35: RuntimeWarning: coroutine 'on_member_update.<locals>.perms' was never awaited
,即使我 运行 顶部的 "longer" 版本代码,一切 运行 都很好。显然我以某种方式错误地定义了函数。我怎样才能解决这个问题?或者,还有更好的方法?谢谢
您也可以这样做:
import discord, asyncio
client = discord.Client()
@client.event
async def on_member_update(preupd, postupd):
for numba in ('5', '6', '7', '8'):
if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel(str(numba))).read_messages == True:
embed = discord.Embed(title="Test", colour=0xFF000)
embed.add_field(name="Test", value="Test <#%s>" % numba)
await client.send_message(client.get_channel(str(numba)), embed=embed)
client.run('ClientTokenHere')